Add Unified GameModeConfig

This commit is contained in:
2025-10-25 21:45:44 +02:00
parent 0a6977e433
commit 87a4836fa1
103 changed files with 1731 additions and 876 deletions
@@ -0,0 +1,154 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2025 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.data;
import de.steamwar.sql.SchematicType;
import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
public class YMLWrapperImpl<M, ST, W> extends YMLWrapper<M, ST, W> {
public static YMLWrapperImpl<Material, SchematicType, String> ofTyped(File file) {
return new YMLWrapperImpl<>(file, ToMaterial, ToSchematicType, ToString);
}
public static <W> YMLWrapperImpl<Material, SchematicType, W> ofTyped(File file, Function<String, W> ToWincondtion) {
return new YMLWrapperImpl<>(file, ToMaterial, ToSchematicType, ToWincondtion);
}
public static YMLWrapperImpl<String, String, String> ofRaw(File file) {
return new YMLWrapperImpl<>(file, ToString, ToString, ToString);
}
public static final Function<String, Material> ToMaterial = material -> {
Material mat = Material.getMaterial(material);
if (mat == null) {
try {
mat = Material.valueOf(material);
} catch (IllegalArgumentException e) {
// Ignore
}
}
return mat;
};
private final FileConfiguration config;
private final String pathPrefix;
private YMLWrapperImpl(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
super(file, materialMapper, schematicTypeMapper, winconditionMapper);
if (file == null || !file.exists()) {
config = null;
pathPrefix = "";
} else {
config = YamlConfiguration.loadConfiguration(file);
pathPrefix = "";
}
}
private YMLWrapperImpl(FileConfiguration config, String pathPrefix, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
super(null, materialMapper, schematicTypeMapper, winconditionMapper);
this.config = config;
this.pathPrefix = pathPrefix;
}
@Override
public boolean canLoad() {
return config != null;
}
@Override
public YMLWrapper<M, ST, W> with(String path) {
if (pathPrefix.isEmpty()) {
path = path + ".";
} else {
path = pathPrefix + path + ".";
}
if (config != null && config.isConfigurationSection(path)) {
return new YMLWrapperImpl<>(this.config, path, materialMapper, schematicTypeMapper, winconditionMapper);
} else {
return new YMLWrapperImpl<>(null, path, materialMapper, schematicTypeMapper, winconditionMapper);
}
}
@Override
public String getDefaultGameName() {
return "WarGear";
}
@Override
public String getString(String path, String defaultValue) {
if (config == null) return defaultValue;
return config.getString(pathPrefix + path, defaultValue);
}
@Override
public int getInt(String path, int defaultValue) {
if (config == null) return defaultValue;
return config.getInt(pathPrefix + path, defaultValue);
}
@Override
public double getDouble(String path, double defaultValue) {
if (config == null) return defaultValue;
return config.getDouble(pathPrefix + path, defaultValue);
}
@Override
public boolean getBoolean(String path, boolean defaultValue) {
if (config == null) return defaultValue;
return config.getBoolean(pathPrefix + path, defaultValue);
}
@Override
public List<String> getStringList(String path) {
if (config == null) return Collections.emptyList();
List<String> list = config.getStringList(pathPrefix + path);
if (list.isEmpty()) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(list);
}
}
@Override
public List<Integer> getIntList(String path) {
if (config == null) return Collections.emptyList();
List<Integer> list = config.getIntegerList(pathPrefix + path);
if (list.isEmpty()) {
return Collections.emptyList();
} else {
return Collections.unmodifiableList(list);
}
}
@Override
public List<Map<?, ?>> getMapList(String path) {
if (config == null) return Collections.emptyList();
return config.getMapList(pathPrefix + path);
}
}
@@ -20,9 +20,10 @@
package de.steamwar.sql;
import de.steamwar.core.Core;
import de.steamwar.data.GameModeConfig;
import de.steamwar.data.YMLWrapperImpl;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import java.io.File;
@@ -36,33 +37,30 @@ public class SQLWrapperImpl implements SQLWrapper {
@Override
public void loadSchemTypes(List<SchematicType> tmpTypes, Map<String, SchematicType> tmpFromDB) {
File folder = new File(Core.getInstance().getDataFolder().getParentFile(), "FightSystem");
if(folder.exists()) {
for(File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) {
YamlConfiguration config = YamlConfiguration.loadConfiguration(configFile);
if(!config.isConfigurationSection("Schematic"))
continue;
if (!folder.exists()) return;
for (File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) {
GameModeConfig<String, String, String> gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofRaw(configFile));
if (!gameModeConfig.Schematic.loaded) continue;
String type = gameModeConfig.Schematic.Type;
assert type != null;
String shortcut = gameModeConfig.Schematic.Shortcut;
if (tmpFromDB.containsKey(type.toLowerCase()))
continue;
String type = config.getString("Schematic.Type");
assert type != null;
String shortcut = config.getString("Schematic.Shortcut");
if(tmpFromDB.containsKey(type.toLowerCase()))
continue;
SchematicType checktype = null;
String material = gameModeConfig.Schematic.Material;
SchematicType checktype = null;
String material = config.getString("Schematic.Material", "STONE_BUTTON");
if(!config.getStringList("CheckQuestions").isEmpty()) {
checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true);
tmpTypes.add(checktype);
tmpFromDB.put(checktype.toDB(), checktype);
}
boolean manualCheck = config.getBoolean("Schematic.ManualCheck", true);
SchematicType current = new SchematicType(type, shortcut, config.isConfigurationSection("Server") ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, manualCheck);
tmpTypes.add(current);
tmpFromDB.put(type.toLowerCase(), current);
if (!gameModeConfig.CheckQuestions.isEmpty()) {
checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true);
tmpTypes.add(checktype);
tmpFromDB.put(checktype.toDB(), checktype);
}
boolean manualCheck = gameModeConfig.Schematic.ManualCheck;
SchematicType current = new SchematicType(type, shortcut, gameModeConfig.Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, manualCheck);
tmpTypes.add(current);
tmpFromDB.put(type.toLowerCase(), current);
}
}
@@ -71,10 +69,10 @@ public class SQLWrapperImpl implements SQLWrapper {
@Override
public void additionalExceptionMetadata(StringBuilder builder) {
builder.append("\nPlayers: ");
for(Player player : Bukkit.getOnlinePlayers())
for (Player player : Bukkit.getOnlinePlayers())
builder.append(player.getName()).append(" ");
builder.append("\nWorlds: ");
for(World world : Bukkit.getWorlds())
for (World world : Bukkit.getWorlds())
builder.append(world.getName()).append(" ");
builder.append("\nServer: ").append(SERVER_VERSION);
}