This commit is contained in:
liujiahua123123 2019-08-08 22:12:02 +08:00
parent 7e2cde8c79
commit eae076a677
6 changed files with 110 additions and 5 deletions

View File

@ -0,0 +1,7 @@
package net.mamoe.mirai.event;
interface Cancelable {
void setCancel(boolean value);
}

View File

@ -1,5 +1,5 @@
package net.mamoe.mirai.event;
public class MiraiEvent {
public abstract class MiraiEvent {
}

View File

@ -0,0 +1,44 @@
package net.mamoe.mirai.event;
import lombok.Getter;
import java.util.function.Consumer;
public class MiraiEventHook<T extends MiraiEvent> {
@Getter
Class<T> eventClass;
@Getter
private Consumer<T> handler;
@Getter
private MiraiHookPreferences preferences;
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){
this.handler = handler;
return this;
}
public MiraiEventHook<T> setPreferences(MiraiHookPreferences preferences){
this.preferences = preferences;
return this;
}
}

View File

@ -0,0 +1,49 @@
package net.mamoe.mirai.event;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class MiraiEventManager {
private MiraiEventManager(){
}
private static MiraiEventManager instance;
static MiraiEventManager getInstance(){
if(MiraiEventManager.instance == null){
MiraiEventManager.instance = new MiraiEventManager();
}
return MiraiEventManager.instance;
}
private Map<Class<? extends MiraiEvent>, List<MiraiEventConsumer<? 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));
}
public <D extends MiraiEvent> void registerOnce(MiraiEventHook<D> hook){
this.registerUntil(hook,(a) -> true);
}
public <D extends MiraiEvent> void register(MiraiEventHook<D> hook){
this.registerUntil(hook,(a) -> false);
}
}
@Data
@AllArgsConstructor
class MiraiEventConsumer<T extends MiraiEvent>{
private MiraiEventHook<T> hook;
private Predicate<T> remove;
}

View File

@ -1,4 +0,0 @@
package net.mamoe.mirai.event;
public class MiraiHook {
}

View File

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