SteamWar/BauSystem2.0
Archiviert
12
0

Update generated code improve startup time with proper lazy init
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2022-08-27 17:45:12 +02:00
Ursprung 80eeaf533e
Commit 3b964a2e18
10 geänderte Dateien mit 157 neuen und 88 gelöschten Zeilen

Datei anzeigen

@ -58,11 +58,13 @@ public class LinkageProcessor extends AbstractProcessor {
if (processed) return false;
processed = true;
List<String> fields = new ArrayList<>();
List<String> linkLines = new ArrayList<>();
List<String> unlinkLines = 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;
@ -73,30 +75,18 @@ public class LinkageProcessor extends AbstractProcessor {
continue;
}
System.out.println("Found element: " + typeElement.getQualifiedName().toString());
if (linkeds.length > 1) {
neededFields.put(typeElement.getQualifiedName().toString(), typeElement);
continue;
}
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<VariableElement> variableElements = typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.FIELD).map(e -> (VariableElement) e).filter(e -> {
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);
@ -110,30 +100,100 @@ public class LinkageProcessor extends AbstractProcessor {
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());
neededFields.put(typeElement.getQualifiedName().toString(), typeElement);
TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement();
neededFields.put(fieldType.getQualifiedName().toString(), fieldType);
staticLines.add(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields).toString());
}
}
neededFields.forEach((s, typeElement) -> {
fields.add(typeElement.getQualifiedName().toString() + " " + typeElement.getSimpleName().toString());
});
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());
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;
}
}
linkLines.computeIfAbsent(linkageType, ignore -> new ArrayList<>()).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 final " + s + ";\n");
writer.write(" private static " + s + ";\n");
}
writer.write("\n");
writer.write(" public static void link() {\n");
for (String s : linkLines) {
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 unlink() {\n");
for (String s : unlinkLines) {
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");
}
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() + "()";
}
}

Datei anzeigen

@ -22,34 +22,39 @@ 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"),
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(),
LISTENER("org.bukkit.Bukkit.getPluginManager().registerEvents($, de.steamwar.bausystem.BauSystem.getInstance())", null, "org.bukkit.event.Listener"),
UNLINK_LISTENER("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");
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");
final boolean unlink;
String toRun = null;
String className = null;
String interfaceName = null;
LinkageType(boolean unlink, String toRun, String className) {
this.unlink = unlink;
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;
}
}

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.bausystem;
import com.comphenix.tinyprotocol.TinyProtocol;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.region.loader.PrototypeLoader;
import de.steamwar.bausystem.region.loader.RegionLoader;
@ -55,7 +56,6 @@ public class BauSystem extends JavaPlugin implements Listener {
@Override
public void onEnable() {
long time = System.currentTimeMillis();
world = Bukkit.getWorlds().get(0);
fixBauSystem();
@ -79,9 +79,12 @@ public class BauSystem extends JavaPlugin implements Listener {
new Updater(PrototypeLoader.file, PrototypeLoader::load);
new Updater(RegionLoader.file, RegionLoader::load);
LinkageUtils.link();
getLogger().info("[BauSystem] Enabled in " + (System.currentTimeMillis() - time) + "ms");
LinkageUtils.run(LinkageType.PLAIN);
LinkageUtils.run(LinkageType.COMMAND);
LinkageUtils.run(LinkageType.ENABLE_LINK);
LinkageUtils.run(LinkageType.LISTENER);
LinkageUtils.run(LinkageType.CONFIG_CONVERTER);
LinkageUtils.run(LinkageType.SCOREBOARD);
// This could disable any watchdog stuff. We need to investigate if this is a problem.
/*
@ -103,7 +106,8 @@ public class BauSystem extends JavaPlugin implements Listener {
@Override
public void onDisable() {
LinkageUtils.unlink();
LinkageUtils.run(LinkageType.DISABLE_LINK);
LinkageUtils.run(LinkageType.UNLINK_LISTENER);
WorldData.write();
Config.getInstance().saveAll();

Datei anzeigen

@ -23,9 +23,10 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.Permission;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.gui.editor.BauGuiMapping;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.linkage.specific.BauGuiItem;
import de.steamwar.inventory.SWInventory;
import lombok.Getter;
import lombok.experimental.UtilityClass;
import net.md_5.bungee.api.ChatColor;
import org.bukkit.Material;
@ -38,9 +39,13 @@ import java.util.*;
@UtilityClass
public class BauGUI {
@Getter
private static final Map<Integer, BauGuiItem> ITEMS = new HashMap<>();
public static Map<Integer, BauGuiItem> getITEMS() {
LinkageUtils.run(LinkageType.BAU_GUI_ITEM);
return ITEMS;
}
private static final Set<Player> OPEN_INVS = new HashSet<>();
private static boolean updating = false;
@ -65,7 +70,7 @@ public class BauGUI {
}
BauGuiMapping mapping = BauGuiMapping.getGuiMapping(p);
SWInventory inv = new SWInventory(p, mapping.getSize(), BauSystem.MESSAGE.parse("GUI_NAME", p));
ITEMS.values().forEach(item -> {
getITEMS().values().forEach(item -> {
if (!mapping.isShown(item.getId())) {
return;
}

Datei anzeigen

@ -4,6 +4,8 @@ import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.features.script.variables.Constants;
import de.steamwar.bausystem.features.script.variables.Context;
import de.steamwar.bausystem.features.script.variables.Value;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.LinkageUtils;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.Bukkit;
@ -16,6 +18,10 @@ import java.util.logging.Level;
public final class ScriptExecutor {
static {
LinkageUtils.run(LinkageType.SCRIPT_COMMAND);
}
public static final Set<SpecialCommand> SPECIAL_COMMANDS = new HashSet<>();
private String specialCommand = null;

Datei anzeigen

@ -24,6 +24,8 @@ import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
import de.steamwar.bausystem.features.slaves.WorldEditUtils;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.LinkageUtils;
import lombok.Getter;
import lombok.SneakyThrows;
import org.bukkit.Location;
@ -37,6 +39,14 @@ import java.util.*;
public class Panzern {
static {
LinkageUtils.run(LinkageType.PANZERN);
}
private static List<PanzernAlgorithm> panzernAlgorithmList = new ArrayList<>();
public static void add(PanzernAlgorithm panzernAlgorithm) {
panzernAlgorithmList.add(panzernAlgorithm);
}
private static final BlockFace[] BLOCK_FACES = new BlockFace[]{BlockFace.NORTH, BlockFace.SOUTH, BlockFace.EAST, BlockFace.WEST, BlockFace.UP, BlockFace.DOWN};
private Set<Location> current = new LinkedHashSet<>();
@ -98,7 +108,7 @@ public class Panzern {
Block currentBlock = world.getBlockAt(toCheck);
PanzernResult panzernResult = PanzernResult.DEFAULT;
for (PanzernAlgorithm panzernAlgorithm : PanzernAlgorithmLazyInit.panzernAlgorithmList) {
for (PanzernAlgorithm panzernAlgorithm : panzernAlgorithmList) {
PanzernResult temp = panzernAlgorithm.check(currentBlock, adjacent, adjacentMaterials);
if (temp != null && temp != PanzernResult.DEFAULT) {
panzernResult = temp;

Datei anzeigen

@ -1,35 +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.features.slaves.panzern;
import lombok.experimental.UtilityClass;
import java.util.ArrayList;
import java.util.List;
@UtilityClass
public class PanzernAlgorithmLazyInit {
static List<PanzernAlgorithm> panzernAlgorithmList = new ArrayList<>();
public static void add(PanzernAlgorithm panzernAlgorithm) {
panzernAlgorithmList.add(panzernAlgorithm);
}
}

Datei anzeigen

@ -21,6 +21,7 @@ package de.steamwar.bausystem.features.smartplace;
import de.steamwar.bausystem.configplayer.Config;
import de.steamwar.bausystem.linkage.LinkageType;
import de.steamwar.bausystem.linkage.LinkageUtils;
import de.steamwar.bausystem.linkage.Linked;
import org.bukkit.GameMode;
import org.bukkit.block.data.BlockData;
@ -47,6 +48,7 @@ public class SmartPlaceListener implements Listener {
@EventHandler
public void onBlockPlace(BlockPlaceEvent event) {
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
LinkageUtils.run(LinkageType.SMART_PLACE);
SmartPlaceBehaviour.SmartPlaceResult smartPlaceResult = SmartPlaceBehaviour.SmartPlaceResult.IGNORED;
for (SmartPlaceBehaviour smartPlaceBehaviour : smartPlaceBehaviours) {
if (smartPlaceBehaviour.getType() == SmartPlaceBehaviour.SmartPlaceType.PLACE) {
@ -61,6 +63,7 @@ public class SmartPlaceListener implements Listener {
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!Config.getInstance().get(event.getPlayer()).getPlainValueOrDefault("smartPlace", false)) return;
LinkageUtils.run(LinkageType.SMART_PLACE);
if (event.getAction() != Action.RIGHT_CLICK_BLOCK) return;
if (event.getPlayer().getGameMode() == GameMode.SPECTATOR) return;

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.region.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.Prototype;
import lombok.experimental.UtilityClass;
import org.bukkit.Bukkit;
@ -39,9 +40,11 @@ public class PrototypeLoader {
public static final File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "prototypes.yapion");
public void load() {
long time = System.currentTimeMillis();
YAPIONObject yapionObject = null;
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
yapionObject = YAPIONParser.parse(bufferedInputStream);
BauSystem.getInstance().getLogger().info("4.1.1 " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
throw new SecurityException(e.getMessage(), e);
}
@ -49,6 +52,7 @@ public class PrototypeLoader {
if (loaded != null && new YAPIONDiff(loaded, yapionObject).getDiffs().stream().anyMatch(DiffDelete.class::isInstance)) {
throw new SecurityException("Version was not the specified version needed.");
}
BauSystem.getInstance().getLogger().info("4.1.2 " + (System.currentTimeMillis() - time) + "ms");
loaded = yapionObject;
yapionObject.forEach((key, yapionAnyType) -> {
@ -56,5 +60,6 @@ public class PrototypeLoader {
new Prototype(key, (YAPIONObject) yapionAnyType);
}
});
BauSystem.getInstance().getLogger().info("4.1.3 " + (System.currentTimeMillis() - time) + "ms");
}
}

Datei anzeigen

@ -19,6 +19,7 @@
package de.steamwar.bausystem.region.loader;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.FlagStorage;
import de.steamwar.bausystem.region.GlobalRegion;
import de.steamwar.bausystem.region.Prototype;
@ -43,9 +44,11 @@ public class RegionLoader {
public static final File file = new File(Bukkit.getWorlds().get(0).getWorldFolder(), "regions.yapion");
public void load() {
long time = System.currentTimeMillis();
YAPIONObject yapionObject = null;
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(file))) {
yapionObject = YAPIONParser.parse(bufferedInputStream);
BauSystem.getInstance().getLogger().info("4.2.1 " + (System.currentTimeMillis() - time) + "ms");
} catch (IOException e) {
throw new SecurityException(e.getMessage(), e);
}
@ -53,6 +56,7 @@ public class RegionLoader {
if (loaded != null && new YAPIONDiff(loaded, yapionObject).getDiffs().stream().anyMatch(diffBase -> !(diffBase instanceof DiffChange))) {
throw new SecurityException("Version was not the specified version needed.");
}
BauSystem.getInstance().getLogger().info("4.2.2 " + (System.currentTimeMillis() - time) + "ms");
loaded = yapionObject;
YAPIONObject optionsYapionObject = WorldData.getRegionsData();
@ -74,6 +78,7 @@ public class RegionLoader {
Prototype.generateRegion(key, regionConfig, regionData);
});
BauSystem.getInstance().getLogger().info("4.2.3 " + (System.currentTimeMillis() - time) + "ms");
YAPIONObject globalOptions = optionsYapionObject.getObject("global");
if (globalOptions == null) {
@ -87,5 +92,6 @@ public class RegionLoader {
flagStorage = new FlagStorage();
}
new GlobalRegion(flagStorage, globalOptions);
BauSystem.getInstance().getLogger().info("4.2.4 " + (System.currentTimeMillis() - time) + "ms");
}
}