From 75ff0c3d45263901e7947388c0e1eb4e7c795145 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 24 Sep 2022 12:53:18 +0200 Subject: [PATCH 1/4] Update some stuff --- src/de/steamwar/linkage/AllowedContexts.java | 2 +- .../{DiscordMode.java => Context.java} | 12 +- src/de/steamwar/linkage/EventMode.java | 2 +- src/de/steamwar/linkage/LinkageProcessor.java | 156 ++++++------------ src/de/steamwar/linkage/LinkageType.java | 26 +-- src/de/steamwar/linkage/LinkageTypeOld.java | 52 ++++++ src/de/steamwar/linkage/Linked.java | 8 - src/de/steamwar/linkage/MaxVersion.java | 2 +- src/de/steamwar/linkage/MinVersion.java | 2 +- src/de/steamwar/linkage/PluginCheck.java | 2 +- .../linkage/{types => typesold}/Command.java | 6 +- .../{types => typesold}/DisableLink.java | 6 +- .../{types => typesold}/EnableLink.java | 6 +- .../{types => typesold}/ListenerLink.java | 7 +- .../linkage/{types => typesold}/Plain.java | 6 +- .../{types => typesold}/UnlinkListener.java | 10 +- 16 files changed, 137 insertions(+), 168 deletions(-) rename src/de/steamwar/linkage/{DiscordMode.java => Context.java} (72%) create mode 100644 src/de/steamwar/linkage/LinkageTypeOld.java rename src/de/steamwar/linkage/{types => typesold}/Command.java (92%) rename src/de/steamwar/linkage/{types => typesold}/DisableLink.java (90%) rename src/de/steamwar/linkage/{types => typesold}/EnableLink.java (90%) rename src/de/steamwar/linkage/{types => typesold}/ListenerLink.java (97%) rename src/de/steamwar/linkage/{types => typesold}/Plain.java (89%) rename src/de/steamwar/linkage/{types => typesold}/UnlinkListener.java (89%) diff --git a/src/de/steamwar/linkage/AllowedContexts.java b/src/de/steamwar/linkage/AllowedContexts.java index 775aaf5..175a783 100644 --- a/src/de/steamwar/linkage/AllowedContexts.java +++ b/src/de/steamwar/linkage/AllowedContexts.java @@ -31,6 +31,6 @@ public @interface AllowedContexts { /** * The context in which this annotation is valid. */ - LinkageType.Context[] value(); + Context[] value(); } diff --git a/src/de/steamwar/linkage/DiscordMode.java b/src/de/steamwar/linkage/Context.java similarity index 72% rename from src/de/steamwar/linkage/DiscordMode.java rename to src/de/steamwar/linkage/Context.java index f30bab8..7559bd0 100644 --- a/src/de/steamwar/linkage/DiscordMode.java +++ b/src/de/steamwar/linkage/Context.java @@ -19,13 +19,7 @@ package de.steamwar.linkage; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@AllowedContexts(LinkageType.Context.BUNGEE) -@Retention(RetentionPolicy.SOURCE) -@Target({ElementType.TYPE}) -public @interface DiscordMode { +public enum Context { + BUNGEE, + SPIGOT } diff --git a/src/de/steamwar/linkage/EventMode.java b/src/de/steamwar/linkage/EventMode.java index 86c8528..cdff466 100644 --- a/src/de/steamwar/linkage/EventMode.java +++ b/src/de/steamwar/linkage/EventMode.java @@ -27,7 +27,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@AllowedContexts(LinkageType.Context.BUNGEE) +@AllowedContexts(Context.BUNGEE) @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) public @interface EventMode { diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 19d962e..ddf7923 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -22,33 +22,27 @@ package de.steamwar.linkage; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.FieldBuilder; import de.steamwar.linkage.plan.MethodBuilder; -import de.steamwar.linkage.plan.ParameterBuilder; import lombok.Getter; import lombok.SneakyThrows; -import javax.annotation.processing.AbstractProcessor; -import javax.annotation.processing.Messager; -import javax.annotation.processing.ProcessingEnvironment; -import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.*; import javax.lang.model.SourceVersion; import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; import java.io.*; -import java.io.Writer; import java.lang.annotation.Annotation; import java.nio.file.Files; import java.nio.file.Path; import java.util.*; import java.util.function.Consumer; import java.util.stream.Collectors; -import java.util.stream.Stream; +@SupportedAnnotationTypes("de.steamwar.linkage.Linked") public class LinkageProcessor extends AbstractProcessor { - @Getter - private static LinkageType.Context context; + private static Context context; @Getter private static String pluginMain; @@ -65,13 +59,6 @@ public class LinkageProcessor extends AbstractProcessor { return SourceVersion.latestSupported(); } - @Override - public Set getSupportedAnnotationTypes() { - return Stream.of(Linked.class, Linked.Linkages.class) - .map(Class::getCanonicalName) - .collect(Collectors.toSet()); - } - @SneakyThrows @Override public synchronized void init(ProcessingEnvironment processingEnv) { @@ -101,7 +88,7 @@ public class LinkageProcessor extends AbstractProcessor { messager.printMessage(Diagnostic.Kind.ERROR, "Could not find plugin.yml or bungee.yml"); return; } - context = pluginYMLFile.get().getName().equals("bungee.yml") ? LinkageType.Context.BUNGEE : LinkageType.Context.SPIGOT; + context = pluginYMLFile.get().getName().equals("bungee.yml") ? Context.BUNGEE : Context.SPIGOT; Optional mainName = getMainName(pluginYMLFile.get()); if (!mainName.isPresent()) { messager.printMessage(Diagnostic.Kind.ERROR, "Could not find main class in plugin.yml or bungee.yml"); @@ -134,36 +121,19 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addImport("java.lang.Class"); buildPlan.addImport("de.steamwar.linkage.LinkageType"); buildPlan.addImport("java.util.HashSet"); - buildPlan.addField(new FieldBuilder("Set>", "enabled", "new HashSet<>()")); - - MethodBuilder runsMethod = new MethodBuilder("run", "void"); - runsMethod.addParameter(new ParameterBuilder("Class...", "types")); - runsMethod.addLine("for (Class type : types) _run(type);"); - buildPlan.addMethod(runsMethod); - - - MethodBuilder runMethod = new MethodBuilder("_run", "void"); - runMethod.setPrivate(true); - buildPlan.addMethod(runMethod); - runMethod.addParameter(new ParameterBuilder("Class", "type")); - runMethod.addLine("if (!enabled.add(type)) return;"); - Set> alreadyGenerated = new HashSet<>(); Set elements = roundEnv.getElementsAnnotatedWith(Linked.class); - elements.addAll((Set) roundEnv.getElementsAnnotatedWith(Linked.Linkages.class)); - Map neededFields = new HashMap<>(); for (Element element : elements) { if (element.getKind() != ElementKind.CLASS) { continue; } TypeElement typeElement = (TypeElement) element; - Linked[] linkeds = element.getAnnotationsByType(Linked.class); - if (linkeds.length == 0) { + Linked linked = element.getAnnotation(Linked.class); + if (linked == null) { continue; } - - if (linkeds.length > 1) { + if (getLinkagesOfType(typeElement).size() > 1) { neededFields.put(typeElement.getQualifiedName().toString(), typeElement); } @@ -211,7 +181,7 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addMethod(initializer); }); - Map, MethodBuilder> methods = new HashMap<>(); + Map methods = new HashMap<>(); for (Element element : elements) { if (element.getKind() != ElementKind.CLASS) { continue; @@ -219,65 +189,21 @@ public class LinkageProcessor extends AbstractProcessor { TypeElement typeElement = (TypeElement) element; System.out.println("Found element: " + typeElement.getQualifiedName().toString()); - element.getAnnotationMirrors().stream() - .filter(annotationMirror -> { - String annotationNames = annotationMirror.getAnnotationType().asElement().getSimpleName().toString(); - return annotationNames.equals("Linked") || annotationNames.equals("Linkages"); - }) - .flatMap(annotationMirror -> annotationMirror.getElementValues().values().stream()) - .map(AnnotationValue::getValue) - .flatMap(o -> { - if (o instanceof List) { - return ((List) o).stream() - .map(AnnotationMirror.class::cast) - .map(AnnotationMirror::getElementValues) - .map(Map::values) - .flatMap(Collection::stream) - .map(AnnotationValue::getValue); - } - return Stream.of(o); - }) - .map(Object::toString) - .map(s -> { - try { - return Class.forName(s); - } catch (Exception e) { - messager.printMessage(Diagnostic.Kind.ERROR, "Could not find class " + s, element); - return null; - } - }) - .filter(Objects::nonNull) - .map(clazz -> { - try { - return (LinkageType) clazz.getDeclaredConstructor().newInstance(); - } catch (Exception e) { - messager.printMessage(Diagnostic.Kind.ERROR, "Could not create instance of " + clazz.getName(), element); - return null; - } - }) - .filter(Objects::nonNull) - .forEach(type -> { - Class clazz = type.getClass(); - if (!type.requirements(typeElement.getSuperclass().toString(), typeElement.getInterfaces().stream().map(TypeMirror::toString).collect(Collectors.toSet()), typeElement)) { - return; - } + Map> linkages = getLinkagesOfType(typeElement); + if (linkages.isEmpty()) { + continue; + } - if (alreadyGenerated.add(clazz)) { - runMethod.addLine("if (type == " + clazz.getTypeName() + ".class) {"); - runMethod.addLine(" run" + clazz.getSimpleName() + "();"); - runMethod.addLine("}"); - } - - MethodBuilder method = methods.computeIfAbsent(clazz, t -> { - MethodBuilder methodBuilder = new MethodBuilder("run" + t.getSimpleName(), "void"); - methodBuilder.setPrivate(true); - buildPlan.addMethod(methodBuilder); - return methodBuilder; - }); - specialElements(typeElement, buildPlan, method::addLine, () -> { - type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement); - }); + for (Map.Entry> entry : linkages.entrySet()) { + MethodBuilder method = methods.computeIfAbsent(entry.getKey(), s -> { + return new MethodBuilder(s, "void"); + }); + for (LinkageType type : entry.getValue()) { + specialElements(typeElement, buildPlan, method::addLine, () -> { + type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement); }); + } + } } BufferedWriter bufferedWriter = new BufferedWriter(writer); @@ -297,10 +223,9 @@ public class LinkageProcessor extends AbstractProcessor { MinVersion minVersion = typeElement.getAnnotation(MinVersion.class); MaxVersion maxVersion = typeElement.getAnnotation(MaxVersion.class); EventMode eventMode = typeElement.getAnnotation(EventMode.class); - DiscordMode discordMode = typeElement.getAnnotation(DiscordMode.class); PluginCheck[] pluginChecks = typeElement.getAnnotationsByType(PluginCheck.class); - if (context == LinkageType.Context.SPIGOT) { - errorOnNonNull(typeElement, eventMode, discordMode); + if (context == Context.SPIGOT) { + errorOnNonNull(typeElement, eventMode); List checks = new ArrayList<>(); if (minVersion != null) { buildPlan.addImport("de.steamwar.core.Core"); @@ -326,10 +251,6 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); checks.add(eventMode.value().getPrefix() + "BungeeCore.EVENT_MODE"); } - if (discordMode != null) { - buildPlan.addImport("de.steamwar.bungeecore.bot.config.SteamwarDiscordBotConfig"); - checks.add("SteamwarDiscordBotConfig.loaded"); - } if (pluginChecks.length != 0) { buildPlan.addImport("net.md_5.bungee.BungeeCord"); Arrays.stream(pluginChecks).map(pluginCheck -> { @@ -349,4 +270,35 @@ public class LinkageProcessor extends AbstractProcessor { } } } + + private Map> getLinkagesOfType(TypeElement typeElement) { + Map> linkages = new HashMap<>(); + LinkageType superClassType = resolveSingle(typeElement.getSuperclass()); + if (superClassType != null) { + linkages.computeIfAbsent(superClassType.method(), s -> new ArrayList<>()).add(superClassType); + } + for (TypeMirror typeMirror : typeElement.getInterfaces()) { + LinkageType interfaceType = resolveSingle(typeMirror); + if (interfaceType != null) { + linkages.computeIfAbsent(interfaceType.method(), s -> new ArrayList<>()).add(interfaceType); + } + } + return linkages; + } + + private LinkageType resolveSingle(TypeMirror typeMirror) { + String qualifier = typeMirror.toString(); + qualifier = qualifier.substring(qualifier.lastIndexOf('.') + 1); + try { + return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + context.name()).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + // Ignore + } + try { + return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + // Ignore + } + return null; + } } diff --git a/src/de/steamwar/linkage/LinkageType.java b/src/de/steamwar/linkage/LinkageType.java index afc37cc..9291279 100644 --- a/src/de/steamwar/linkage/LinkageType.java +++ b/src/de/steamwar/linkage/LinkageType.java @@ -23,36 +23,14 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -import java.util.Set; public interface LinkageType { - Context context = LinkageProcessor.getContext(); default String getPluginMain() { return LinkageProcessor.getPluginMain(); } - enum Context { - BUNGEE, - SPIGOT - } + String method(); - default boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - return true; - } - - default void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - switch (context) { - case BUNGEE: - generateCodeBungee(buildPlan, linkageTypeMethod, instance, typeElement); - break; - case SPIGOT: - generateCodeSpigot(buildPlan, linkageTypeMethod, instance, typeElement); - break; - } - } - default void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - } - default void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - } + void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement); } diff --git a/src/de/steamwar/linkage/LinkageTypeOld.java b/src/de/steamwar/linkage/LinkageTypeOld.java new file mode 100644 index 0000000..d85db84 --- /dev/null +++ b/src/de/steamwar/linkage/LinkageTypeOld.java @@ -0,0 +1,52 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage; + +import de.steamwar.linkage.plan.BuildPlan; +import de.steamwar.linkage.plan.MethodBuilder; + +import javax.lang.model.element.TypeElement; +import java.util.Set; + +public interface LinkageTypeOld { + + default String getPluginMain() { + return LinkageProcessor.getPluginMain(); + } + + default boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { + return true; + } + + default void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + switch (context) { + case BUNGEE: + generateCodeBungee(buildPlan, linkageTypeMethod, instance, typeElement); + break; + case SPIGOT: + generateCodeSpigot(buildPlan, linkageTypeMethod, instance, typeElement); + break; + } + } + default void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + } + default void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + } +} diff --git a/src/de/steamwar/linkage/Linked.java b/src/de/steamwar/linkage/Linked.java index b352982..f3836e0 100644 --- a/src/de/steamwar/linkage/Linked.java +++ b/src/de/steamwar/linkage/Linked.java @@ -23,13 +23,5 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) -@Repeatable(Linked.Linkages.class) public @interface Linked { - Class value(); - - @Retention(RetentionPolicy.SOURCE) - @Target({ElementType.TYPE}) - @interface Linkages { - @SuppressWarnings("unused") Linked[] value() default {}; - } } diff --git a/src/de/steamwar/linkage/MaxVersion.java b/src/de/steamwar/linkage/MaxVersion.java index 589a67f..e144b16 100644 --- a/src/de/steamwar/linkage/MaxVersion.java +++ b/src/de/steamwar/linkage/MaxVersion.java @@ -24,7 +24,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@AllowedContexts(LinkageType.Context.SPIGOT) +@AllowedContexts(Context.SPIGOT) @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) public @interface MaxVersion { diff --git a/src/de/steamwar/linkage/MinVersion.java b/src/de/steamwar/linkage/MinVersion.java index d897935..b6f5b99 100644 --- a/src/de/steamwar/linkage/MinVersion.java +++ b/src/de/steamwar/linkage/MinVersion.java @@ -24,7 +24,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -@AllowedContexts(LinkageType.Context.SPIGOT) +@AllowedContexts(Context.SPIGOT) @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) public @interface MinVersion { diff --git a/src/de/steamwar/linkage/PluginCheck.java b/src/de/steamwar/linkage/PluginCheck.java index 6b9931a..241f19a 100644 --- a/src/de/steamwar/linkage/PluginCheck.java +++ b/src/de/steamwar/linkage/PluginCheck.java @@ -21,7 +21,7 @@ package de.steamwar.linkage; import java.lang.annotation.*; -@AllowedContexts({LinkageType.Context.BUNGEE, LinkageType.Context.SPIGOT}) +@AllowedContexts({Context.BUNGEE, Context.SPIGOT}) @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) @Repeatable(PluginCheck.PluginChecks.class) diff --git a/src/de/steamwar/linkage/types/Command.java b/src/de/steamwar/linkage/typesold/Command.java similarity index 92% rename from src/de/steamwar/linkage/types/Command.java rename to src/de/steamwar/linkage/typesold/Command.java index 6d6948c..d890a49 100644 --- a/src/de/steamwar/linkage/types/Command.java +++ b/src/de/steamwar/linkage/typesold/Command.java @@ -17,16 +17,16 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.LinkageTypeOld; 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 { +public class Command implements LinkageTypeOld { @Override public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { diff --git a/src/de/steamwar/linkage/types/DisableLink.java b/src/de/steamwar/linkage/typesold/DisableLink.java similarity index 90% rename from src/de/steamwar/linkage/types/DisableLink.java rename to src/de/steamwar/linkage/typesold/DisableLink.java index 72f816d..a78d873 100644 --- a/src/de/steamwar/linkage/types/DisableLink.java +++ b/src/de/steamwar/linkage/typesold/DisableLink.java @@ -17,16 +17,16 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.LinkageTypeOld; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; import java.util.Set; -public class DisableLink implements LinkageType { +public class DisableLink implements LinkageTypeOld { @Override public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { diff --git a/src/de/steamwar/linkage/types/EnableLink.java b/src/de/steamwar/linkage/typesold/EnableLink.java similarity index 90% rename from src/de/steamwar/linkage/types/EnableLink.java rename to src/de/steamwar/linkage/typesold/EnableLink.java index d7630a4..a0b5171 100644 --- a/src/de/steamwar/linkage/types/EnableLink.java +++ b/src/de/steamwar/linkage/typesold/EnableLink.java @@ -17,16 +17,16 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.LinkageTypeOld; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; import java.util.Set; -public class EnableLink implements LinkageType { +public class EnableLink implements LinkageTypeOld { @Override public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { diff --git a/src/de/steamwar/linkage/types/ListenerLink.java b/src/de/steamwar/linkage/typesold/ListenerLink.java similarity index 97% rename from src/de/steamwar/linkage/types/ListenerLink.java rename to src/de/steamwar/linkage/typesold/ListenerLink.java index 5c815c5..5a96d73 100644 --- a/src/de/steamwar/linkage/types/ListenerLink.java +++ b/src/de/steamwar/linkage/typesold/ListenerLink.java @@ -17,9 +17,10 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.Context; +import de.steamwar.linkage.LinkageTypeOld; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.FieldBuilder; import de.steamwar.linkage.plan.MethodBuilder; @@ -31,7 +32,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -public class ListenerLink implements LinkageType { +public class ListenerLink implements LinkageTypeOld { @Override public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { diff --git a/src/de/steamwar/linkage/types/Plain.java b/src/de/steamwar/linkage/typesold/Plain.java similarity index 89% rename from src/de/steamwar/linkage/types/Plain.java rename to src/de/steamwar/linkage/typesold/Plain.java index 1890521..25258fc 100644 --- a/src/de/steamwar/linkage/types/Plain.java +++ b/src/de/steamwar/linkage/typesold/Plain.java @@ -17,15 +17,15 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.LinkageTypeOld; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -public class Plain implements LinkageType { +public class Plain implements LinkageTypeOld { @Override public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { diff --git a/src/de/steamwar/linkage/types/UnlinkListener.java b/src/de/steamwar/linkage/typesold/UnlinkListener.java similarity index 89% rename from src/de/steamwar/linkage/types/UnlinkListener.java rename to src/de/steamwar/linkage/typesold/UnlinkListener.java index 564d567..94e4662 100644 --- a/src/de/steamwar/linkage/types/UnlinkListener.java +++ b/src/de/steamwar/linkage/typesold/UnlinkListener.java @@ -17,17 +17,17 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.types; +package de.steamwar.linkage.typesold; -import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.Context; +import de.steamwar.linkage.LinkageTypeOld; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -import java.lang.reflect.Type; import java.util.Set; -public class UnlinkListener implements LinkageType { +public class UnlinkListener implements LinkageTypeOld { @Override public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { @@ -42,7 +42,7 @@ public class UnlinkListener implements LinkageType { public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); - linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(" + instance + ");"); + linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().unregisterListener(" + instance + ");"); } @Override -- 2.39.2 From 49a6a6f23fce853c610909acc6167ce4b7fb7dfb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 24 Sep 2022 13:34:10 +0200 Subject: [PATCH 2/4] Update some stuff --- src/de/steamwar/linkage/LinkageProcessor.java | 34 ++++++------ src/de/steamwar/linkage/LinkageTypeOld.java | 52 ------------------ .../Disable_GENERIC.java} | 17 +++--- .../Enable_GENERIC.java} | 17 +++--- .../Listener_BUNGEE.java} | 24 ++++----- .../Listener_SPIGOT.java} | 32 ++++------- .../Plain.java => types/Plain_GENERIC.java} | 17 +++--- .../linkage/types/SWCommand_BUNGEE.java | 39 ++++++++++++++ .../linkage/types/SWCommand_SPIGOT.java | 39 ++++++++++++++ .../linkage/typesold/UnlinkListener.java | 53 ------------------- 10 files changed, 139 insertions(+), 185 deletions(-) delete mode 100644 src/de/steamwar/linkage/LinkageTypeOld.java rename src/de/steamwar/linkage/{typesold/EnableLink.java => types/Disable_GENERIC.java} (66%) rename src/de/steamwar/linkage/{typesold/DisableLink.java => types/Enable_GENERIC.java} (66%) rename src/de/steamwar/linkage/{typesold/Command.java => types/Listener_BUNGEE.java} (53%) rename src/de/steamwar/linkage/{typesold/ListenerLink.java => types/Listener_SPIGOT.java} (74%) rename src/de/steamwar/linkage/{typesold/Plain.java => types/Plain_GENERIC.java} (73%) create mode 100644 src/de/steamwar/linkage/types/SWCommand_BUNGEE.java create mode 100644 src/de/steamwar/linkage/types/SWCommand_SPIGOT.java delete mode 100644 src/de/steamwar/linkage/typesold/UnlinkListener.java diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index ddf7923..83f6794 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -22,6 +22,7 @@ package de.steamwar.linkage; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.FieldBuilder; import de.steamwar.linkage.plan.MethodBuilder; +import de.steamwar.linkage.types.Plain_GENERIC; import lombok.Getter; import lombok.SneakyThrows; @@ -117,10 +118,7 @@ public class LinkageProcessor extends AbstractProcessor { Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter(); BuildPlan buildPlan = new BuildPlan(packageName, className); - buildPlan.addImport("java.util.Set"); - buildPlan.addImport("java.lang.Class"); buildPlan.addImport("de.steamwar.linkage.LinkageType"); - buildPlan.addImport("java.util.HashSet"); Set elements = roundEnv.getElementsAnnotatedWith(Linked.class); Map neededFields = new HashMap<>(); @@ -168,17 +166,7 @@ public class LinkageProcessor extends AbstractProcessor { } neededFields.forEach((s, typeElement) -> { buildPlan.addImport(typeElement.getQualifiedName().toString()); - buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString())); - - MethodBuilder initializer = new MethodBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString()); - initializer.setPrivate(true); - specialElements(typeElement, buildPlan, initializer::addLine, () -> { - initializer.addLine("if (" + typeElement.getSimpleName().toString() + " == null) {"); - initializer.addLine(" " + typeElement.getSimpleName().toString() + " = new " + typeElement.getSimpleName().toString() + "();"); - initializer.addLine("}"); - }); - initializer.addLine("return " + typeElement.getSimpleName().toString() + ";"); - buildPlan.addMethod(initializer); + buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString(), "new " + typeElement.getSimpleName().toString() + "()")); }); Map methods = new HashMap<>(); @@ -196,10 +184,13 @@ public class LinkageProcessor extends AbstractProcessor { for (Map.Entry> entry : linkages.entrySet()) { MethodBuilder method = methods.computeIfAbsent(entry.getKey(), s -> { - return new MethodBuilder(s, "void"); + MethodBuilder methodBuilder = new MethodBuilder(s, "void"); + buildPlan.addMethod(methodBuilder); + return methodBuilder; }); for (LinkageType type : entry.getValue()) { specialElements(typeElement, buildPlan, method::addLine, () -> { + buildPlan.addImport(typeElement.getQualifiedName().toString()); type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement); }); } @@ -214,9 +205,9 @@ public class LinkageProcessor extends AbstractProcessor { private String getElement(TypeElement typeElement, Map neededFields) { if (neededFields.containsKey(typeElement.getQualifiedName().toString())) { - return typeElement.getSimpleName().toString() + "()"; + return typeElement.getSimpleName().toString(); } - return "new " + typeElement.getQualifiedName().toString() + "()"; + return "new " + typeElement.getSimpleName().toString() + "()"; } private void specialElements(TypeElement typeElement, BuildPlan buildPlan, Consumer stringConsumer, Runnable inner) { @@ -271,6 +262,8 @@ public class LinkageProcessor extends AbstractProcessor { } } + private Plain_GENERIC plain_GENERIC = new Plain_GENERIC(); + private Map> getLinkagesOfType(TypeElement typeElement) { Map> linkages = new HashMap<>(); LinkageType superClassType = resolveSingle(typeElement.getSuperclass()); @@ -283,6 +276,9 @@ public class LinkageProcessor extends AbstractProcessor { linkages.computeIfAbsent(interfaceType.method(), s -> new ArrayList<>()).add(interfaceType); } } + if (!linkages.containsKey(plain_GENERIC.method())) { + linkages.put(plain_GENERIC.method(), Collections.singletonList(plain_GENERIC)); + } return linkages; } @@ -290,12 +286,12 @@ public class LinkageProcessor extends AbstractProcessor { String qualifier = typeMirror.toString(); qualifier = qualifier.substring(qualifier.lastIndexOf('.') + 1); try { - return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + context.name()).getDeclaredConstructor().newInstance(); + return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + "_" + context.name()).getDeclaredConstructor().newInstance(); } catch (Exception e) { // Ignore } try { - return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier).getDeclaredConstructor().newInstance(); + return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + "_GENERIC").getDeclaredConstructor().newInstance(); } catch (Exception e) { // Ignore } diff --git a/src/de/steamwar/linkage/LinkageTypeOld.java b/src/de/steamwar/linkage/LinkageTypeOld.java deleted file mode 100644 index d85db84..0000000 --- a/src/de/steamwar/linkage/LinkageTypeOld.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2022 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.linkage; - -import de.steamwar.linkage.plan.BuildPlan; -import de.steamwar.linkage.plan.MethodBuilder; - -import javax.lang.model.element.TypeElement; -import java.util.Set; - -public interface LinkageTypeOld { - - default String getPluginMain() { - return LinkageProcessor.getPluginMain(); - } - - default boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - return true; - } - - default void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - switch (context) { - case BUNGEE: - generateCodeBungee(buildPlan, linkageTypeMethod, instance, typeElement); - break; - case SPIGOT: - generateCodeSpigot(buildPlan, linkageTypeMethod, instance, typeElement); - break; - } - } - default void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - } - default void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - } -} diff --git a/src/de/steamwar/linkage/typesold/EnableLink.java b/src/de/steamwar/linkage/types/Disable_GENERIC.java similarity index 66% rename from src/de/steamwar/linkage/typesold/EnableLink.java rename to src/de/steamwar/linkage/types/Disable_GENERIC.java index a0b5171..ea58c91 100644 --- a/src/de/steamwar/linkage/typesold/EnableLink.java +++ b/src/de/steamwar/linkage/types/Disable_GENERIC.java @@ -1,7 +1,7 @@ /* * This file is a part of the SteamWar software. * - * Copyright (C) 2022 SteamWar.de-Serverteam + * Copyright (C) 2020 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,24 +17,23 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.typesold; +package de.steamwar.linkage.types; -import de.steamwar.linkage.LinkageTypeOld; +import de.steamwar.linkage.LinkageType; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -import java.util.Set; -public class EnableLink implements LinkageTypeOld { +public class Disable_GENERIC implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - return interfaces.contains("de.steamwar.linkage.api.Enable"); + public String method() { + return "unlink"; } @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - linkageTypeMethod.addLine(instance + ".enable();"); + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ".disable();"); } } diff --git a/src/de/steamwar/linkage/typesold/DisableLink.java b/src/de/steamwar/linkage/types/Enable_GENERIC.java similarity index 66% rename from src/de/steamwar/linkage/typesold/DisableLink.java rename to src/de/steamwar/linkage/types/Enable_GENERIC.java index a78d873..03afd57 100644 --- a/src/de/steamwar/linkage/typesold/DisableLink.java +++ b/src/de/steamwar/linkage/types/Enable_GENERIC.java @@ -1,7 +1,7 @@ /* * This file is a part of the SteamWar software. * - * Copyright (C) 2022 SteamWar.de-Serverteam + * Copyright (C) 2020 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,24 +17,23 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.typesold; +package de.steamwar.linkage.types; -import de.steamwar.linkage.LinkageTypeOld; +import de.steamwar.linkage.LinkageType; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -import java.util.Set; -public class DisableLink implements LinkageTypeOld { +public class Enable_GENERIC implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - return interfaces.contains("de.steamwar.linkage.api.Disable"); + public String method() { + return "link"; } @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - linkageTypeMethod.addLine(instance + ".disable();"); + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ".enable();"); } } diff --git a/src/de/steamwar/linkage/typesold/Command.java b/src/de/steamwar/linkage/types/Listener_BUNGEE.java similarity index 53% rename from src/de/steamwar/linkage/typesold/Command.java rename to src/de/steamwar/linkage/types/Listener_BUNGEE.java index d890a49..327b19a 100644 --- a/src/de/steamwar/linkage/typesold/Command.java +++ b/src/de/steamwar/linkage/types/Listener_BUNGEE.java @@ -1,7 +1,7 @@ /* * This file is a part of the SteamWar software. * - * Copyright (C) 2022 SteamWar.de-Serverteam + * Copyright (C) 2020 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,29 +17,25 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.typesold; +package de.steamwar.linkage.types; -import de.steamwar.linkage.LinkageTypeOld; +import de.steamwar.linkage.LinkageType; 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 LinkageTypeOld { +public class Listener_BUNGEE implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - return superClass.equals("de.steamwar.command.SWCommand"); + public String method() { + return "link"; } @Override - public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - linkageTypeMethod.addLine(instance + ";"); - } - - @Override - public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - linkageTypeMethod.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);"); + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); + buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); + method.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");"); } } diff --git a/src/de/steamwar/linkage/typesold/ListenerLink.java b/src/de/steamwar/linkage/types/Listener_SPIGOT.java similarity index 74% rename from src/de/steamwar/linkage/typesold/ListenerLink.java rename to src/de/steamwar/linkage/types/Listener_SPIGOT.java index 5a96d73..4d78b08 100644 --- a/src/de/steamwar/linkage/typesold/ListenerLink.java +++ b/src/de/steamwar/linkage/types/Listener_SPIGOT.java @@ -17,10 +17,9 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.typesold; +package de.steamwar.linkage.types; -import de.steamwar.linkage.Context; -import de.steamwar.linkage.LinkageTypeOld; +import de.steamwar.linkage.LinkageType; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.FieldBuilder; import de.steamwar.linkage.plan.MethodBuilder; @@ -30,28 +29,16 @@ import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; import java.util.HashMap; import java.util.Map; -import java.util.Set; -public class ListenerLink implements LinkageTypeOld { +public class Listener_SPIGOT implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - if (context == Context.BUNGEE) { - return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); - } else { - return interfaces.contains("org.bukkit.event.Listener"); - } + public String method() { + return "link"; } @Override - public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); - buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); - linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");"); - } - - @Override - public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { Map eventClasses = new HashMap<>(); Map eventMethods = new HashMap<>(); @@ -88,12 +75,11 @@ public class ListenerLink implements LinkageTypeOld { methodBuilder.addLine("};"); 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();"); + method.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();"); }); - buildPlan.addImport(typeElement.getQualifiedName().toString()); String localInstance = "local" + typeElement.getSimpleName().toString(); - linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); + method.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); eventMethods.forEach((type, executableElement) -> { AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); if (eventHandler == null) { @@ -108,7 +94,7 @@ public class ListenerLink implements LinkageTypeOld { ignoreCancelled = entry.getValue().getValue().toString(); } } - linkageTypeMethod.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");"); + method.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");"); }); } } diff --git a/src/de/steamwar/linkage/typesold/Plain.java b/src/de/steamwar/linkage/types/Plain_GENERIC.java similarity index 73% rename from src/de/steamwar/linkage/typesold/Plain.java rename to src/de/steamwar/linkage/types/Plain_GENERIC.java index 25258fc..eca2830 100644 --- a/src/de/steamwar/linkage/typesold/Plain.java +++ b/src/de/steamwar/linkage/types/Plain_GENERIC.java @@ -1,7 +1,7 @@ /* * This file is a part of the SteamWar software. * - * Copyright (C) 2022 SteamWar.de-Serverteam + * Copyright (C) 2020 SteamWar.de-Serverteam * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by @@ -17,18 +17,23 @@ * along with this program. If not, see . */ -package de.steamwar.linkage.typesold; +package de.steamwar.linkage.types; -import de.steamwar.linkage.LinkageTypeOld; +import de.steamwar.linkage.LinkageType; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -public class Plain implements LinkageTypeOld { +public class Plain_GENERIC implements LinkageType { @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - linkageTypeMethod.addLine(instance + ";"); + public String method() { + return "link"; + } + + @Override + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ";"); } } diff --git a/src/de/steamwar/linkage/types/SWCommand_BUNGEE.java b/src/de/steamwar/linkage/types/SWCommand_BUNGEE.java new file mode 100644 index 0000000..5db62c4 --- /dev/null +++ b/src/de/steamwar/linkage/types/SWCommand_BUNGEE.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage.types; + +import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.plan.BuildPlan; +import de.steamwar.linkage.plan.MethodBuilder; + +import javax.lang.model.element.TypeElement; + +public class SWCommand_BUNGEE implements LinkageType { + + @Override + public String method() { + return "link"; + } + + @Override + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ";"); + } +} diff --git a/src/de/steamwar/linkage/types/SWCommand_SPIGOT.java b/src/de/steamwar/linkage/types/SWCommand_SPIGOT.java new file mode 100644 index 0000000..96e3f77 --- /dev/null +++ b/src/de/steamwar/linkage/types/SWCommand_SPIGOT.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage.types; + +import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.plan.BuildPlan; +import de.steamwar.linkage.plan.MethodBuilder; + +import javax.lang.model.element.TypeElement; + +public class SWCommand_SPIGOT implements LinkageType { + + @Override + public String method() { + return "link"; + } + + @Override + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);"); + } +} diff --git a/src/de/steamwar/linkage/typesold/UnlinkListener.java b/src/de/steamwar/linkage/typesold/UnlinkListener.java deleted file mode 100644 index 94e4662..0000000 --- a/src/de/steamwar/linkage/typesold/UnlinkListener.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 SteamWar.de-Serverteam - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - */ - -package de.steamwar.linkage.typesold; - -import de.steamwar.linkage.Context; -import de.steamwar.linkage.LinkageTypeOld; -import de.steamwar.linkage.plan.BuildPlan; -import de.steamwar.linkage.plan.MethodBuilder; - -import javax.lang.model.element.TypeElement; -import java.util.Set; - -public class UnlinkListener implements LinkageTypeOld { - - @Override - public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { - if (context == Context.BUNGEE) { - return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); - } else { - return interfaces.contains("org.bukkit.event.Listener"); - } - } - - @Override - public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); - buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); - linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().unregisterListener(" + instance + ");"); - } - - @Override - public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - buildPlan.addImport("org.bukkit.event.HandlerList"); - linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");"); - } -} -- 2.39.2 From f0151f2c8d85b7fce92cfa1546484ba6b8229740 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 24 Sep 2022 13:38:00 +0200 Subject: [PATCH 3/4] Update some stuff --- src/de/steamwar/linkage/LinkageProcessor.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 83f6794..f2f245c 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -166,7 +166,9 @@ public class LinkageProcessor extends AbstractProcessor { } neededFields.forEach((s, typeElement) -> { buildPlan.addImport(typeElement.getQualifiedName().toString()); - buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString(), "new " + typeElement.getSimpleName().toString() + "()")); + String t = typeElement.getSimpleName().toString(); + t = t.substring(0, 1).toLowerCase() + t.substring(1); + buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), t, "new " + typeElement.getSimpleName().toString() + "()")); }); Map methods = new HashMap<>(); @@ -205,7 +207,8 @@ public class LinkageProcessor extends AbstractProcessor { private String getElement(TypeElement typeElement, Map neededFields) { if (neededFields.containsKey(typeElement.getQualifiedName().toString())) { - return typeElement.getSimpleName().toString(); + String s = typeElement.getSimpleName().toString(); + return s.substring(0, 1).toLowerCase() + s.substring(1); } return "new " + typeElement.getSimpleName().toString() + "()"; } -- 2.39.2 From e02930f124322a1d3a8be7e20a674146348c3a1d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 24 Sep 2022 13:48:40 +0200 Subject: [PATCH 4/4] Add PacketHandler_GENERIC --- .../linkage/types/PacketHandler_GENERIC.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/de/steamwar/linkage/types/PacketHandler_GENERIC.java diff --git a/src/de/steamwar/linkage/types/PacketHandler_GENERIC.java b/src/de/steamwar/linkage/types/PacketHandler_GENERIC.java new file mode 100644 index 0000000..630f6f9 --- /dev/null +++ b/src/de/steamwar/linkage/types/PacketHandler_GENERIC.java @@ -0,0 +1,39 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage.types; + +import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.plan.BuildPlan; +import de.steamwar.linkage.plan.MethodBuilder; + +import javax.lang.model.element.TypeElement; + +public class PacketHandler_GENERIC implements LinkageType { + + @Override + public String method() { + return "link"; + } + + @Override + public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) { + method.addLine(instance + ".register();"); + } +} -- 2.39.2