From 317ac754ce4de72a8ba13746023c3b70090a0811 Mon Sep 17 00:00:00 2001 From: liujiahua123123 <liujiahua123123@126.com> Date: Thu, 8 Aug 2019 23:03:10 +0800 Subject: [PATCH] Server --- .../main/java/net/mamoe/mirai/MiraiMain.java | 6 ++- .../java/net/mamoe/mirai/MiraiServer.java | 40 +++++++++++++++++++ .../net/mamoe/mirai/event/MiraiEventHook.java | 1 + .../mamoe/mirai/event/MiraiEventManager.java | 7 ++-- .../Cancellable.java} | 4 +- .../mirai/event/{ => events}/MiraiEvent.java | 13 +++++- .../events/server/ServerDisableEvent.java | 6 +++ .../events/server/ServerEnableEvent.java | 7 ++++ .../mamoe/mirai/task/MiraiTaskManager.java | 11 +++++ 9 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java rename mirai-core/src/main/java/net/mamoe/mirai/event/{Cancelable.java => events/Cancellable.java} (64%) rename mirai-core/src/main/java/net/mamoe/mirai/event/{ => events}/MiraiEvent.java (66%) create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerDisableEvent.java create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerEnableEvent.java diff --git a/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java b/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java index 44d4e4e49..1fb7004f6 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java @@ -5,7 +5,11 @@ package net.mamoe.mirai; * @author Him188moe @ Mirai Project */ public final class MiraiMain { + private static MiraiServer server; public static void main(String[] args) { - + server = new MiraiServer(); + Runtime.getRuntime().addShutdownHook(new Thread(() -> { + server.shutdown(); + })); } } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java b/mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java new file mode 100644 index 000000000..0713e9f22 --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/MiraiServer.java @@ -0,0 +1,40 @@ +package net.mamoe.mirai; + +import lombok.Getter; +import net.mamoe.mirai.event.MiraiEventManager; +import net.mamoe.mirai.event.events.MiraiEvent; +import net.mamoe.mirai.event.events.server.ServerDisableEvent; +import net.mamoe.mirai.event.events.server.ServerEnableEvent; +import net.mamoe.mirai.task.MiraiTaskManager; + +public class MiraiServer { + @Getter + private static MiraiServer instance; + + protected MiraiServer(){ + instance = this; + this.onLoad(); + } + + protected void shutdown(){ + this.getEventManager().boardcastEvent(new ServerDisableEvent()); + } + + + @Getter + MiraiEventManager eventManager; + @Getter + MiraiTaskManager taskManager; + + private void onLoad(){ + + this.eventManager = MiraiEventManager.getInstance(); + this.taskManager = MiraiTaskManager.getInstance(); + + this.eventManager.boardcastEvent(new ServerEnableEvent()); + } + + + + +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java index d4b7f37da..16d13b10f 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java @@ -1,6 +1,7 @@ package net.mamoe.mirai.event; import lombok.Getter; +import net.mamoe.mirai.event.events.MiraiEvent; import java.util.function.Consumer; diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java index d3378fc0c..f3d354f41 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java @@ -2,11 +2,12 @@ package net.mamoe.mirai.event; import lombok.AllArgsConstructor; import lombok.Data; +import net.mamoe.mirai.event.events.Cancellable; +import net.mamoe.mirai.event.events.MiraiEvent; import java.util.*; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; -import java.util.function.Consumer; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -17,7 +18,7 @@ public class MiraiEventManager { private static MiraiEventManager instance; - static MiraiEventManager getInstance(){ + public static MiraiEventManager getInstance(){ if(MiraiEventManager.instance == null){ MiraiEventManager.instance = new MiraiEventManager(); } @@ -68,7 +69,7 @@ class MiraiEventConsumer<T extends MiraiEvent>{ @SuppressWarnings("unchecked") public boolean accept(MiraiEvent event) { - if(!(event instanceof Cancelable && event.isCancelled() && hook.getPreferences().isIgnoreCanceled())){ + if(!(event instanceof Cancellable && event.isCancelled() && hook.getPreferences().isIgnoreCanceled())){ hook.getHandler().accept((T) event); } return remove.test((T)event); diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/Cancellable.java similarity index 64% rename from mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java rename to mirai-core/src/main/java/net/mamoe/mirai/event/events/Cancellable.java index b9e444c15..73d84f8bc 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/Cancellable.java @@ -1,8 +1,8 @@ -package net.mamoe.mirai.event; +package net.mamoe.mirai.event.events; import lombok.Getter; -interface Cancelable { +public interface Cancellable { boolean isCancelled(); diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/MiraiEvent.java similarity index 66% rename from mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java rename to mirai-core/src/main/java/net/mamoe/mirai/event/events/MiraiEvent.java index 93aeefe8f..b25fe0f79 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/MiraiEvent.java @@ -1,6 +1,6 @@ -package net.mamoe.mirai.event; +package net.mamoe.mirai.event.events; -import net.mamoe.jpre.event.Cancellable; +import net.mamoe.mirai.event.events.Cancellable; import net.mamoe.mirai.utils.EventException; public abstract class MiraiEvent { @@ -25,4 +25,13 @@ public abstract class MiraiEvent { this.cancelled = value; } + protected String eventName; + public String getEventName() { + if(this.eventName == null){ + return this.getClass().getSimpleName(); + } + return this.eventName; + } + + } diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerDisableEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerDisableEvent.java new file mode 100644 index 000000000..74a8795bc --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerDisableEvent.java @@ -0,0 +1,6 @@ +package net.mamoe.mirai.event.events.server; + +import net.mamoe.mirai.event.events.MiraiEvent; + +public class ServerDisableEvent extends MiraiEvent { +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerEnableEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerEnableEvent.java new file mode 100644 index 000000000..b019202ab --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/events/server/ServerEnableEvent.java @@ -0,0 +1,7 @@ +package net.mamoe.mirai.event.events.server; + +import net.mamoe.mirai.event.events.MiraiEvent; + +public class ServerEnableEvent extends MiraiEvent { + +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java index a71bd452e..a83919f67 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/task/MiraiTaskManager.java @@ -1,4 +1,15 @@ package net.mamoe.mirai.task; + public class MiraiTaskManager { + + private static MiraiTaskManager instance; + + public static MiraiTaskManager getInstance(){ + if(MiraiTaskManager.instance == null){ + MiraiTaskManager.instance = new MiraiTaskManager(); + } + return MiraiTaskManager.instance; + } + }