From 805e4de4e5b639224929eb6e89197454a8d1c5b0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Sat, 27 Aug 2022 15:07:10 +0200 Subject: [PATCH] Remove reflections from startup Signed-off-by: yoyosource --- BauSystem_Linkage/build.gradle | 69 +++++++++ .../bausystem/linkage/LinkageProcessor.java | 139 ++++++++++++++++++ .../bausystem/linkage/LinkageType.java | 55 +++++++ .../de/steamwar/bausystem/linkage/Linked.java | 0 .../bausystem/linkage/LinkedInstance.java | 0 .../linkage/ProcessorImplementation.java | 27 ++++ BauSystem_Main/build.gradle | 3 + .../bausystem/features/bau/BauCommand.java | 2 +- .../bausystem/features/bau/InfoCommand.java | 2 +- .../features/detonator/DetonatorCommand.java | 2 +- .../bausystem/features/gui/BauGUICommand.java | 2 +- .../features/hotbar/HotbarCommand.java | 2 +- .../features/loader/LoaderCommand.java | 2 +- .../features/loadtimer/LoadtimerCommand.java | 2 +- .../features/region/ColorCommand.java | 2 +- .../features/region/RegionCommand.java | 4 +- .../features/region/TNTListener.java | 2 +- .../region/items/ResetBauGuiItem.java | 2 +- .../region/items/TestblockBauGuiItem.java | 2 +- .../features/script/ScriptCommand.java | 2 +- .../features/script/ScriptEventListener.java | 2 +- .../features/techhider/TechHiderCommand.java | 2 +- .../features/util/NoClipCommand.java | 2 +- .../bausystem/features/util/SlotCommand.java | 2 +- .../util/items/DeclutterBauGuiItem.java | 2 +- .../util/items/KillAllBauGuiItem.java | 2 +- .../bausystem/features/warp/WarpCommand.java | 4 +- .../bausystem/features/xray/XrayCommand.java | 2 +- .../bausystem/linkage/LinkageType.java | 69 --------- .../bausystem/linkage/LinkageUtils.java | 139 ------------------ 30 files changed, 316 insertions(+), 231 deletions(-) create mode 100644 BauSystem_Linkage/build.gradle create mode 100644 BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java create mode 100644 BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java rename {BauSystem_Main => BauSystem_Linkage}/src/de/steamwar/bausystem/linkage/Linked.java (100%) rename {BauSystem_Main => BauSystem_Linkage}/src/de/steamwar/bausystem/linkage/LinkedInstance.java (100%) create mode 100644 BauSystem_Linkage/src/de/steamwar/bausystem/linkage/ProcessorImplementation.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java delete mode 100644 BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java diff --git a/BauSystem_Linkage/build.gradle b/BauSystem_Linkage/build.gradle new file mode 100644 index 00000000..486cb7c7 --- /dev/null +++ b/BauSystem_Linkage/build.gradle @@ -0,0 +1,69 @@ +/* + * 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 . + */ + +plugins { + id 'base' + id 'java' +} + +group 'steamwar' +version '1.0' + +compileJava.options.encoding = 'UTF-8' + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +sourceSets { + main { + java { + srcDirs = ['src/'] + } + resources { + srcDirs = ['src/'] + exclude '**/*.java', '**/*.kt' + } + } +} + +dependencies { + compileOnly 'org.projectlombok:lombok:1.18.22' + testCompileOnly 'org.projectlombok:lombok:1.18.22' + annotationProcessor 'org.projectlombok:lombok:1.18.22' + testAnnotationProcessor 'org.projectlombok:lombok:1.18.22' + + implementation 'org.atteo.classindex:classindex:3.11' + testImplementation 'org.atteo.classindex:classindex:3.11' + annotationProcessor 'org.atteo.classindex:classindex:3.11' + testAnnotationProcessor 'org.atteo.classindex:classindex:3.11' +} + +task buildResources { + doLast { + File from = new File("${buildDir}/classes/java/main/META-INF/annotations/de.steamwar.bausystem.linkage.ProcessorImplementation") + File to = new File("${buildDir}/classes/java/main/META-INF/services/javax.annotation.processing.Processor") + to.delete() + to.parentFile.mkdirs() + to.createNewFile() + for (String s : from.readLines()) { + to.append(s + "\n") + } + } +} +classes.finalizedBy(buildResources) \ No newline at end of file diff --git a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java new file mode 100644 index 00000000..f615dd1f --- /dev/null +++ b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageProcessor.java @@ -0,0 +1,139 @@ +/* + * 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.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 javax.tools.JavaFileObject; +import java.io.Writer; +import java.util.*; +import java.util.stream.Collectors; + +@ProcessorImplementation +@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 annotations, RoundEnvironment roundEnv) { + if (processed) return false; + processed = true; + List fields = new ArrayList<>(); + List linkLines = new ArrayList<>(); + List unlinkLines = new ArrayList<>(); + + Set elements = roundEnv.getElementsAnnotatedWith(Linked.class); + elements.addAll((Set)roundEnv.getElementsAnnotatedWith(Linked.Linkages.class)); + 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; + } + + System.out.println("Found element: " + typeElement.getQualifiedName().toString()); + + fields.add(typeElement.getQualifiedName().toString() + " " + typeElement.getSimpleName().toString() + " = new " + 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; + } + } + (linkageType.unlink ? unlinkLines : linkLines).add(linkageType.toRun.replace("$", typeElement.getSimpleName().toString())); + }); + + List variableElements = typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.FIELD).map(e -> (VariableElement) e).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; + } + linkLines.add(typeElement.getSimpleName().toString() + "." + variableElement.getSimpleName().toString() + " = " + ((DeclaredType) variableElement.asType()).asElement().getSimpleName().toString()); + } + } + + writer.write("package de.steamwar.bausystem.linkage;\n\n"); + writer.write("public class LinkageUtils {\n"); + for (String s : fields) { + writer.write(" public static " + s + ";\n"); + } + writer.write("\n"); + writer.write(" public static void link() {\n"); + for (String s : linkLines) { + writer.write(" " + s + ";\n"); + } + writer.write(" }\n"); + writer.write("\n"); + writer.write(" public static void unlink() {\n"); + for (String s : unlinkLines) { + writer.write(" " + s + ";\n"); + } + writer.write(" }\n"); + writer.write("}\n"); + writer.flush(); + writer.close(); + return true; + } +} diff --git a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java new file mode 100644 index 00000000..81e506e7 --- /dev/null +++ b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkageType.java @@ -0,0 +1,55 @@ +/* + * 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.bausystem.linkage; + +import lombok.AllArgsConstructor; +import lombok.RequiredArgsConstructor; + +@RequiredArgsConstructor +@AllArgsConstructor +public enum LinkageType { + + // NORMAL + COMMAND(false, "$.setMessage(de.steamwar.bausystem.BauSystem.MESSAGE)", "de.steamwar.command.SWCommand"), + ENABLE_LINK(false, "$.enable()", null, "de.steamwar.bausystem.linkage.Enable"), + DISABLE_LINK(true, "$.disable()", null, "de.steamwar.bausystem.linkage.Disable"), + PLAIN(false), + LISTENER(false, "org.bukkit.Bukkit.getPluginManager().registerEvents($, de.steamwar.bausystem.BauSystem.getInstance())", null, "org.bukkit.event.Listener"), + UNLINK_LISTENER(true, "org.bukkit.event.HandlerList.unregisterAll($)", null, "org.bukkit.event.Listener"), + + // SPECIFIC + BAU_GUI_ITEM(false, "de.steamwar.bausystem.features.gui.BauGUI.addItem($)", "de.steamwar.bausystem.linkage.specific.BauGuiItem"), + SCRIPT_COMMAND(false, "de.steamwar.bausystem.features.script.ScriptExecutor.SPECIAL_COMMANDS.add($)", null, "de.steamwar.bausystem.features.script.SpecialCommand"), + CONFIG_CONVERTER(false, "de.steamwar.bausystem.configplayer.Config.addConfigConverter($)", null, "de.steamwar.bausystem.configplayer.ConfigConverter"), + SCOREBOARD(false, null, null, "de.steamwar.bausystem.linkage.specific.ScoreboardItem"), + PANZERN(false, "de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithmLazyInit.add($)", null, "de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithm"), + SMART_PLACE(false, "de.steamwar.bausystem.features.smartplace.SmartPlaceListener.add($)", null, "de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour"); + + final boolean unlink; + String toRun = null; + String className = null; + String interfaceName = null; + + LinkageType(boolean unlink, String toRun, String className) { + this.unlink = unlink; + this.toRun = toRun; + this.className = className; + } +} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/Linked.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/Linked.java similarity index 100% rename from BauSystem_Main/src/de/steamwar/bausystem/linkage/Linked.java rename to BauSystem_Linkage/src/de/steamwar/bausystem/linkage/Linked.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedInstance.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkedInstance.java similarity index 100% rename from BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkedInstance.java rename to BauSystem_Linkage/src/de/steamwar/bausystem/linkage/LinkedInstance.java diff --git a/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/ProcessorImplementation.java b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/ProcessorImplementation.java new file mode 100644 index 00000000..96cae2d3 --- /dev/null +++ b/BauSystem_Linkage/src/de/steamwar/bausystem/linkage/ProcessorImplementation.java @@ -0,0 +1,27 @@ +/* + * Copyright 2019,2020,2021 yoyosource + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package de.steamwar.bausystem.linkage; + +import org.atteo.classindex.IndexAnnotated; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@IndexAnnotated +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.TYPE}) +public @interface ProcessorImplementation { +} diff --git a/BauSystem_Main/build.gradle b/BauSystem_Main/build.gradle index 8a23e1a3..34c40b56 100644 --- a/BauSystem_Main/build.gradle +++ b/BauSystem_Main/build.gradle @@ -56,6 +56,9 @@ dependencies { annotationProcessor 'org.atteo.classindex:classindex:3.11' testAnnotationProcessor 'org.atteo.classindex:classindex:3.11' + implementation project(":BauSystem_Linkage") + annotationProcessor project(":BauSystem_Linkage") + compileOnly 'org.spigotmc:spigot-api:1.19-R0.1-SNAPSHOT' compileOnly 'com.mojang:authlib:1.5.25' compileOnly 'io.netty:netty-all:4.1.68.Final' diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauCommand.java index 8680f7bf..3e039a90 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/bau/BauCommand.java @@ -29,7 +29,7 @@ import org.bukkit.entity.Player; public class BauCommand extends SWCommand { @LinkedInstance - private InfoCommand infoCommand; + public InfoCommand infoCommand; public BauCommand() { super("bau", "b", "gs"); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/bau/InfoCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/bau/InfoCommand.java index 327e4903..58b3dc20 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/bau/InfoCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/bau/InfoCommand.java @@ -22,7 +22,7 @@ import static de.steamwar.bausystem.features.tpslimit.TPSWarpUtils.getTps; public class InfoCommand extends SWCommand { @LinkedInstance - private BauServer bauServer; + public BauServer bauServer; public InfoCommand() { super("bauinfo"); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java index 219b1b6f..a016fbd0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/detonator/DetonatorCommand.java @@ -54,7 +54,7 @@ public class DetonatorCommand extends SWCommand { return wand; } - protected DetonatorCommand() { + public DetonatorCommand() { super("detonator", "dt"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java index 344937cd..30ae286b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGUICommand.java @@ -29,7 +29,7 @@ import org.bukkit.entity.Player; @Linked(LinkageType.COMMAND) public class BauGUICommand extends SWCommand { - protected BauGUICommand() { + public BauGUICommand() { super("gui", "baugui", "g"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java index b319fecb..26b4024b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/hotbar/HotbarCommand.java @@ -30,7 +30,7 @@ import org.bukkit.inventory.ItemStack; @Linked(LinkageType.COMMAND) public class HotbarCommand extends SWCommand { - protected HotbarCommand() { + public HotbarCommand() { super("hotbar", "hb"); addDefaultHelpMessage("HOTBAR_HELP_GENERIC"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java index 11a77a5f..afafed99 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loader/LoaderCommand.java @@ -29,7 +29,7 @@ import org.bukkit.entity.Player; @Linked(LinkageType.COMMAND) public class LoaderCommand extends SWCommand { - protected LoaderCommand() { + public LoaderCommand() { super("loader", "autoloader", "al"); addDefaultHelpMessage("LOADER_HELP_OTHER"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerCommand.java index df52a419..29bf67f2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/loadtimer/LoadtimerCommand.java @@ -27,7 +27,7 @@ import org.bukkit.entity.Player; @Linked(LinkageType.COMMAND) public class LoadtimerCommand extends SWCommand { - protected LoadtimerCommand() { + public LoadtimerCommand() { super("loadtimer", "lt", "stopuhr"); addDefaultHelpMessage("LOADTIMER_HELP_OVERVIEW"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java index beb5fc4e..7d990231 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/ColorCommand.java @@ -40,7 +40,7 @@ import java.io.IOException; public class ColorCommand extends SWCommand { @LinkedInstance - private BauServer bauServer; + public BauServer bauServer; public ColorCommand() { super("color"); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java index dc0fa2ac..819db4e0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/RegionCommand.java @@ -50,10 +50,10 @@ import java.util.stream.Collectors; public class RegionCommand extends SWCommand { @LinkedInstance - private SelectCommand selectCommand; + public SelectCommand selectCommand; @LinkedInstance - private ColorCommand colorCommand; + public ColorCommand colorCommand; public RegionCommand() { super("region", "rg"); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java index b52dff50..ff1b0b9c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/TNTListener.java @@ -42,7 +42,7 @@ import java.util.concurrent.atomic.AtomicBoolean; public class TNTListener implements Listener { @LinkedInstance - private CustomScriptManager customScriptManager; + public CustomScriptManager customScriptManager; @EventHandler public void onExplode(EntityExplodeEvent event) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ResetBauGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ResetBauGuiItem.java index 336843fb..67d7c20d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ResetBauGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/ResetBauGuiItem.java @@ -37,7 +37,7 @@ import org.bukkit.inventory.ItemStack; public class ResetBauGuiItem extends BauGuiItem { @LinkedInstance - private ResetCommand resetCommand; + public ResetCommand resetCommand; public ResetBauGuiItem() { super(6); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java index f789ea78..ed9be915 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/region/items/TestblockBauGuiItem.java @@ -37,7 +37,7 @@ import org.bukkit.inventory.ItemStack; public class TestblockBauGuiItem extends BauGuiItem { @LinkedInstance - private TestblockCommand testblockCommand; + public TestblockCommand testblockCommand; public TestblockBauGuiItem() { super(5); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 89725037..e6fa0f49 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -25,7 +25,7 @@ public class ScriptCommand extends SWCommand { } @LinkedInstance - private CustomScriptManager customScriptManager = null; + public CustomScriptManager customScriptManager = null; private List loreBuilder(Player p, String... strings) { List result = new ArrayList<>(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java index e17e0b99..32c8d40c 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptEventListener.java @@ -47,7 +47,7 @@ import java.util.Set; public class ScriptEventListener implements Listener { @LinkedInstance - private CustomScriptManager customScriptManager; + public CustomScriptManager customScriptManager; private static final Map LAST_FS = new HashMap<>(); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java index 972cb23e..798c9d02 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/techhider/TechHiderCommand.java @@ -53,7 +53,7 @@ public class TechHiderCommand extends SWCommand implements Listener { private Map> hidden = new HashMap<>(); @LinkedInstance - private XrayCommand xrayCommand; + public XrayCommand xrayCommand; @Register(description = "TECHHIDER_HELP") public void toggleHider(Player player) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java index 33f7ac88..9b1589d9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/NoClipCommand.java @@ -72,7 +72,7 @@ public class NoClipCommand extends SWCommand implements Listener { private static final List NOCLIPS = new ArrayList<>(); private static final Map LAST_TICKS = new HashMap<>(); - protected NoClipCommand() { + public NoClipCommand() { super("noclip", "nc"); BiFunction first = (player, o) -> { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/SlotCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/SlotCommand.java index c6a3af49..c0dafe18 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/SlotCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/SlotCommand.java @@ -36,7 +36,7 @@ import java.util.Arrays; @Linked(LinkageType.COMMAND) public class SlotCommand extends SWCommand { - protected SlotCommand() { + public SlotCommand() { super("slot", "s"); addDefaultHelpMessage("OTHER_NOCLIP_SLOT_INFO"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/DeclutterBauGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/DeclutterBauGuiItem.java index 70e7296d..9ed5a67f 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/DeclutterBauGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/DeclutterBauGuiItem.java @@ -36,7 +36,7 @@ import org.bukkit.inventory.ItemStack; public class DeclutterBauGuiItem extends BauGuiItem { @LinkedInstance - private DeclutterCommand declutterCommand; + public DeclutterCommand declutterCommand; public DeclutterBauGuiItem() { super(30); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/KillAllBauGuiItem.java b/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/KillAllBauGuiItem.java index e31ea268..0e66a163 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/KillAllBauGuiItem.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/util/items/KillAllBauGuiItem.java @@ -39,7 +39,7 @@ import java.util.Arrays; public class KillAllBauGuiItem extends BauGuiItem { @LinkedInstance - private KillAllCommand killAllCommand; + public KillAllCommand killAllCommand; public KillAllBauGuiItem() { super(32); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpCommand.java index da0d816d..c2ae7528 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/warp/WarpCommand.java @@ -47,7 +47,7 @@ public class WarpCommand extends SWCommand implements Disable, Enable { "add", "create", "delete", "list", "info", "gui" }; - protected WarpCommand() { + public WarpCommand() { super("warp"); } @@ -132,7 +132,7 @@ public class WarpCommand extends SWCommand implements Disable, Enable { @Linked(LinkageType.COMMAND) public static class WarpsLink extends SWCommand { - protected WarpsLink() { + public WarpsLink() { super("warps"); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java index abd97ee1..88f2cae5 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/xray/XrayCommand.java @@ -60,7 +60,7 @@ public class XrayCommand extends SWCommand implements Listener { private Map> hidden = new HashMap<>(); @LinkedInstance - private TechHiderCommand techHiderCommand; + public TechHiderCommand techHiderCommand; @Register(description = "XRAY_HELP") public void toggleHider(Player player) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java deleted file mode 100644 index 5d2ebef0..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageType.java +++ /dev/null @@ -1,69 +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 . - */ - -package de.steamwar.bausystem.linkage; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.configplayer.Config; -import de.steamwar.bausystem.configplayer.ConfigConverter; -import de.steamwar.bausystem.features.gui.BauGUI; -import de.steamwar.bausystem.features.script.ScriptExecutor; -import de.steamwar.bausystem.features.script.SpecialCommand; -import de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithm; -import de.steamwar.bausystem.features.slaves.panzern.PanzernAlgorithmLazyInit; -import de.steamwar.bausystem.features.smartplace.SmartPlaceBehaviour; -import de.steamwar.bausystem.features.smartplace.SmartPlaceListener; -import de.steamwar.bausystem.linkage.specific.BauGuiItem; -import de.steamwar.bausystem.linkage.specific.ScoreboardItem; -import de.steamwar.command.SWCommand; -import lombok.AllArgsConstructor; -import lombok.Getter; -import lombok.RequiredArgsConstructor; -import org.bukkit.Bukkit; -import org.bukkit.event.HandlerList; -import org.bukkit.event.Listener; - -import java.util.function.Consumer; -import java.util.function.Predicate; - -@RequiredArgsConstructor -public enum LinkageType { - - // NORMAL - COMMAND(false, SWCommand.class::isAssignableFrom, o -> ((SWCommand) o).setMessage(BauSystem.MESSAGE)), - ENABLE_LINK(false, Enable.class::isAssignableFrom, o -> ((Enable) o).enable()), - DISABLE_LINK(true, Disable.class::isAssignableFrom, o -> ((Disable) o).disable()), - PLAIN(false, clazz -> true, o -> {}), - LISTENER(false, Listener.class::isAssignableFrom, o -> Bukkit.getPluginManager().registerEvents((Listener) o, BauSystem.getInstance())), - UNLINK_LISTENER(true, Listener.class::isAssignableFrom, o -> HandlerList.unregisterAll((Listener) o)), - - // SPECIFIC - BAU_GUI_ITEM(false, BauGuiItem.class::isAssignableFrom, o -> BauGUI.addItem((BauGuiItem) o)), - SCRIPT_COMMAND(false, SpecialCommand.class::isAssignableFrom, o -> ScriptExecutor.SPECIAL_COMMANDS.add((SpecialCommand) o)), - CONFIG_CONVERTER(false, ConfigConverter.class::isAssignableFrom, o -> Config.addConfigConverter((ConfigConverter) o)), - SCOREBOARD(false, ScoreboardItem.class::isAssignableFrom, o -> {}), - PANZERN(false, PanzernAlgorithm.class::isAssignableFrom, o -> PanzernAlgorithmLazyInit.add((PanzernAlgorithm) o)), - SMART_PLACE(false, SmartPlaceBehaviour.class::isAssignableFrom, o -> SmartPlaceListener.add((SmartPlaceBehaviour) o)); - - final boolean unlink; - - final Predicate> linkagePredicate; - - final Consumer linkageConsumer; -} \ No newline at end of file diff --git a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java b/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java deleted file mode 100644 index 298f94c7..00000000 --- a/BauSystem_Main/src/de/steamwar/bausystem/linkage/LinkageUtils.java +++ /dev/null @@ -1,139 +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 . - */ - -package de.steamwar.bausystem.linkage; - -import de.steamwar.bausystem.BauSystem; -import de.steamwar.bausystem.configplayer.Config; -import de.steamwar.bausystem.utils.FlatteningWrapper; -import de.steamwar.bausystem.utils.NMSWrapper; -import de.steamwar.bausystem.utils.ProtocolWrapper; -import lombok.experimental.UtilityClass; -import org.bukkit.Bukkit; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.util.*; -import java.util.logging.Level; -import java.util.stream.Collectors; - -@UtilityClass -public class LinkageUtils { - - private Map, Object> objectMap = new HashMap<>(); - private List fieldsToLink = new ArrayList<>(); - - { - objectMap.put(Config.class, Config.getInstance()); - objectMap.put(BauSystem.class, BauSystem.getInstance()); - objectMap.put(FlatteningWrapper.class, FlatteningWrapper.impl); - objectMap.put(NMSWrapper.class, NMSWrapper.impl); - objectMap.put(ProtocolWrapper.class, ProtocolWrapper.impl); - } - - public void link() { - internalLinkOrUnlink(false, Linked.class); - internalLinkFields(); - } - - public void unlink() { - internalLinkOrUnlink(true, Linked.class); - } - - private void internalLinkOrUnlink(boolean unlink, Class toLink) { - try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(BauSystem.class.getResourceAsStream("/de.steamwar.bausystem/" + toLink.getTypeName().replace("$", "."))))) { - bufferedReader.lines().forEach(s -> { - try { - linkOrUnlink(Class.forName(s), unlink); - } catch (ClassNotFoundException e) { - // ignored - } catch (Exception e) { - Bukkit.getLogger().log(Level.WARNING, e.getMessage(), e); - } - }); - } catch (IOException e) { - Bukkit.shutdown(); - } catch (NullPointerException e) { - // Ignored - } - } - - private void internalLinkFields() { - for (Field field : fieldsToLink) { - LinkedInstance linkedInstance = field.getDeclaredAnnotation(LinkedInstance.class); - if (linkedInstance == null) { - continue; - } - Object object = objectMap.getOrDefault(field.getType(), null); - Object source = objectMap.getOrDefault(field.getDeclaringClass(), null); - try { - field.setAccessible(true); - field.set(source, object); - } catch (IllegalAccessException e) { - // Ignored - } - } - } - - private void linkOrUnlink(Class clazz, boolean unlink) { - Linked[] linkages = clazz.getDeclaredAnnotationsByType(Linked.class); - if (linkages.length == 0) { - return; - } - - List linkageTypeList = Arrays.stream(linkages).map(Linked::value).filter(linkageType -> linkageType.unlink == unlink).filter(linkageType -> linkageType.linkagePredicate.test(clazz)).collect(Collectors.toList()); - if (linkageTypeList.isEmpty()) { - return; - } - - if (unlink) { - Object object = objectMap.remove(clazz); - if (object == null) { - return; - } - linkageTypeList.forEach(linkageType -> linkageType.linkageConsumer.accept(object)); - } else { - Object object = constructInstance(clazz); - objectMap.put(clazz, object); - linkageTypeList.forEach(linkageType -> linkageType.linkageConsumer.accept(object)); - - for (Field field : clazz.getDeclaredFields()) { - LinkedInstance linkedInstance = field.getDeclaredAnnotation(LinkedInstance.class); - if (linkedInstance == null) { - continue; - } - fieldsToLink.add(field); - } - } - } - - private Object constructInstance(Class clazz) { - try { - Constructor constructor = clazz.getDeclaredConstructor(); - constructor.setAccessible(true); //NOSONAR - return constructor.newInstance(); - } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException | InstantiationException e) { - throw new SecurityException(e.getMessage(), e.getCause()); - } - } -} \ No newline at end of file