SteamWar/BauSystem2.0
Archiviert
12
0

Optimize listener injection
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-08-27 20:56:19 +02:00
Ursprung ee1a82b5d3
Commit 3e38e27b73
2 geänderte Dateien mit 56 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -26,7 +26,6 @@ import javax.lang.model.SourceVersion;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
import javax.tools.Diagnostic; import javax.tools.Diagnostic;
import javax.tools.JavaFileObject;
import java.io.Writer; import java.io.Writer;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -111,6 +110,49 @@ public class LinkageProcessor extends AbstractProcessor {
fields.add(typeElement.getQualifiedName().toString() + " " + typeElement.getSimpleName().toString()); fields.add(typeElement.getQualifiedName().toString() + " " + typeElement.getSimpleName().toString());
}); });
Map<String, TypeElement> eventClasses = new HashMap<>();
Map<TypeElement, Map<TypeElement, ExecutableElement>> eventMethods = new HashMap<>();
for (Element element : elements) {
if (element.getKind() != ElementKind.CLASS) {
continue;
}
TypeElement typeElement = (TypeElement) element;
typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD).map(ExecutableElement.class::cast).filter(e -> {
return e.getAnnotationMirrors().stream().anyMatch(annotationMirror -> {
return annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler");
});
}).forEach(e -> {
TypeElement current = ((TypeElement)((DeclaredType) e.getParameters().get(0).asType()).asElement());
eventClasses.put(current.getQualifiedName().toString(), current);
eventMethods.computeIfAbsent(typeElement, k -> new HashMap<>()).put(current, e);
});
}
if (!eventMethods.isEmpty()) {
List<String> eventLines = new ArrayList<>();
linkLines.put(LinkageType.LISTENER, eventLines);
eventMethods.forEach((typeElement, map) -> {
String instance = "local" + typeElement.getSimpleName().toString();
eventLines.add(typeElement.getQualifiedName().toString() + " " + instance + " = " + getElement(typeElement, neededFields));
map.forEach((typeElement1, executableElement) -> {
AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null);
if (eventHandler == null) {
return;
}
String priority = "NORMAL";
String ignoreCancelled = "false";
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : eventHandler.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("priority")) {
priority = entry.getValue().getValue().toString();
} else if (entry.getKey().getSimpleName().toString().equals("ignoreCancelled")) {
ignoreCancelled = entry.getValue().getValue().toString();
}
}
eventLines.add(typeElement1.getSimpleName().toString() + "(" + instance + ", " + instance + "::" + executableElement.getSimpleName().toString() + ", org.bukkit.event.EventPriority." + priority + ", " + ignoreCancelled + ")");
});
});
}
for (Element element : elements) { for (Element element : elements) {
if (element.getKind() != ElementKind.CLASS) { if (element.getKind() != ElementKind.CLASS) {
continue; continue;
@ -184,6 +226,18 @@ public class LinkageProcessor extends AbstractProcessor {
writer.write(" }\n"); writer.write(" }\n");
writer.write("\n"); writer.write("\n");
} }
for (Map.Entry<String, TypeElement> entry : eventClasses.entrySet()) {
writer.write(" private static org.bukkit.event.HandlerList handlerList" + entry.getValue().getSimpleName().toString() + " = " + entry.getKey() + ".getHandlerList();\n");
writer.write(" private static void " + entry.getValue().getSimpleName().toString() + "(org.bukkit.event.Listener listener, java.util.function.Consumer<" + entry.getKey() + "> consumer, org.bukkit.event.EventPriority eventPriority, boolean ignoreCancelled) {\n");
writer.write(" org.bukkit.plugin.EventExecutor eventExecutor = (l, event) -> {\n");
writer.write(" if (event instanceof " + entry.getKey() + ") {\n");
writer.write(" consumer.accept((" + entry.getKey() + ") event);\n");
writer.write(" }\n");
writer.write(" };\n");
writer.write(" handlerList" + entry.getValue().getSimpleName() + ".register(new org.bukkit.plugin.RegisteredListener(listener, eventExecutor, eventPriority, de.steamwar.bausystem.BauSystem.getInstance(), ignoreCancelled));\n");
writer.write(" }\n");
writer.write("\n");
}
writer.write("}\n"); writer.write("}\n");
writer.flush(); writer.flush();
writer.close(); writer.close();

Datei anzeigen

@ -29,7 +29,7 @@ public enum LinkageType {
ENABLE_LINK("$.enable()", null, "de.steamwar.bausystem.linkage.Enable"), ENABLE_LINK("$.enable()", null, "de.steamwar.bausystem.linkage.Enable"),
DISABLE_LINK("$.disable()", null, "de.steamwar.bausystem.linkage.Disable"), DISABLE_LINK("$.disable()", null, "de.steamwar.bausystem.linkage.Disable"),
PLAIN(), PLAIN(),
LISTENER("org.bukkit.Bukkit.getPluginManager().registerEvents($, de.steamwar.bausystem.BauSystem.getInstance())", null, "org.bukkit.event.Listener"), LISTENER(), // Is handled internally from LinkageProcessor
UNLINK_LISTENER("org.bukkit.event.HandlerList.unregisterAll($)", null, "org.bukkit.event.Listener"), UNLINK_LISTENER("org.bukkit.event.HandlerList.unregisterAll($)", null, "org.bukkit.event.Listener"),
// SPECIFIC // SPECIFIC