From 3e38e27b7366b13d5d053e19c48fcefdc56307a6 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 27 Aug 2022 20:56:19 +0200 Subject: [PATCH] Optimize listener injection Signed-off-by: yoyosource --- .../bausystem/linkage/LinkageProcessor.java | 56 ++++++++++++++++++- .../bausystem/linkage/LinkageType.java | 2 +- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java index f4237060..e799c597 100644 --- a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java +++ b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java @@ -26,7 +26,6 @@ import javax.lang.model.SourceVersion; import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; import javax.tools.Diagnostic; -import javax.tools.JavaFileObject; import java.io.Writer; import java.util.*; import java.util.stream.Collectors; @@ -111,6 +110,49 @@ public class LinkageProcessor extends AbstractProcessor { fields.add(typeElement.getQualifiedName().toString() + " " + typeElement.getSimpleName().toString()); }); + Map eventClasses = new HashMap<>(); + Map> 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 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 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) { if (element.getKind() != ElementKind.CLASS) { continue; @@ -184,6 +226,18 @@ public class LinkageProcessor extends AbstractProcessor { writer.write(" }\n"); writer.write("\n"); } + for (Map.Entry 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.flush(); writer.close(); diff --git a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java index d1e08b36..5e7f3c58 100644 --- a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java +++ b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -29,7 +29,7 @@ public enum LinkageType { ENABLE_LINK("$.enable()", null, "de.steamwar.bausystem.linkage.Enable"), DISABLE_LINK("$.disable()", null, "de.steamwar.bausystem.linkage.Disable"), 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"), // SPECIFIC