From 5ab04d051873747d3f8a1b4a7e9834e519567caf Mon Sep 17 00:00:00 2001 From: YoyoNow Date: Sun, 26 Oct 2025 13:34:25 +0100 Subject: [PATCH] Add cache for GameModeConfig --- .../region/fixed/FixedGlobalRegion.java | 7 +-- .../bausystem/region/fixed/FixedRegion.java | 9 ++-- .../src/de/steamwar/sql/GameModeConfig.java | 46 ++++++++++++++++++- .../src/de/steamwar/fightsystem/Config.java | 2 +- .../schematicsystem/SchematicSystem.java | 26 ++--------- .../src/de/steamwar/sql/SQLWrapperImpl.java | 2 +- .../src/de/steamwar/sql/SQLWrapperImpl.java | 2 +- .../de/steamwar/velocitycore/ArenaMode.java | 2 +- .../src/de/steamwar/sql/SQLWrapperImpl.kt | 2 +- 9 files changed, 61 insertions(+), 37 deletions(-) diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java index cac519e0..b5da64f5 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedGlobalRegion.java @@ -22,7 +22,6 @@ package de.steamwar.bausystem.region.fixed; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.bausystem.region.*; import de.steamwar.bausystem.utils.PasteBuilder; -import de.steamwar.data.GameModeConfigUtils; import de.steamwar.sql.GameModeConfig; import lombok.NonNull; import lombok.Setter; @@ -93,8 +92,6 @@ public final class FixedGlobalRegion implements Region { } }; - private static final GameModeConfig GLOBAL_CONFIG = new GameModeConfig<>(null, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear); - private FixedGlobalRegion() { } @@ -129,8 +126,8 @@ public final class FixedGlobalRegion implements Region { } @Override - public @NonNull GameModeConfig getGameModeConfig() { - return GLOBAL_CONFIG; + public @NonNull GameModeConfig getGameModeConfig() { + return GameModeConfig.getDefaults(); } @Override diff --git a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java index ce48b5b6..0d8c03f5 100644 --- a/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java +++ b/BauSystem/BauSystem_RegionFixed/src/de/steamwar/bausystem/region/fixed/FixedRegion.java @@ -28,7 +28,6 @@ import de.steamwar.bausystem.utils.FlatteningWrapper; import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.core.Core; -import de.steamwar.data.GameModeConfigUtils; import de.steamwar.sql.GameModeConfig; import de.steamwar.sql.SchematicType; import lombok.NonNull; @@ -339,7 +338,11 @@ public class FixedRegion implements Region { break; } } - this.gameModeConfig = new GameModeConfig<>(found, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear); + if (found == null) { + this.gameModeConfig = GameModeConfig.getDefaults(); + } else { + this.gameModeConfig = GameModeConfig.getByFileName(found); + } this.regionData = new RegionData.RegionDataImpl(regionData, WorldData::write); } @@ -374,7 +377,7 @@ public class FixedRegion implements Region { } @Override - public @NonNull GameModeConfig getGameModeConfig() { + public @NonNull GameModeConfig getGameModeConfig() { return gameModeConfig; } diff --git a/CommonCore/SQL/src/de/steamwar/sql/GameModeConfig.java b/CommonCore/SQL/src/de/steamwar/sql/GameModeConfig.java index 071db180..2bef6599 100644 --- a/CommonCore/SQL/src/de/steamwar/sql/GameModeConfig.java +++ b/CommonCore/SQL/src/de/steamwar/sql/GameModeConfig.java @@ -34,6 +34,28 @@ import java.util.stream.Collectors; @ToString public final class GameModeConfig { + private static final Map> byFileName = new HashMap<>(); + private static final Map> byGameName = new HashMap<>(); + private static final Map> bySchematicType = new HashMap<>(); + + public static GameModeConfig getByFileName(File file) { + return (GameModeConfig) byFileName.get(file.getName()); + } + + public static GameModeConfig getByGameName(String gameName) { + return (GameModeConfig) byGameName.get(gameName); + } + + public static GameModeConfig getBySchematicType(SchematicType schematicType) { + return (GameModeConfig) bySchematicType.get(schematicType); + } + + private static final GameModeConfig DEFAULTS = SQLWrapper.impl.loadGameModeConfig(null); + + public static GameModeConfig getDefaults() { + return (GameModeConfig) DEFAULTS; + } + public static final Function ToString = Function.identity(); public static final Function ToStaticWarGear = __ -> "WarGear"; public static final Function ToInternalName = file -> file.getName().replace(".yml", ""); @@ -145,7 +167,7 @@ public final class GameModeConfig { } } - public GameModeConfig(File file, Function materialMapper, Function winconditionMapper, Function defaultGameName) { + public GameModeConfig(File file, Function materialMapper, Function winconditionMapper, Function defaultGameName, boolean cacheInstance) { YMLWrapper loader = new YMLWrapper<>(file, materialMapper, winconditionMapper); configFile = file; @@ -182,7 +204,20 @@ public final class GameModeConfig { Arena = new ArenaConfig(loader.with("Arena"), Schematic.Size, EnterStages); - if (Schematic.Type != null) return; + if (cacheInstance) { + byFileName.put(configFile.getName(), (GameModeConfig) this); + byGameName.put(GameName, (GameModeConfig) this); + } + + if (Schematic.Type != null) { + if (cacheInstance) { + bySchematicType.put(Schematic.Type, (GameModeConfig) this); + if (Schematic.Type.checkType() != null) { + bySchematicType.put(Schematic.Type.checkType(), (GameModeConfig) this); + } + } + return; + } if (!Schematic.loaded) return; String Schematic_Type = loader.with("Schematic").getString("Type", "Normal"); if (Schematic_Type.equals("Normal")) return; @@ -197,6 +232,13 @@ public final class GameModeConfig { SchematicType current = new SchematicType(Schematic_Type, Schematic_Shortcut, Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, Deadline, Schematic.ManualCheck); try { Schematic_TypeField.set(Schematic, current); + + if (cacheInstance) { + bySchematicType.put(Schematic.Type, (GameModeConfig) this); + if (Schematic.Type.checkType() != null) { + bySchematicType.put(Schematic.Type.checkType(), (GameModeConfig) this); + } + } } catch (IllegalAccessException e) { throw new SecurityException(e.getMessage(), e); } diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/Config.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/Config.java index 3b8d2378..a5c89bca 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/Config.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/Config.java @@ -109,7 +109,7 @@ public class Config { Bukkit.getLogger().log(Level.SEVERE, "Arenaconfig fehlt!"); Bukkit.shutdown(); } - GameModeConfig = new GameModeConfig<>(new File(FightSystem.getPlugin().getDataFolder(), configFile), GameModeConfigUtils.ToMaterial, Winconditions::valueOf, de.steamwar.sql.GameModeConfig.ToStaticWarGear); + GameModeConfig = new GameModeConfig<>(new File(FightSystem.getPlugin().getDataFolder(), configFile), GameModeConfigUtils.ToMaterial, Winconditions::valueOf, de.steamwar.sql.GameModeConfig.ToStaticWarGear, false); File worldConfigFile = new File(world.getWorldFolder(), "config.yml"); if(!worldConfigFile.exists()) { diff --git a/SchematicSystem/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java b/SchematicSystem/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java index b75ddbdf..3687a52b 100644 --- a/SchematicSystem/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java +++ b/SchematicSystem/SchematicSystem_Core/src/de/steamwar/schematicsystem/SchematicSystem.java @@ -19,48 +19,30 @@ package de.steamwar.schematicsystem; -import de.steamwar.sql.GameModeConfig; -import de.steamwar.data.GameModeConfigUtils; import de.steamwar.linkage.AbstractLinker; import de.steamwar.linkage.SpigotLinker; import de.steamwar.message.Message; +import de.steamwar.sql.GameModeConfig; import de.steamwar.sql.SchematicType; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.plugin.java.JavaPlugin; -import java.io.File; -import java.util.HashMap; -import java.util.Map; - public class SchematicSystem extends JavaPlugin { public static final Message MESSAGE = new Message("SchematicSystem", SchematicSystem.class.getClassLoader()); private static SchematicSystem instance; - private static final Map> types = new HashMap<>(); - public static GameModeConfig getGameModeConfig(SchematicType type){ - return types.get(type); + GameModeConfig gameModeConfig = GameModeConfig.getBySchematicType(type); + if (gameModeConfig.CheckQuestions.isEmpty() && gameModeConfig.Schematic.ManualCheck) return null; + return gameModeConfig; } @Override public void onEnable() { instance = this; - File folder = new File(this.getDataFolder().getParentFile(), "FightSystem"); - if(folder.exists()) { - for(File configFile : folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))) { - GameModeConfig gameModeConfig = new GameModeConfig<>(configFile, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear); - if (gameModeConfig.CheckQuestions.isEmpty() && gameModeConfig.Schematic.ManualCheck) - continue; - types.put(gameModeConfig.Schematic.Type, gameModeConfig); - if (gameModeConfig.Schematic.Type.checkType() != null) { - types.put(gameModeConfig.Schematic.Type.checkType(), gameModeConfig); - } - } - } - SpigotLinker spigotLinker = new SpigotLinker(this, MESSAGE); try { spigotLinker.link(); diff --git a/SpigotCore/SpigotCore_Main/src/de/steamwar/sql/SQLWrapperImpl.java b/SpigotCore/SpigotCore_Main/src/de/steamwar/sql/SQLWrapperImpl.java index 1aa3f7f5..39e2cef6 100644 --- a/SpigotCore/SpigotCore_Main/src/de/steamwar/sql/SQLWrapperImpl.java +++ b/SpigotCore/SpigotCore_Main/src/de/steamwar/sql/SQLWrapperImpl.java @@ -37,7 +37,7 @@ public class SQLWrapperImpl implements SQLWrapper { @Override public GameModeConfig loadGameModeConfig(File file) { - return new GameModeConfig<>(file, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear); + return new GameModeConfig<>(file, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear, true); } private static final String SERVER_VERSION = Bukkit.getServer().getVersion(); diff --git a/VelocityCore/src/de/steamwar/sql/SQLWrapperImpl.java b/VelocityCore/src/de/steamwar/sql/SQLWrapperImpl.java index 3ad15026..8e469d20 100644 --- a/VelocityCore/src/de/steamwar/sql/SQLWrapperImpl.java +++ b/VelocityCore/src/de/steamwar/sql/SQLWrapperImpl.java @@ -35,7 +35,7 @@ public class SQLWrapperImpl implements SQLWrapper { @Override public GameModeConfig loadGameModeConfig(File file) { - return new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName); + return new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName, true); } @Override diff --git a/VelocityCore/src/de/steamwar/velocitycore/ArenaMode.java b/VelocityCore/src/de/steamwar/velocitycore/ArenaMode.java index 9707bdd9..87e2bf92 100644 --- a/VelocityCore/src/de/steamwar/velocitycore/ArenaMode.java +++ b/VelocityCore/src/de/steamwar/velocitycore/ArenaMode.java @@ -51,7 +51,7 @@ public class ArenaMode { return; for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) { - GameModeConfig gameModeConfig = new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName); + GameModeConfig gameModeConfig = new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName, false); if (!gameModeConfig.Server.loaded) continue; allModes.add(gameModeConfig); diff --git a/WebsiteBackend/src/de/steamwar/sql/SQLWrapperImpl.kt b/WebsiteBackend/src/de/steamwar/sql/SQLWrapperImpl.kt index be41c8ab..0e843c25 100644 --- a/WebsiteBackend/src/de/steamwar/sql/SQLWrapperImpl.kt +++ b/WebsiteBackend/src/de/steamwar/sql/SQLWrapperImpl.kt @@ -27,7 +27,7 @@ class SQLWrapperImpl: SQLWrapper { } override fun loadGameModeConfig(file: File): GameModeConfig { - return GameModeConfig(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear) + return GameModeConfig(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear, true) } override fun additionalExceptionMetadata(builder: StringBuilder) {