summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author zaaarf <zaaarf@proton.me>2023-08-25 03:23:54 +0200
committer zaaarf <zaaarf@proton.me>2023-08-25 03:23:54 +0200
commitf5cf21050f7496ef4fad624850c75ae88479af83 (patch)
treed6183fcb6d9d33d8e5e909af1aa636f28d83c5d7 /src
parent5add2ddd82b959cea8c3cb25901cebcfd9836e3c (diff)
added handling for cancelable events
Diffstat (limited to 'src')
-rw-r--r--src/main/java/ftbsc/geb/processor/GEBProcessor.java24
1 files changed, 20 insertions, 4 deletions
diff --git a/src/main/java/ftbsc/geb/processor/GEBProcessor.java b/src/main/java/ftbsc/geb/processor/GEBProcessor.java
index 98bf251..f05bef4 100644
--- a/src/main/java/ftbsc/geb/processor/GEBProcessor.java
+++ b/src/main/java/ftbsc/geb/processor/GEBProcessor.java
@@ -2,6 +2,7 @@ package ftbsc.geb.processor;
import com.squareup.javapoet.*;
import ftbsc.geb.api.IEvent;
+import ftbsc.geb.api.IEventCancelable;
import ftbsc.geb.api.IEventDispatcher;
import ftbsc.geb.api.IListener;
import ftbsc.geb.api.annotations.Listen;
@@ -50,6 +51,11 @@ public class GEBProcessor extends AbstractProcessor {
private TypeMirror eventInterface;
/**
+ * A {@link TypeMirror} representing the {@link IEventCancelable} interface.
+ */
+ private TypeMirror cancelableEventInterface;
+
+ /**
* A {@link TypeMirror} representing the {@link IEventDispatcher} interface.
*/
private TypeMirror dispatcherInterface;
@@ -62,9 +68,14 @@ public class GEBProcessor extends AbstractProcessor {
@Override
public synchronized void init(ProcessingEnvironment env) {
super.init(env);
- listenerInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IListener").asType();
- eventInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IEvent").asType();
- dispatcherInterface = env.getElementUtils().getTypeElement("ftbsc.geb.api.IEventDispatcher").asType();
+ this.listenerInterface = env.getElementUtils()
+ .getTypeElement("ftbsc.geb.api.IListener").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() {
this.listenerMap.forEach((event, listeners) -> {
TypeElement eventClass = (TypeElement) this.processingEnv.getTypeUtils().asElement(event);
+ boolean cancelable = this.processingEnv.getTypeUtils().isAssignable(event, this.cancelableEventInterface);
//reorder the injectors to follow priority
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("if($L.isActive()) (($T) $L).$L(($T) $N)", varName, listener.parent, varName,
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")
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Override.class)
@@ -206,7 +222,7 @@ public class GEBProcessor extends AbstractProcessor {
String resultingClassName = String.format("%s.%s", packageName, clazzName);
try {
- JavaFileObject injectorFile = processingEnv.getFiler().createSourceFile(resultingClassName);
+ JavaFileObject injectorFile = this.processingEnv.getFiler().createSourceFile(resultingClassName);
PrintWriter out = new PrintWriter(injectorFile.openWriter());
javaFile.writeTo(out);
out.close();