mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-31 11:30:16 +08:00
Event Hook
This commit is contained in:
parent
6e8d5ae9fd
commit
bd7e08b483
@ -14,10 +14,32 @@ public final class MiraiMain {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
server.shutdown();
|
||||
}));
|
||||
server.getEventManager().register(
|
||||
new MiraiEventHook<>(ServerDisableEvent.class)
|
||||
.setHandler(a -> System.out.println("close"))
|
||||
);
|
||||
server.getEventManager()
|
||||
.onEvent(ServerDisableEvent.class)
|
||||
.setHandler(a -> {
|
||||
System.out.println("?");
|
||||
});
|
||||
|
||||
server.getEventManager()
|
||||
.onEventOnce(ServerDisableEvent.class)
|
||||
.setHandler(a -> {
|
||||
System.out.println("?");
|
||||
});
|
||||
|
||||
server.getEventManager()
|
||||
.onEvent(ServerDisableEvent.class)
|
||||
.setHandler(a -> {
|
||||
System.out.println("?");
|
||||
})
|
||||
.setValidWhile((a) -> true);
|
||||
|
||||
|
||||
server.getEventManager()
|
||||
.onEvent(ServerDisableEvent.class)
|
||||
.setHandler(a -> {
|
||||
System.out.println("?");
|
||||
})
|
||||
.setValidUntil((a) -> true);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -21,6 +21,9 @@ public class MiraiEventHook<T extends MiraiEvent> {
|
||||
@Getter
|
||||
private boolean ignoreCancelled = true;
|
||||
|
||||
/**
|
||||
* return true -> this hook need to be removed
|
||||
*/
|
||||
@Getter
|
||||
private Predicate<T> valid;
|
||||
|
||||
@ -49,11 +52,20 @@ public class MiraiEventHook<T extends MiraiEvent> {
|
||||
}
|
||||
|
||||
|
||||
public MiraiEventHook<T> setValid(Predicate<T> valid) {
|
||||
private MiraiEventHook<T> setValid(Predicate<T> valid) {
|
||||
this.valid = valid;
|
||||
return this;
|
||||
}
|
||||
|
||||
public MiraiEventHook<T> setValidUntil(Predicate<T> valid) {
|
||||
return this.setValid(valid);
|
||||
}
|
||||
|
||||
public MiraiEventHook<T> setValidWhile(Predicate<T> valid) {
|
||||
return this.setValid(valid.negate());
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public boolean accept(MiraiEvent event) {
|
||||
if(!(event instanceof Cancellable && event.isCancelled() && this.isIgnoreCancelled())){
|
||||
|
@ -30,7 +30,12 @@ public class MiraiEventManager {
|
||||
|
||||
public <D extends MiraiEvent> void registerUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
|
||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
||||
hooks.get(hook.getEventClass()).add(hook.setValid(toRemove));
|
||||
hooks.get(hook.getEventClass()).add(hook.setValidUntil(toRemove));
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> void registerWhile(MiraiEventHook<D> hook, Predicate<D> toKeep){
|
||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
||||
hooks.get(hook.getEventClass()).add(hook.setValidWhile(toKeep));
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> void registerOnce(MiraiEventHook<D> hook){
|
||||
@ -47,6 +52,26 @@ public class MiraiEventManager {
|
||||
return hook;
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> MiraiEventHook<D> onEventOnce(Class<D> event){
|
||||
MiraiEventHook<D> hook = new MiraiEventHook<>(event);
|
||||
this.registerOnce(hook);
|
||||
return hook;
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> MiraiEventHook<D> onEventUntil(Class<D> event, Predicate<D> toRemove){
|
||||
MiraiEventHook<D> hook = new MiraiEventHook<>(event);
|
||||
this.registerUntil(hook,toRemove);
|
||||
return hook;
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> MiraiEventHook<D> onEventWhile(Class<D> event, Predicate<D> toKeep){
|
||||
MiraiEventHook<D> hook = new MiraiEventHook<>(event);
|
||||
this.registerWhile(hook,toKeep);
|
||||
return hook;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void boardcastEvent(MiraiEvent event){
|
||||
hooksLock.lock();
|
||||
if(hooks.containsKey(event.getClass())){
|
||||
|
Loading…
Reference in New Issue
Block a user