Merge remote-tracking branch 'origin/master'

This commit is contained in:
Him188moe 2019-08-08 22:53:46 +08:00
commit a907267965
4 changed files with 115 additions and 5 deletions

View File

@ -1,7 +1,13 @@
package net.mamoe.mirai.event;
import lombok.Getter;
interface Cancelable {
void setCancel(boolean value);
boolean isCancelled();
void setCancelled();
void setCancelled(boolean forceCancel);
}

View File

@ -1,5 +1,28 @@
package net.mamoe.mirai.event;
import net.mamoe.jpre.event.Cancellable;
import net.mamoe.mirai.utils.EventException;
public abstract class MiraiEvent {
private boolean cancelled;
public boolean isCancelled() {
if (!(this instanceof Cancellable)) {
throw new EventException("Event is not Cancellable");
}
return this.cancelled;
}
public void setCancelled() {
setCancelled(true);
}
public void setCancelled(boolean value) {
if (!(this instanceof Cancellable)) {
throw new EventException("Event is not Cancellable");
}
this.cancelled = value;
}
}

View File

@ -3,12 +3,12 @@ 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.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public class MiraiEventManager {
private MiraiEventManager(){
@ -24,6 +24,7 @@ public class MiraiEventManager {
return MiraiEventManager.instance;
}
Lock hooksLock = new ReentrantLock();
private Map<Class<? extends MiraiEvent>, List<MiraiEventConsumer<? extends MiraiEvent>>> hooks = new HashMap<>();
public <D extends MiraiEvent> void registerUntil(MiraiEventHook<D> hook, Predicate<D> toRemove){
@ -39,11 +40,38 @@ public class MiraiEventManager {
this.registerUntil(hook,(a) -> false);
}
public void boardcastEvent(MiraiEvent event){
hooksLock.lock();
if(hooks.containsKey(event.getClass())){
hooks.put(event.getClass(),
hooks.get(event.getClass())
.stream()
.sorted(Comparator.comparingInt(MiraiEventConsumer::getPriority))
.dropWhile(a -> a.accept(event))
.collect(Collectors.toList())
);
}
hooksLock.unlock();
}
}
@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 Cancelable && event.isCancelled() && hook.getPreferences().isIgnoreCanceled())){
hook.getHandler().accept((T) event);
}
return remove.test((T)event);
}
}

View File

@ -0,0 +1,53 @@
package net.mamoe.mirai.utils;
public class EventException extends RuntimeException {
private final Throwable cause;
/**
* Constructs a new EventException based on the given Exception
*
* @param throwable Exception that triggered this Exception
*/
public EventException(Throwable throwable) {
cause = throwable;
}
/**
* Constructs a new EventException
*/
public EventException() {
cause = null;
}
/**
* Constructs a new EventException with the given message
*
* @param cause The exception that caused this
* @param message The message
*/
public EventException(Throwable cause, String message) {
super(message);
this.cause = cause;
}
/**
* Constructs a new EventException with the given message
*
* @param message The message
*/
public EventException(String message) {
super(message);
cause = null;
}
/**
* If applicable, returns the Exception that triggered this Exception
*
* @return Inner exception, or null if one does not exist
*/
@Override
public Throwable getCause() {
return cause;
}
}