Add cache for GameModeConfig

This commit is contained in:
2025-10-26 13:34:25 +01:00
parent 79ebdcea85
commit 5ab04d0518
9 changed files with 61 additions and 37 deletions
@@ -22,7 +22,6 @@ package de.steamwar.bausystem.region.fixed;
import com.sk89q.worldedit.extent.clipboard.Clipboard; import com.sk89q.worldedit.extent.clipboard.Clipboard;
import de.steamwar.bausystem.region.*; import de.steamwar.bausystem.region.*;
import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.data.GameModeConfigUtils;
import de.steamwar.sql.GameModeConfig; import de.steamwar.sql.GameModeConfig;
import lombok.NonNull; import lombok.NonNull;
import lombok.Setter; import lombok.Setter;
@@ -93,8 +92,6 @@ public final class FixedGlobalRegion implements Region {
} }
}; };
private static final GameModeConfig<Material, String> GLOBAL_CONFIG = new GameModeConfig<>(null, GameModeConfigUtils.ToMaterial, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear);
private FixedGlobalRegion() { private FixedGlobalRegion() {
} }
@@ -129,8 +126,8 @@ public final class FixedGlobalRegion implements Region {
} }
@Override @Override
public @NonNull GameModeConfig getGameModeConfig() { public @NonNull GameModeConfig<Material, String> getGameModeConfig() {
return GLOBAL_CONFIG; return GameModeConfig.getDefaults();
} }
@Override @Override
@@ -28,7 +28,6 @@ import de.steamwar.bausystem.utils.FlatteningWrapper;
import de.steamwar.bausystem.utils.PasteBuilder; import de.steamwar.bausystem.utils.PasteBuilder;
import de.steamwar.bausystem.worlddata.WorldData; import de.steamwar.bausystem.worlddata.WorldData;
import de.steamwar.core.Core; import de.steamwar.core.Core;
import de.steamwar.data.GameModeConfigUtils;
import de.steamwar.sql.GameModeConfig; import de.steamwar.sql.GameModeConfig;
import de.steamwar.sql.SchematicType; import de.steamwar.sql.SchematicType;
import lombok.NonNull; import lombok.NonNull;
@@ -339,7 +338,11 @@ public class FixedRegion implements Region {
break; 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); this.regionData = new RegionData.RegionDataImpl(regionData, WorldData::write);
} }
@@ -374,7 +377,7 @@ public class FixedRegion implements Region {
} }
@Override @Override
public @NonNull GameModeConfig getGameModeConfig() { public @NonNull GameModeConfig<Material, String> getGameModeConfig() {
return gameModeConfig; return gameModeConfig;
} }
@@ -34,6 +34,28 @@ import java.util.stream.Collectors;
@ToString @ToString
public final class GameModeConfig<M, W> { public final class GameModeConfig<M, W> {
private static final Map<String, GameModeConfig<?, String>> byFileName = new HashMap<>();
private static final Map<String, GameModeConfig<?, String>> byGameName = new HashMap<>();
private static final Map<SchematicType, GameModeConfig<?, String>> bySchematicType = new HashMap<>();
public static <M> GameModeConfig<M, String> getByFileName(File file) {
return (GameModeConfig<M, String>) byFileName.get(file.getName());
}
public static <M> GameModeConfig<M, String> getByGameName(String gameName) {
return (GameModeConfig<M, String>) byGameName.get(gameName);
}
public static <M> GameModeConfig<M, String> getBySchematicType(SchematicType schematicType) {
return (GameModeConfig<M, String>) bySchematicType.get(schematicType);
}
private static final GameModeConfig<?, String> DEFAULTS = SQLWrapper.impl.loadGameModeConfig(null);
public static <M> GameModeConfig<M, String> getDefaults() {
return (GameModeConfig<M, String>) DEFAULTS;
}
public static final Function<String, String> ToString = Function.identity(); public static final Function<String, String> ToString = Function.identity();
public static final Function<File, String> ToStaticWarGear = __ -> "WarGear"; public static final Function<File, String> ToStaticWarGear = __ -> "WarGear";
public static final Function<File, String> ToInternalName = file -> file.getName().replace(".yml", ""); public static final Function<File, String> ToInternalName = file -> file.getName().replace(".yml", "");
@@ -145,7 +167,7 @@ public final class GameModeConfig<M, W> {
} }
} }
public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName) { public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName, boolean cacheInstance) {
YMLWrapper<M, W> loader = new YMLWrapper<>(file, materialMapper, winconditionMapper); YMLWrapper<M, W> loader = new YMLWrapper<>(file, materialMapper, winconditionMapper);
configFile = file; configFile = file;
@@ -182,7 +204,20 @@ public final class GameModeConfig<M, W> {
Arena = new ArenaConfig(loader.with("Arena"), Schematic.Size, EnterStages); 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; if (!Schematic.loaded) return;
String Schematic_Type = loader.with("Schematic").getString("Type", "Normal"); String Schematic_Type = loader.with("Schematic").getString("Type", "Normal");
if (Schematic_Type.equals("Normal")) return; if (Schematic_Type.equals("Normal")) return;
@@ -197,6 +232,13 @@ public final class GameModeConfig<M, W> {
SchematicType current = new SchematicType(Schematic_Type, Schematic_Shortcut, Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, Deadline, Schematic.ManualCheck); SchematicType current = new SchematicType(Schematic_Type, Schematic_Shortcut, Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, Deadline, Schematic.ManualCheck);
try { try {
Schematic_TypeField.set(Schematic, current); 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) { } catch (IllegalAccessException e) {
throw new SecurityException(e.getMessage(), e); throw new SecurityException(e.getMessage(), e);
} }
@@ -109,7 +109,7 @@ public class Config {
Bukkit.getLogger().log(Level.SEVERE, "Arenaconfig fehlt!"); Bukkit.getLogger().log(Level.SEVERE, "Arenaconfig fehlt!");
Bukkit.shutdown(); 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"); File worldConfigFile = new File(world.getWorldFolder(), "config.yml");
if(!worldConfigFile.exists()) { if(!worldConfigFile.exists()) {
@@ -19,48 +19,30 @@
package de.steamwar.schematicsystem; package de.steamwar.schematicsystem;
import de.steamwar.sql.GameModeConfig;
import de.steamwar.data.GameModeConfigUtils;
import de.steamwar.linkage.AbstractLinker; import de.steamwar.linkage.AbstractLinker;
import de.steamwar.linkage.SpigotLinker; import de.steamwar.linkage.SpigotLinker;
import de.steamwar.message.Message; import de.steamwar.message.Message;
import de.steamwar.sql.GameModeConfig;
import de.steamwar.sql.SchematicType; import de.steamwar.sql.SchematicType;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class SchematicSystem extends JavaPlugin { public class SchematicSystem extends JavaPlugin {
public static final Message MESSAGE = new Message("SchematicSystem", SchematicSystem.class.getClassLoader()); public static final Message MESSAGE = new Message("SchematicSystem", SchematicSystem.class.getClassLoader());
private static SchematicSystem instance; private static SchematicSystem instance;
private static final Map<SchematicType, GameModeConfig<Material, String>> types = new HashMap<>();
public static GameModeConfig<Material, String> getGameModeConfig(SchematicType type){ public static GameModeConfig<Material, String> getGameModeConfig(SchematicType type){
return types.get(type); GameModeConfig<Material, String> gameModeConfig = GameModeConfig.getBySchematicType(type);
if (gameModeConfig.CheckQuestions.isEmpty() && gameModeConfig.Schematic.ManualCheck) return null;
return gameModeConfig;
} }
@Override @Override
public void onEnable() { public void onEnable() {
instance = this; 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<Material, String> 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); SpigotLinker spigotLinker = new SpigotLinker(this, MESSAGE);
try { try {
spigotLinker.link(); spigotLinker.link();
@@ -37,7 +37,7 @@ public class SQLWrapperImpl implements SQLWrapper<Material> {
@Override @Override
public GameModeConfig<Material, String> loadGameModeConfig(File file) { public GameModeConfig<Material, String> 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(); private static final String SERVER_VERSION = Bukkit.getServer().getVersion();
@@ -35,7 +35,7 @@ public class SQLWrapperImpl implements SQLWrapper<String> {
@Override @Override
public GameModeConfig<String, String> loadGameModeConfig(File file) { public GameModeConfig<String, String> 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 @Override
@@ -51,7 +51,7 @@ public class ArenaMode {
return; return;
for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) { for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) {
GameModeConfig<String, String> gameModeConfig = new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName); GameModeConfig<String, String> gameModeConfig = new GameModeConfig<>(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToInternalName, false);
if (!gameModeConfig.Server.loaded) continue; if (!gameModeConfig.Server.loaded) continue;
allModes.add(gameModeConfig); allModes.add(gameModeConfig);
@@ -27,7 +27,7 @@ class SQLWrapperImpl: SQLWrapper<String> {
} }
override fun loadGameModeConfig(file: File): GameModeConfig<String, String> { override fun loadGameModeConfig(file: File): GameModeConfig<String, String> {
return GameModeConfig(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear) return GameModeConfig(file, GameModeConfig.ToString, GameModeConfig.ToString, GameModeConfig.ToStaticWarGear, true)
} }
override fun additionalExceptionMetadata(builder: StringBuilder) { override fun additionalExceptionMetadata(builder: StringBuilder) {