Fix registering target for listeners and for command messages
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
yoyosource 2022-09-22 13:39:58 +02:00
Ursprung f628bb67dd
Commit a2c2b3ecf1
4 geänderte Dateien mit 52 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -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<String> 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<String> 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<? extends LinkageType>...", "types"));
runsMethod.addLine("for (Class<? extends LinkageType> type : types) _run(type);");
runsMethod.addLine("for (Class<? extends LinkageType> 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<? extends LinkageType>", "type"));

Datei anzeigen

@ -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<String> getPluginMain() {
return LinkageProcessor.getPluginMain();
}
enum Context {
BUNGEE,

Datei anzeigen

@ -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<String> 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);");
}
}

Datei anzeigen

@ -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();");
});