From eae076a67767b932fa0812518d852d63a1301af1 Mon Sep 17 00:00:00 2001 From: liujiahua123123 Date: Thu, 8 Aug 2019 22:12:02 +0800 Subject: [PATCH] events --- .../net/mamoe/mirai/event/Cancelable.java | 7 +++ .../net/mamoe/mirai/event/MiraiEvent.java | 2 +- .../net/mamoe/mirai/event/MiraiEventHook.java | 44 +++++++++++++++++ .../mamoe/mirai/event/MiraiEventManager.java | 49 +++++++++++++++++++ .../java/net/mamoe/mirai/event/MiraiHook.java | 4 -- .../mirai/event/MiraiHookPreferences.java | 9 ++++ 6 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java delete mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHook.java create mode 100644 mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java b/mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java new file mode 100644 index 000000000..5c0e79116 --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/Cancelable.java @@ -0,0 +1,7 @@ +package net.mamoe.mirai.event; + +interface Cancelable { + + void setCancel(boolean value); + +} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java index bae753df5..541283429 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEvent.java @@ -1,5 +1,5 @@ package net.mamoe.mirai.event; -public class MiraiEvent { +public abstract class MiraiEvent { } 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 new file mode 100644 index 000000000..d4b7f37da --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventHook.java @@ -0,0 +1,44 @@ +package net.mamoe.mirai.event; + +import lombok.Getter; + +import java.util.function.Consumer; + +public class MiraiEventHook { + + @Getter + Class eventClass; + + @Getter + private Consumer handler; + @Getter + private MiraiHookPreferences preferences; + + + public MiraiEventHook(Class eventClass) { + this(eventClass,a -> {}); + } + + public MiraiEventHook(Class eventClass, Consumer handler){ + this(eventClass,handler,new MiraiHookPreferences()); + } + + public MiraiEventHook(Class eventClass, Consumer handler, MiraiHookPreferences preferences){ + this.eventClass = eventClass; + this.setHandler(handler); + this.setPreferences(preferences); + } + + public MiraiEventHook setHandler(Consumer handler){ + this.handler = handler; + return this; + } + + public MiraiEventHook setPreferences(MiraiHookPreferences preferences){ + this.preferences = preferences; + return this; + } + + + +} 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 new file mode 100644 index 000000000..d2566e3ef --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiEventManager.java @@ -0,0 +1,49 @@ +package net.mamoe.mirai.event; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.function.Consumer; +import java.util.function.Predicate; + +public class MiraiEventManager { + private MiraiEventManager(){ + + } + + private static MiraiEventManager instance; + + static MiraiEventManager getInstance(){ + if(MiraiEventManager.instance == null){ + MiraiEventManager.instance = new MiraiEventManager(); + } + return MiraiEventManager.instance; + } + + private Map, List>> hooks = new HashMap<>(); + + public void registerUntil(MiraiEventHook hook, Predicate toRemove){ + hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>()); + hooks.get(hook.getEventClass()).add(new MiraiEventConsumer<>(hook,toRemove)); + } + + public void registerOnce(MiraiEventHook hook){ + this.registerUntil(hook,(a) -> true); + } + + public void register(MiraiEventHook hook){ + this.registerUntil(hook,(a) -> false); + } + +} +@Data +@AllArgsConstructor +class MiraiEventConsumer{ + private MiraiEventHook hook; + private Predicate remove; +} + diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHook.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHook.java deleted file mode 100644 index 40bb85ca3..000000000 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHook.java +++ /dev/null @@ -1,4 +0,0 @@ -package net.mamoe.mirai.event; - -public class MiraiHook { -} diff --git a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java new file mode 100644 index 000000000..11dc939d3 --- /dev/null +++ b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java @@ -0,0 +1,9 @@ +package net.mamoe.mirai.event; + +import lombok.Data; + +@Data +public class MiraiHookPreferences { + private int priority = 0; + private boolean ignoreCanceled = true; +}