mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-01 09:50:36 +08:00
events
This commit is contained in:
parent
7e2cde8c79
commit
eae076a677
@ -0,0 +1,7 @@
|
||||
package net.mamoe.mirai.event;
|
||||
|
||||
interface Cancelable {
|
||||
|
||||
void setCancel(boolean value);
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
package net.mamoe.mirai.event;
|
||||
|
||||
public class MiraiEvent {
|
||||
public abstract class MiraiEvent {
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -1,4 +0,0 @@
|
||||
package net.mamoe.mirai.event;
|
||||
|
||||
public class MiraiHook {
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package net.mamoe.mirai.event;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class MiraiHookPreferences {
|
||||
private int priority = 0;
|
||||
private boolean ignoreCanceled = true;
|
||||
}
|
Loading…
Reference in New Issue
Block a user