added handling for cancelable events

This commit is contained in:
zaaarf 2023-08-25 03:23:54 +02:00
parent 5add2ddd82
commit f5cf21050f
No known key found for this signature in database
GPG key ID: 6445A5CD15E5B40C

View file

@ -2,6 +2,7 @@ package ftbsc.geb.processor;
import com.squareup.javapoet.*; import com.squareup.javapoet.*;
import ftbsc.geb.api.IEvent; import ftbsc.geb.api.IEvent;
import ftbsc.geb.api.IEventCancelable;
import ftbsc.geb.api.IEventDispatcher; import ftbsc.geb.api.IEventDispatcher;
import ftbsc.geb.api.IListener; import ftbsc.geb.api.IListener;
import ftbsc.geb.api.annotations.Listen; import ftbsc.geb.api.annotations.Listen;
@ -49,6 +50,11 @@ public class GEBProcessor extends AbstractProcessor {
*/ */
private TypeMirror eventInterface; private TypeMirror eventInterface;
/**
* A {@link TypeMirror} representing the {@link IEventCancelable} interface.
*/
private TypeMirror cancelableEventInterface;
/** /**
* A {@link TypeMirror} representing the {@link IEventDispatcher} interface. * A {@link TypeMirror} representing the {@link IEventDispatcher} interface.
*/ */
@ -62,9 +68,14 @@ public class GEBProcessor extends AbstractProcessor {
@Override @Override
public synchronized void init(ProcessingEnvironment env) { public synchronized void init(ProcessingEnvironment env) {
super.init(env); super.init(env);
listenerInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IListener").asType(); this.listenerInterface = env.getElementUtils()
eventInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IEvent").asType(); .getTypeElement("ftbsc.geb.api.IListener").asType();
dispatcherInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IEventDispatcher").asType(); this.eventInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEvent").asType();
this.dispatcherInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEventDispatcher").asType();
this.cancelableEventInterface = env.getElementUtils()
.getTypeElement("ftbsc.geb.api.IEventCancelable").asType();
} }
/** /**
@ -154,6 +165,7 @@ public class GEBProcessor extends AbstractProcessor {
private void generateClasses() { private void generateClasses() {
this.listenerMap.forEach((event, listeners) -> { this.listenerMap.forEach((event, listeners) -> {
TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event); TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event);
boolean cancelable = this.processingEnv.getTypeUtils().isAssignable(event, this.cancelableEventInterface);
//reorder the injectors to follow priority //reorder the injectors to follow priority
List<ListenerContainer> ordered = listeners.stream().sorted(Comparator.comparingInt( List<ListenerContainer> ordered = listeners.stream().sorted(Comparator.comparingInt(
@ -184,8 +196,12 @@ public class GEBProcessor extends AbstractProcessor {
.addStatement("$T $L = $N.get($T.class)", this.listenerInterface, varName, listenersParam, listener.parent) .addStatement("$T $L = $N.get($T.class)", this.listenerInterface, varName, listenersParam, listener.parent)
.addStatement("if($L.isActive()) (($T) $L).$L(($T) $N)", varName, listener.parent, varName, .addStatement("if($L.isActive()) (($T) $L).$L(($T) $N)", varName, listener.parent, varName,
listener.method.getSimpleName().toString(), event, eventParam); listener.method.getSimpleName().toString(), event, eventParam);
if(cancelable) callListenersBuilder
.addStatement("if((($T) $N).isCanceled()) return true", this.cancelableEventInterface, eventParam);
} }
callListenersBuilder.addStatement("return false");
MethodSpec eventType = MethodSpec.methodBuilder("eventType") MethodSpec eventType = MethodSpec.methodBuilder("eventType")
.addModifiers(Modifier.PUBLIC) .addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class) .addAnnotation(Override.class)
@ -206,7 +222,7 @@ public class GEBProcessor extends AbstractProcessor {
String resultingClassName = String.format("%s.%s", packageName, clazzName); String resultingClassName = String.format("%s.%s", packageName, clazzName);
try { try {
JavaFileObject injectorFile = processingEnv.getFiler().createSourceFile(resultingClassName); JavaFileObject injectorFile = this.processingEnv.getFiler().createSourceFile(resultingClassName);
PrintWriter out = new PrintWriter(injectorFile.openWriter()); PrintWriter out = new PrintWriter(injectorFile.openWriter());
javaFile.writeTo(out); javaFile.writeTo(out);
out.close(); out.close();