From 5a7570001e6ff4651734c33054d21920a06077b2 Mon Sep 17 00:00:00 2001 From: liujiahua123123 Date: Sat, 17 Aug 2019 15:41:34 +0800 Subject: [PATCH] deprecated onEvent --- .../mamoe/mirai/event/MiraiEventManager.java | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) 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 ce6a04545..cc7df4418 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 @@ -25,49 +25,61 @@ public class MiraiEventManager { Lock hooksLock = new ReentrantLock(); private Map, List>> hooks = new HashMap<>(); - public void registerUntil(MiraiEventHook hook, Predicate toRemove){ + public void hookUntil(MiraiEventHook hook, Predicate toRemove){ hooksLock.lock(); hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>()); hooks.get(hook.getEventClass()).add(hook.setValidUntil(toRemove)); hooksLock.unlock(); } - public void registerWhile(MiraiEventHook hook, Predicate toKeep){ + public void hookWhile(MiraiEventHook hook, Predicate toKeep){ hooksLock.lock(); hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>()); hooks.get(hook.getEventClass()).add(hook.setValidWhile(toKeep)); hooksLock.unlock(); } - public void registerOnce(MiraiEventHook hook){ - this.registerUntil(hook,(a) -> true); + public void hookOnce(MiraiEventHook hook){ + this.hookUntil(hook,(a) -> true); } - public void register(MiraiEventHook hook){ - this.registerUntil(hook,(a) -> false); + public void registerHook(MiraiEventHook hook){ + this.hookUntil(hook,(a) -> false); } + + /** + * 不推荐onEvent + * 非线程安全 + * 不能保证下一个 D event发生时handler就位 + * @author NaturalHG Aug27 + */ + + @Deprecated public MiraiEventHook onEvent(Class event){ MiraiEventHook hook = new MiraiEventHook<>(event); - this.register(hook); + this.registerHook(hook); return hook; } + @Deprecated public MiraiEventHook onEventOnce(Class event){ MiraiEventHook hook = new MiraiEventHook<>(event); - this.registerOnce(hook); + this.hookOnce(hook); return hook; } + @Deprecated public MiraiEventHook onEventUntil(Class event, Predicate toRemove){ MiraiEventHook hook = new MiraiEventHook<>(event); - this.registerUntil(hook,toRemove); + this.hookUntil(hook,toRemove); return hook; } + @Deprecated public MiraiEventHook onEventWhile(Class event, Predicate toKeep){ MiraiEventHook hook = new MiraiEventHook<>(event); - this.registerWhile(hook,toKeep); + this.hookWhile(hook,toKeep); return hook; }