mirror of
https://github.com/mamoe/mirai.git
synced 2025-01-31 11:30:16 +08:00
fix event
This commit is contained in:
parent
6eaa004bee
commit
35f3d0ca70
@ -14,31 +14,9 @@ public final class MiraiMain {
|
||||
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
||||
server.shutdown();
|
||||
}));
|
||||
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);
|
||||
server.getTaskManager().repeatingTask(() -> {
|
||||
System.out.println(3);
|
||||
},1000,5);
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -43,6 +43,7 @@ public class MiraiServer {
|
||||
this.eventManager = MiraiEventManager.getInstance();
|
||||
this.taskManager = MiraiTaskManager.getInstance();
|
||||
|
||||
/*
|
||||
try {
|
||||
Network.start(Network.getAvailablePort());
|
||||
} catch (InterruptedException | IOException e) {
|
||||
@ -50,6 +51,7 @@ public class MiraiServer {
|
||||
this.shutdown();
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,13 +13,13 @@ public class MiraiEventHook<T extends MiraiEvent> {
|
||||
Class<T> eventClass;
|
||||
|
||||
@Getter
|
||||
private Consumer<T> handler;
|
||||
private volatile Consumer<T> handler;
|
||||
|
||||
@Getter
|
||||
private int priority = 0;
|
||||
private volatile int priority = 0;
|
||||
|
||||
@Getter
|
||||
private boolean ignoreCancelled = true;
|
||||
private volatile boolean ignoreCancelled = true;
|
||||
|
||||
/**
|
||||
* return true -> this hook need to be removed
|
||||
|
@ -29,13 +29,17 @@ public class MiraiEventManager {
|
||||
private Map<Class<? extends MiraiEvent>, List<MiraiEventHook<? extends MiraiEvent>>> hooks = new HashMap<>();
|
||||
|
||||
public <D extends MiraiEvent> void registerUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
|
||||
hooksLock.lock();
|
||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
||||
hooks.get(hook.getEventClass()).add(hook.setValidUntil(toRemove));
|
||||
hooksLock.unlock();
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> void registerWhile(MiraiEventHook<D> hook, Predicate<D> toKeep){
|
||||
hooksLock.lock();
|
||||
hooks.putIfAbsent(hook.getEventClass(),new ArrayList<>());
|
||||
hooks.get(hook.getEventClass()).add(hook.setValidWhile(toKeep));
|
||||
hooksLock.unlock();
|
||||
}
|
||||
|
||||
public <D extends MiraiEvent> void registerOnce(MiraiEventHook<D> hook){
|
||||
@ -74,12 +78,14 @@ public class MiraiEventManager {
|
||||
|
||||
public void boardcastEvent(MiraiEvent event){
|
||||
hooksLock.lock();
|
||||
System.out.println(hooks);
|
||||
if(hooks.containsKey(event.getClass())){
|
||||
System.out.println(hooks.get(event.getClass()).size());
|
||||
hooks.put(event.getClass(),
|
||||
hooks.get(event.getClass())
|
||||
.stream()
|
||||
.sorted(Comparator.comparingInt(MiraiEventHook::getPriority))
|
||||
.dropWhile(a -> a.accept(event))
|
||||
.filter(a -> !a.accept(event))
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
}
|
||||
|
@ -25,9 +25,11 @@ public class MiraiTaskManager {
|
||||
|
||||
private MiraiTaskManager() {
|
||||
this.pool = new MiraiThreadPool();
|
||||
|
||||
MiraiServer.getInstance().getEventManager()
|
||||
.onEvent(ServerDisableEvent.class)
|
||||
.setHandler(a -> this.pool.close());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@ -99,7 +101,7 @@ public class MiraiTaskManager {
|
||||
}
|
||||
|
||||
public void repeatingTask(Runnable runnable, long interval, int times, MiraiTaskExceptionHandler handler){
|
||||
AtomicInteger integer = new AtomicInteger(times);
|
||||
AtomicInteger integer = new AtomicInteger(times-1);
|
||||
this.repeatingTask(
|
||||
runnable,interval, a -> integer.getAndDecrement() > 0, handler
|
||||
);
|
||||
|
@ -27,7 +27,7 @@ import java.util.Random;
|
||||
* </code>
|
||||
* </pre>
|
||||
*
|
||||
* @author https://juejin.im/post/5c5bdf8cf265da2dc706d532
|
||||
* @author b
|
||||
* @author luma
|
||||
* @author notXX
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user