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
@@ -34,6 +34,28 @@ import java.util.stream.Collectors;
@ToString
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<File, String> ToStaticWarGear = __ -> "WarGear";
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);
configFile = file;
@@ -182,7 +204,20 @@ public final class GameModeConfig<M, W> {
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<M, W> {
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);
}