Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
ac91c77584
Commit
98925dcb4a
@ -1,264 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
|
||||
import javax.annotation.processing.*;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.*;
|
||||
import javax.lang.model.type.DeclaredType;
|
||||
import javax.tools.Diagnostic;
|
||||
import java.io.Writer;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@SupportedAnnotationTypes("de.steamwar.bausystem.linkage.Linked")
|
||||
public class LinkageProcessor extends AbstractProcessor {
|
||||
|
||||
private Messager messager;
|
||||
private Writer writer;
|
||||
private boolean processed = false;
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
return SourceVersion.latestSupported();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public synchronized void init(ProcessingEnvironment processingEnv) {
|
||||
super.init(processingEnv);
|
||||
writer = processingEnv.getFiler().createSourceFile("de.steamwar.bausystem.linkage.LinkageUtils").openWriter();
|
||||
messager = processingEnv.getMessager();
|
||||
}
|
||||
|
||||
@SneakyThrows
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (processed) return false;
|
||||
processed = true;
|
||||
List<String> fields = new ArrayList<>();
|
||||
Map<LinkageType, List<String>> linkLines = new HashMap<>();
|
||||
List<String> staticLines = new ArrayList<>();
|
||||
|
||||
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Linked.class);
|
||||
elements.addAll((Set)roundEnv.getElementsAnnotatedWith(Linked.Linkages.class));
|
||||
|
||||
Map<String, TypeElement> 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) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (linkeds.length > 1) {
|
||||
neededFields.put(typeElement.getQualifiedName().toString(), typeElement);
|
||||
}
|
||||
|
||||
List<VariableElement> variableElements = typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.FIELD).map(VariableElement.class::cast).filter(e -> {
|
||||
return e.getAnnotation(LinkedInstance.class) != null;
|
||||
}).collect(Collectors.toList());
|
||||
if (variableElements.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (VariableElement variableElement : variableElements) {
|
||||
if (!variableElement.getModifiers().contains(Modifier.PUBLIC)) {
|
||||
messager.printMessage(Diagnostic.Kind.ERROR, "Field " + variableElement.getSimpleName() + " must be public", variableElement);
|
||||
continue;
|
||||
}
|
||||
if (variableElement.getModifiers().contains(Modifier.STATIC)) {
|
||||
messager.printMessage(Diagnostic.Kind.ERROR, "Field " + variableElement.getSimpleName() + " must be non static", variableElement);
|
||||
continue;
|
||||
}
|
||||
if (variableElement.getModifiers().contains(Modifier.FINAL)) {
|
||||
messager.printMessage(Diagnostic.Kind.ERROR, "Field " + variableElement.getSimpleName() + " must be non final", variableElement);
|
||||
continue;
|
||||
}
|
||||
neededFields.put(typeElement.getQualifiedName().toString(), typeElement);
|
||||
TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement();
|
||||
neededFields.put(fieldType.getQualifiedName().toString(), fieldType);
|
||||
|
||||
System.out.println(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields));
|
||||
staticLines.add(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields));
|
||||
}
|
||||
}
|
||||
neededFields.forEach((s, typeElement) -> {
|
||||
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);
|
||||
eventClasses.forEach((s, typeElement) -> {
|
||||
eventLines.add("handlerList" + typeElement.getSimpleName().toString() + " = " + typeElement.getQualifiedName().toString() + ".getHandlerList()");
|
||||
});
|
||||
eventLines.add("");
|
||||
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) {
|
||||
if (element.getKind() != ElementKind.CLASS) {
|
||||
continue;
|
||||
}
|
||||
TypeElement typeElement = (TypeElement) element;
|
||||
Linked[] linkeds = element.getAnnotationsByType(Linked.class);
|
||||
if (linkeds.length == 0) {
|
||||
continue;
|
||||
}
|
||||
MinVersion minVersion = element.getAnnotation(MinVersion.class);
|
||||
|
||||
System.out.println("Found element: " + typeElement.getQualifiedName().toString());
|
||||
|
||||
Arrays.stream(linkeds).map(Linked::value).forEach(linkageType -> {
|
||||
if (linkageType.toRun == null) return;
|
||||
if (linkageType.className != null) {
|
||||
if (!typeElement.getSuperclass().toString().equals(linkageType.className)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (linkageType.interfaceName != null) {
|
||||
if (typeElement.getInterfaces().stream().noneMatch(i -> i.toString().equals(linkageType.interfaceName))) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
List<String> strings = linkLines.computeIfAbsent(linkageType, ignore -> new ArrayList<>());
|
||||
if (minVersion != null) {
|
||||
strings.add("if (de.steamwar.core.Core.getVersion() >= " + minVersion.value() + ") {");
|
||||
strings.add(" " + linkageType.toRun.replace("$", getElement(typeElement, neededFields)));
|
||||
strings.add("}");
|
||||
} else {
|
||||
strings.add(linkageType.toRun.replace("$", getElement(typeElement, neededFields)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
writer.write("package de.steamwar.bausystem.linkage;\n\n");
|
||||
writer.write("public class LinkageUtils {\n");
|
||||
for (String s : fields) {
|
||||
writer.write(" private static " + s + ";\n");
|
||||
}
|
||||
writer.write("\n");
|
||||
for (Map.Entry<String, TypeElement> entry : neededFields.entrySet()) {
|
||||
String field = entry.getValue().getSimpleName().toString();
|
||||
writer.write(" public static " + entry.getValue().getQualifiedName().toString() + " " + field + "() {\n");
|
||||
writer.write(" if (" + field + " == null) {\n");
|
||||
writer.write(" " + field + " = new " + entry.getValue().getQualifiedName().toString() + "();\n");
|
||||
writer.write(" }\n");
|
||||
writer.write(" return " + field + ";\n");
|
||||
writer.write(" }\n");
|
||||
writer.write("\n");
|
||||
}
|
||||
writer.write(" private static final java.util.Set<de.steamwar.bausystem.linkage.LinkageType> LINKED = new java.util.HashSet<>();\n");
|
||||
writer.write("\n");
|
||||
writer.write(" static {\n");
|
||||
for (String s : staticLines) {
|
||||
writer.write(" " + s + ";\n");
|
||||
}
|
||||
writer.write(" }\n");
|
||||
writer.write("\n");
|
||||
writer.write(" public static void run(de.steamwar.bausystem.linkage.LinkageType link) {\n");
|
||||
writer.write(" if (!LINKED.add(link)) return;\n");
|
||||
writer.write(" switch (link) {\n");
|
||||
for (LinkageType type : linkLines.keySet()) {
|
||||
writer.write(" case " + type.name() + ":\n");
|
||||
writer.write(" " + type + "();\n");
|
||||
writer.write(" break;\n");
|
||||
}
|
||||
writer.write(" default:\n");
|
||||
writer.write(" break;\n");
|
||||
writer.write(" }\n");
|
||||
writer.write(" }\n");
|
||||
writer.write("\n");
|
||||
for (Map.Entry<LinkageType, List<String>> entry : linkLines.entrySet()) {
|
||||
writer.write(" private static void " + entry.getKey() + "() {\n");
|
||||
for (String s : entry.getValue()) {
|
||||
writer.write(" " + s + ";\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() + ";\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();
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getElement(TypeElement typeElement, Map<String, TypeElement> neededFields) {
|
||||
if (neededFields.containsKey(typeElement.getQualifiedName().toString())) {
|
||||
return typeElement.getSimpleName().toString() + "()";
|
||||
}
|
||||
return "new " + typeElement.getQualifiedName().toString() + "()";
|
||||
}
|
||||
}
|
@ -1,57 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
public enum LinkageType {
|
||||
|
||||
// NORMAL
|
||||
COMMAND("$.setMessage(de.steamwar.bausystem.BauSystem.MESSAGE)", "de.steamwar.command.SWCommand"),
|
||||
ENABLE_LINK("$.enable()", null, "de.steamwar.bausystem.linkage.Enable"),
|
||||
DISABLE_LINK("$.disable()", null, "de.steamwar.bausystem.linkage.Disable"),
|
||||
PLAIN("$", null),
|
||||
LISTENER(), // Is handled internally from LinkageProcessor
|
||||
UNLINK_LISTENER("org.bukkit.event.HandlerList.unregisterAll($)", null, "org.bukkit.event.Listener"),
|
||||
|
||||
// SPECIFIC
|
||||
BAU_GUI_ITEM("de.steamwar.bausystem.features.gui.BauGUI.addItem($)", "de.steamwar.bausystem.linkage.specific.BauGuiItem"),
|
||||
SCRIPT_COMMAND("de.steamwar.bausystem.features.script.ScriptExecutor.SPECIAL_COMMANDS.add($)", null, "de.steamwar.bausystem.features.script.SpecialCommand"),
|
||||
CONFIG_CONVERTER("de.steamwar.bausystem.configplayer.Config.addConfigConverter($)", null, "de.steamwar.bausystem.configplayer.ConfigConverter"),
|
||||
SCOREBOARD(null, null, "de.steamwar.bausystem.linkage.specific.ScoreboardItem"),
|
||||
PANZERN("de.steamwar.bausystem.features.slaves.panzern.Panzern.add($)", null, "de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithm"),
|
||||
SMART_PLACE("de.steamwar.bausystem.features.smartplace.SmartPlaceListener.add($)", null, "de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour");
|
||||
|
||||
String toRun = null;
|
||||
String className = null;
|
||||
String interfaceName = null;
|
||||
|
||||
LinkageType() {
|
||||
}
|
||||
|
||||
LinkageType(String toRun, String className) {
|
||||
this.toRun = toRun;
|
||||
this.className = className;
|
||||
}
|
||||
|
||||
LinkageType(String toRun, String className, String interfaceName) {
|
||||
this.toRun = toRun;
|
||||
this.className = className;
|
||||
this.interfaceName = interfaceName;
|
||||
}
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
import org.atteo.classindex.IndexAnnotated;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@IndexAnnotated
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@Repeatable(Linked.Linkages.class)
|
||||
public @interface Linked {
|
||||
LinkageType value();
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
@interface Linkages {
|
||||
@SuppressWarnings("unused") Linked[] value() default {};
|
||||
}
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.FIELD})
|
||||
public @interface LinkedInstance {
|
||||
}
|
@ -1,31 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.linkage;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target({ElementType.TYPE})
|
||||
public @interface MinVersion {
|
||||
int value();
|
||||
}
|
In neuem Issue referenzieren
Einen Benutzer sperren