fix event

This commit is contained in:
liujiahua123123 2019-08-09 14:41:50 +08:00
parent 6eaa004bee
commit 35f3d0ca70
6 changed files with 19 additions and 31 deletions

View File

@ -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);
}
}

View File

@ -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();
}
*/
}

View File

@ -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

View File

@ -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())
);
}

View File

@ -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
);

View File

@ -27,7 +27,7 @@ import java.util.Random;
* </code>
* </pre>
*
* @author https://juejin.im/post/5c5bdf8cf265da2dc706d532
* @author b
* @author luma
* @author notXX
*/