mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-05 23:50:08 +08:00
thread safe event system
This commit is contained in:
parent
5a7570001e
commit
69b97a3b24
@ -1,8 +1,10 @@
|
|||||||
package net.mamoe.mirai.event;
|
package net.mamoe.mirai.event;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import net.mamoe.mirai.event.events.Cancellable;
|
import net.mamoe.mirai.event.events.Cancellable;
|
||||||
import net.mamoe.mirai.event.events.MiraiEvent;
|
import net.mamoe.mirai.event.events.MiraiEvent;
|
||||||
|
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
|
||||||
|
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
@ -21,6 +23,10 @@ public class MiraiEventHook<T extends MiraiEvent> {
|
|||||||
@Getter
|
@Getter
|
||||||
private volatile boolean ignoreCancelled = true;
|
private volatile boolean ignoreCancelled = true;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private volatile boolean mount = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* return true -> this hook need to be removed
|
* return true -> this hook need to be removed
|
||||||
*/
|
*/
|
||||||
@ -74,4 +80,23 @@ public class MiraiEventHook<T extends MiraiEvent> {
|
|||||||
return this.valid.test((T)event);
|
return this.valid.test((T)event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 更加安全高效的方式
|
||||||
|
* Remember to use {@link this.mount()} at last
|
||||||
|
* */
|
||||||
|
|
||||||
|
public static <D extends MiraiEvent> MiraiEventHook<D> onEvent(Class<D> event){
|
||||||
|
return new MiraiEventHook<>(event);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mount(){
|
||||||
|
MiraiEventManager.getInstance().registerHook(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void mountOnce(){
|
||||||
|
MiraiEventManager.getInstance().hookOnce(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -26,17 +26,11 @@ public class MiraiEventManager {
|
|||||||
private Map<Class<? extends MiraiEvent>, List<MiraiEventHook<? extends MiraiEvent>>> hooks = new HashMap<>();
|
private Map<Class<? extends MiraiEvent>, List<MiraiEventHook<? extends MiraiEvent>>> hooks = new HashMap<>();
|
||||||
|
|
||||||
public <D extends MiraiEvent> void hookUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
|
public <D extends MiraiEvent> void hookUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
|
||||||
hooksLock.lock();
|
this.mountHook(hook.setValidUntil(toRemove));
|
||||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
|
||||||
hooks.get(hook.getEventClass()).add(hook.setValidUntil(toRemove));
|
|
||||||
hooksLock.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <D extends MiraiEvent> void hookWhile(MiraiEventHook<D> hook, Predicate<D> toKeep){
|
public <D extends MiraiEvent> void hookWhile(MiraiEventHook<D> hook, Predicate<D> toKeep){
|
||||||
hooksLock.lock();
|
this.mountHook(hook.setValidWhile(toKeep));
|
||||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
|
||||||
hooks.get(hook.getEventClass()).add(hook.setValidWhile(toKeep));
|
|
||||||
hooksLock.unlock();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public <D extends MiraiEvent> void hookOnce(MiraiEventHook<D> hook){
|
public <D extends MiraiEvent> void hookOnce(MiraiEventHook<D> hook){
|
||||||
@ -44,15 +38,25 @@ public class MiraiEventManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <D extends MiraiEvent> void registerHook(MiraiEventHook<D> hook){
|
public <D extends MiraiEvent> void registerHook(MiraiEventHook<D> hook){
|
||||||
this.hookUntil(hook,(a) -> false);
|
this.mountHook(hook);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <D extends MiraiEvent> void mountHook(MiraiEventHook<D> hook){
|
||||||
|
if(!hook.isMount()) {
|
||||||
|
hook.setMount(true);
|
||||||
|
hooksLock.lock();
|
||||||
|
hooks.putIfAbsent(hook.getEventClass(), new ArrayList<>());
|
||||||
|
hooks.get(hook.getEventClass()).add(hook);
|
||||||
|
hooksLock.unlock();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 不推荐onEvent
|
* 不推荐onEvent
|
||||||
* 非线程安全
|
* 由于不能保证Hook的原子性 非线程安全
|
||||||
* 不能保证下一个 D event发生时handler就位
|
* 不能保证下一个 D event发生时handler就位
|
||||||
* @author NaturalHG Aug27
|
* @author NaturalHG Aug27
|
||||||
|
* use {@link MiraiEventHook::onEvent()} to replace
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Deprecated
|
@Deprecated
|
||||||
@ -85,6 +89,7 @@ public class MiraiEventManager {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void boardcastEvent(MiraiEvent event){
|
public void boardcastEvent(MiraiEvent event){
|
||||||
hooksLock.lock();
|
hooksLock.lock();
|
||||||
if(hooks.containsKey(event.getClass())){
|
if(hooks.containsKey(event.getClass())){
|
||||||
|
@ -2,6 +2,7 @@ package net.mamoe.mirai.task;
|
|||||||
|
|
||||||
|
|
||||||
import net.mamoe.mirai.MiraiServer;
|
import net.mamoe.mirai.MiraiServer;
|
||||||
|
import net.mamoe.mirai.event.MiraiEventHook;
|
||||||
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
|
import net.mamoe.mirai.event.events.server.ServerDisableEvent;
|
||||||
|
|
||||||
import java.util.concurrent.Callable;
|
import java.util.concurrent.Callable;
|
||||||
@ -26,9 +27,10 @@ public class MiraiTaskManager {
|
|||||||
private MiraiTaskManager() {
|
private MiraiTaskManager() {
|
||||||
this.pool = new MiraiThreadPool();
|
this.pool = new MiraiThreadPool();
|
||||||
|
|
||||||
MiraiServer.getInstance().getEventManager()
|
MiraiEventHook
|
||||||
.onEvent(ServerDisableEvent.class)
|
.onEvent(ServerDisableEvent.class)
|
||||||
.setHandler(a -> this.pool.close());
|
.setHandler(a -> this.pool.close())
|
||||||
|
.mount();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user