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 59f1b06ed..026b2c8e6 100644 --- a/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java +++ b/mirai-core/src/main/java/net/mamoe/mirai/MiraiMain.java @@ -18,5 +18,6 @@ public final class MiraiMain { new MiraiEventHook<>(ServerDisableEvent.class) .setHandler(a -> System.out.println("close")) ); + } } 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 16d13b10f..ec6df9d90 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,9 +1,11 @@ package net.mamoe.mirai.event; import lombok.Getter; +import net.mamoe.mirai.event.events.Cancellable; import net.mamoe.mirai.event.events.MiraiEvent; import java.util.function.Consumer; +import java.util.function.Predicate; public class MiraiEventHook { @@ -12,22 +14,23 @@ public class MiraiEventHook { @Getter private Consumer handler; - @Getter - private MiraiHookPreferences preferences; + @Getter + private int priority = 0; + + @Getter + private boolean ignoreCancelled = true; + + @Getter + private Predicate valid; 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){ @@ -35,11 +38,29 @@ public class MiraiEventHook { return this; } - public MiraiEventHook setPreferences(MiraiHookPreferences preferences){ - this.preferences = preferences; + public MiraiEventHook setPriority(int priority){ + this.priority = priority; + return this; + } + + public MiraiEventHook setIgnoreCancelled(boolean ignoreCancelled){ + this.ignoreCancelled = ignoreCancelled; return this; } + public MiraiEventHook setValid(Predicate valid) { + this.valid = valid; + return this; + } + + @SuppressWarnings("unchecked") + public boolean accept(MiraiEvent event) { + if(!(event instanceof Cancellable && event.isCancelled() && this.isIgnoreCancelled())){ + this.getHandler().accept((T) event); + } + return this.valid.test((T)event); + } + } 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 7df4e2ae4..47eb1c4d3 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 @@ -26,11 +26,11 @@ public class MiraiEventManager { } Lock hooksLock = new ReentrantLock(); - private Map, List>> hooks = new HashMap<>(); + 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)); + hooks.get(hook.getEventClass()).add(hook.setValid(toRemove)); } public void registerOnce(MiraiEventHook hook){ @@ -53,7 +53,7 @@ public class MiraiEventManager { hooks.put(event.getClass(), hooks.get(event.getClass()) .stream() - .sorted(Comparator.comparingInt(MiraiEventConsumer::getPriority)) + .sorted(Comparator.comparingInt(MiraiEventHook::getPriority)) .dropWhile(a -> a.accept(event)) .collect(Collectors.toList()) ); @@ -62,23 +62,5 @@ public class MiraiEventManager { } } -@Data -@AllArgsConstructor -class MiraiEventConsumer{ - private MiraiEventHook hook; - private Predicate remove; - public int getPriority(){ - return hook.getPreferences().getPriority(); - } - - @SuppressWarnings("unchecked") - public boolean accept(MiraiEvent event) { - 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/MiraiHookPreferences.java b/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java deleted file mode 100644 index 11dc939d3..000000000 --- a/mirai-core/src/main/java/net/mamoe/mirai/event/MiraiHookPreferences.java +++ /dev/null @@ -1,9 +0,0 @@ -package net.mamoe.mirai.event; - -import lombok.Data; - -@Data -public class MiraiHookPreferences { - private int priority = 0; - private boolean ignoreCanceled = true; -}