event hook

This commit is contained in:
liujiahua123123 2019-08-08 23:16:45 +08:00
parent 73cbb5b2d1
commit 6e8d5ae9fd
4 changed files with 34 additions and 39 deletions

View File

@ -18,5 +18,6 @@ public final class MiraiMain {
new MiraiEventHook<>(ServerDisableEvent.class)
.setHandler(a -> System.out.println("close"))
);
}
}

View File

@ -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<T extends MiraiEvent> {
@ -12,22 +14,23 @@ public class MiraiEventHook<T extends MiraiEvent> {
@Getter
private Consumer<T> handler;
@Getter
private MiraiHookPreferences preferences;
@Getter
private int priority = 0;
@Getter
private boolean ignoreCancelled = true;
@Getter
private Predicate<T> valid;
public MiraiEventHook(Class<T> eventClass) {
this(eventClass,a -> {});
}
public MiraiEventHook(Class<T> eventClass, Consumer<T> handler){
this(eventClass,handler,new MiraiHookPreferences());
}
public MiraiEventHook(Class<T> eventClass, Consumer<T> handler, MiraiHookPreferences preferences){
this.eventClass = eventClass;
this.setHandler(handler);
this.setPreferences(preferences);
}
public MiraiEventHook<T> setHandler(Consumer<T> handler){
@ -35,11 +38,29 @@ public class MiraiEventHook<T extends MiraiEvent> {
return this;
}
public MiraiEventHook<T> setPreferences(MiraiHookPreferences preferences){
this.preferences = preferences;
public MiraiEventHook<T> setPriority(int priority){
this.priority = priority;
return this;
}
public MiraiEventHook<T> setIgnoreCancelled(boolean ignoreCancelled){
this.ignoreCancelled = ignoreCancelled;
return this;
}
public MiraiEventHook<T> setValid(Predicate<T> 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);
}
}

View File

@ -26,11 +26,11 @@ public class MiraiEventManager {
}
Lock hooksLock = new ReentrantLock();
private Map<Class<? extends MiraiEvent>, List<MiraiEventConsumer<? extends MiraiEvent>>> hooks = new HashMap<>();
private Map<Class<? extends MiraiEvent>, List<MiraiEventHook<? extends MiraiEvent>>> hooks = new HashMap<>();
public <D extends MiraiEvent> void registerUntil(MiraiEventHook<D> hook, Predicate<D> 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 <D extends MiraiEvent> void registerOnce(MiraiEventHook<D> 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<T extends MiraiEvent>{
private MiraiEventHook<T> hook;
private Predicate<T> 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);
}
}

View File

@ -1,9 +0,0 @@
package net.mamoe.mirai.event;
import lombok.Data;
@Data
public class MiraiHookPreferences {
private int priority = 0;
private boolean ignoreCanceled = true;
}