From a2c2b3ecf1e646bac3706df97a315cd569c14e74 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 22 Sep 2022 13:39:58 +0200 Subject: [PATCH] Fix registering target for listeners and for command messages --- src/de/steamwar/linkage/LinkageProcessor.java | 40 +++++++++++++++++-- src/de/steamwar/linkage/LinkageType.java | 4 ++ src/de/steamwar/linkage/types/Command.java | 11 +++++ .../steamwar/linkage/types/ListenerLink.java | 3 +- 4 files changed, 52 insertions(+), 6 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 343ee56..25eb4bd 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -35,8 +35,7 @@ import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; -import java.io.BufferedWriter; -import java.io.File; +import java.io.*; import java.io.Writer; import java.util.*; import java.util.stream.Collectors; @@ -47,6 +46,9 @@ public class LinkageProcessor extends AbstractProcessor { @Getter private static LinkageType.Context context; + @Getter + private static String pluginMain; + private String name; private String packageName; private String className; @@ -81,6 +83,36 @@ public class LinkageProcessor extends AbstractProcessor { this.name = name; packageName = "de.steamwar." + name + ".linkage"; className = "LinkageUtils"; + mainClass(); + } + + private void mainClass() { + File file = new File(System.getProperty("user.dir")); + String subModuleName = file.getName(); + if (subModuleName.contains(".")) subModuleName = subModuleName.substring(0, subModuleName.indexOf('.')); + + Optional main = getMainName(new File(file, "src/plugin.yml")); + if (!main.isPresent()) main = getMainName(new File(file, "src/main/resources/plugin.yml")); + if (!main.isPresent()) main = getMainName(new File(file, subModuleName + "_Core/src/plugin.yml")); + if (!main.isPresent()) main = getMainName(new File(file, subModuleName + "_Core/src/main/resources/plugin.yml")); + if (!main.isPresent()) main = getMainName(new File(file, subModuleName + "_Main/src/plugin.yml")); + if (!main.isPresent()) main = getMainName(new File(file, subModuleName + "_Main/src/main/resources/plugin.yml")); + if (!main.isPresent()) { + messager.printMessage(Diagnostic.Kind.ERROR, "Could not find plugin.yml"); + return; + } + pluginMain = main.get(); + } + + @SneakyThrows + private Optional getMainName(File pluginYML) { + if (!pluginYML.exists()) return Optional.empty(); + try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(pluginYML)))) { + return reader.lines() + .filter(line -> line.startsWith("main:")) + .map(line -> line.substring(line.indexOf(':') + 1).trim()) + .findFirst(); + } } @SneakyThrows @@ -100,11 +132,11 @@ public class LinkageProcessor extends AbstractProcessor { MethodBuilder runsMethod = new MethodBuilder("run", "void"); runsMethod.addParameter(new ParameterBuilder("Class...", "types")); - runsMethod.addLine("for (Class type : types) _run(type);"); + runsMethod.addLine("for (Class type : types) run(type);"); buildPlan.addMethod(runsMethod); - MethodBuilder runMethod = new MethodBuilder("_run", "void"); + MethodBuilder runMethod = new MethodBuilder("run", "void"); runMethod.setPrivate(true); buildPlan.addMethod(runMethod); runMethod.addParameter(new ParameterBuilder("Class", "type")); diff --git a/src/de/steamwar/linkage/LinkageType.java b/src/de/steamwar/linkage/LinkageType.java index 9308905..8ed8a6e 100644 --- a/src/de/steamwar/linkage/LinkageType.java +++ b/src/de/steamwar/linkage/LinkageType.java @@ -23,12 +23,16 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; +import java.util.Optional; import java.util.OptionalInt; import java.util.Set; public interface LinkageType { Context context = LinkageProcessor.getContext(); + default Optional getPluginMain() { + return LinkageProcessor.getPluginMain(); + } enum Context { BUNGEE, diff --git a/src/de/steamwar/linkage/types/Command.java b/src/de/steamwar/linkage/types/Command.java index fc522da..68c4607 100644 --- a/src/de/steamwar/linkage/types/Command.java +++ b/src/de/steamwar/linkage/types/Command.java @@ -24,10 +24,21 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; +import java.util.Set; public class Command implements LinkageType { + @Override + public boolean requirements(String superClass, Set interfaces) { + return superClass.equals("de.steamwar.command.SWCommand"); + } + @Override public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + if (context == Context.BUNGEE) { + linkageTypeMethod.addLine(instance + ";"); + return; + } + linkageTypeMethod.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);"); } } diff --git a/src/de/steamwar/linkage/types/ListenerLink.java b/src/de/steamwar/linkage/types/ListenerLink.java index 5a1fef2..3eb140a 100644 --- a/src/de/steamwar/linkage/types/ListenerLink.java +++ b/src/de/steamwar/linkage/types/ListenerLink.java @@ -71,7 +71,6 @@ public class ListenerLink implements LinkageType { buildPlan.addImport("org.bukkit.event.EventPriority"); buildPlan.addImport("org.bukkit.plugin.RegisteredListener"); buildPlan.addImport("org.bukkit.plugin.EventExecutor"); - buildPlan.addImport("de.steamwar.core.Core"); buildPlan.addImport(s); buildPlan.addField(new FieldBuilder("HandlerList", "handlerList" + eventType.getSimpleName())); MethodBuilder methodBuilder = new MethodBuilder(eventType.getSimpleName().toString(), "void"); @@ -85,7 +84,7 @@ public class ListenerLink implements LinkageType { methodBuilder.addLine(" consumer.accept((" + eventType.getSimpleName() + ") event);"); methodBuilder.addLine(" }"); methodBuilder.addLine("};"); - methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, Core.getInstance(), ignoreCancelled));"); + methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, " + getPluginMain() + ".getInstance(), ignoreCancelled));"); buildPlan.addMethod(methodBuilder); linkageTypeMethod.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();"); });