forked from SteamWar/SteamWar
Improve YMLWrapper
This commit is contained in:
+3
-2
@@ -23,7 +23,8 @@ import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||
import de.steamwar.bausystem.region.*;
|
||||
import de.steamwar.bausystem.utils.PasteBuilder;
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.data.YMLWrapperUtils;
|
||||
import de.steamwar.sql.SchematicType;
|
||||
import lombok.NonNull;
|
||||
import lombok.Setter;
|
||||
@@ -94,7 +95,7 @@ public final class FixedGlobalRegion implements Region {
|
||||
}
|
||||
};
|
||||
|
||||
private static final GameModeConfig<Material, SchematicType, String> GLOBAL_CONFIG = new GameModeConfig<>(YMLWrapperImpl.ofTyped(null));
|
||||
private static final GameModeConfig<Material, SchematicType, String> GLOBAL_CONFIG = new GameModeConfig<>(null, YMLWrapperUtils.ToMaterial, YMLWrapper.ToSchematicType, YMLWrapper.ToString, YMLWrapper.ToStaticWarGear);
|
||||
|
||||
private FixedGlobalRegion() {
|
||||
}
|
||||
|
||||
+3
-2
@@ -29,7 +29,8 @@ import de.steamwar.bausystem.utils.PasteBuilder;
|
||||
import de.steamwar.bausystem.worlddata.WorldData;
|
||||
import de.steamwar.core.Core;
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.data.YMLWrapperUtils;
|
||||
import de.steamwar.sql.SchematicType;
|
||||
import lombok.NonNull;
|
||||
import org.bukkit.Bukkit;
|
||||
@@ -339,7 +340,7 @@ public class FixedRegion implements Region {
|
||||
break;
|
||||
}
|
||||
}
|
||||
this.gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofTyped(found));
|
||||
this.gameModeConfig = new GameModeConfig<>(found, YMLWrapperUtils.ToMaterial, YMLWrapper.ToSchematicType, YMLWrapper.ToString, YMLWrapper.ToStaticWarGear);
|
||||
this.regionData = new RegionData.RegionDataImpl(regionData, WorldData::write);
|
||||
}
|
||||
|
||||
|
||||
@@ -23,4 +23,6 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
api(project(":CommonCore:SQL"))
|
||||
|
||||
implementation("org.yaml:snakeyaml:2.2")
|
||||
}
|
||||
@@ -28,6 +28,7 @@ import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ToString
|
||||
@@ -94,8 +95,10 @@ public final class GameModeConfig<M, ST, W> {
|
||||
public final List<Integer> EnterStages;
|
||||
public final Techhider<M> Techhider;
|
||||
|
||||
public GameModeConfig(YMLWrapper<M, ST, W> loader) {
|
||||
configFile = loader.getFile();
|
||||
public GameModeConfig(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper, Function<File, String> defaultGameName) {
|
||||
YMLWrapper<M, ST, W> loader = new YMLWrapper<>(file, materialMapper, schematicTypeMapper, winconditionMapper);
|
||||
|
||||
configFile = file;
|
||||
loaded = loader.canLoad();
|
||||
Server = new Server(loader.with("Server"));
|
||||
|
||||
@@ -116,7 +119,7 @@ public final class GameModeConfig<M, ST, W> {
|
||||
Times = new Times(loader.with("Times"));
|
||||
// Arena would be here to be in config order but needs Schematic.Size and EnterStages loaded afterwards
|
||||
Schematic = new Schematic<>(loader.with("Schematic"));
|
||||
GameName = loader.getString("GameName", loader.getDefaultGameName());
|
||||
GameName = loader.getString("GameName", defaultGameName.apply(file));
|
||||
ActiveMonths = loader.getIntList("ActiveMonths");
|
||||
TeamChatPrefix = loader.getString("TeamChatPrefix", "+");
|
||||
Blue = new Blue(loader.with("Blue"));
|
||||
|
||||
@@ -20,8 +20,11 @@
|
||||
package de.steamwar.data;
|
||||
|
||||
import de.steamwar.sql.SchematicType;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -29,34 +32,75 @@ import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public abstract class YMLWrapper<M, ST, W> {
|
||||
public final class YMLWrapper<M, ST, W> {
|
||||
|
||||
public static final Function<String, String> ToString = Function.identity();
|
||||
public static final Function<String, SchematicType> ToSchematicType = SchematicType::fromDB;
|
||||
|
||||
protected final File file;
|
||||
public final Function<String, M> materialMapper;
|
||||
public final Function<String, ST> schematicTypeMapper;
|
||||
public final Function<String, W> winconditionMapper;
|
||||
public static final Function<File, String> ToStaticWarGear = __ -> "WarGear";
|
||||
public static final Function<File, String> ToInternalName = file -> file.getName().replace(".yml", "");
|
||||
|
||||
protected YMLWrapper(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
|
||||
private final File file;
|
||||
final Function<String, M> materialMapper;
|
||||
final Function<String, ST> schematicTypeMapper;
|
||||
final Function<String, W> winconditionMapper;
|
||||
|
||||
private final boolean canLoad;
|
||||
private final Map<String, Object> document;
|
||||
|
||||
public YMLWrapper(File file, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
|
||||
this.file = file;
|
||||
this.materialMapper = materialMapper;
|
||||
this.schematicTypeMapper = schematicTypeMapper;
|
||||
this.winconditionMapper = winconditionMapper;
|
||||
|
||||
Yaml yaml = new Yaml();
|
||||
Map<String, Object> document = Collections.emptyMap();
|
||||
boolean canLoad = false;
|
||||
if (file != null && file.exists() && file.isFile()) {
|
||||
try {
|
||||
document = yaml.load(new FileReader(file));
|
||||
canLoad = true;
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
this.document = document;
|
||||
this.canLoad = canLoad;
|
||||
}
|
||||
|
||||
public final File getFile() {
|
||||
return file;
|
||||
private YMLWrapper(boolean canLoad, Map<String, Object> document, Function<String, M> materialMapper, Function<String, ST> schematicTypeMapper, Function<String, W> winconditionMapper) {
|
||||
this.file = null;
|
||||
this.materialMapper = materialMapper;
|
||||
this.schematicTypeMapper = schematicTypeMapper;
|
||||
this.winconditionMapper = winconditionMapper;
|
||||
this.canLoad = canLoad;
|
||||
this.document = document;
|
||||
}
|
||||
|
||||
public abstract boolean canLoad();
|
||||
public boolean canLoad() {
|
||||
return canLoad;
|
||||
}
|
||||
|
||||
public abstract YMLWrapper<M, ST, W> with(String path);
|
||||
public YMLWrapper<M, ST, W> with(String path) {
|
||||
if (document.containsKey(path)) {
|
||||
Object value = document.get(path);
|
||||
if (value instanceof Map) {
|
||||
return new YMLWrapper<>(true, (Map) value, materialMapper, schematicTypeMapper, winconditionMapper);
|
||||
}
|
||||
}
|
||||
return new YMLWrapper<>(false, Collections.emptyMap(), materialMapper, schematicTypeMapper, winconditionMapper);
|
||||
}
|
||||
|
||||
public abstract String getDefaultGameName();
|
||||
|
||||
public abstract <T> T get(String path, T defaultValue, Function<Object, T> mapper);
|
||||
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
|
||||
Object value = this.document.get(path);
|
||||
if (value == null) return defaultValue;
|
||||
try {
|
||||
return mapper.apply(value);
|
||||
} catch (ClassCastException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public String getString(String path, String defaultValue) {
|
||||
return get(path, defaultValue, Objects::toString);
|
||||
@@ -74,16 +118,24 @@ public abstract class YMLWrapper<M, ST, W> {
|
||||
return get(path, defaultValue, Boolean.class::cast);
|
||||
}
|
||||
|
||||
public final ST getSchematicType(String path, String defaultValue) {
|
||||
public ST getSchematicType(String path, String defaultValue) {
|
||||
String schematicType = getString(path, defaultValue);
|
||||
return schematicTypeMapper.apply(schematicType);
|
||||
}
|
||||
|
||||
public final M getMaterial(String path, String defaultValue) {
|
||||
public M getMaterial(String path, String defaultValue) {
|
||||
return materialMapper.apply(getString(path, defaultValue).toUpperCase());
|
||||
}
|
||||
|
||||
public abstract <T> List<T> get(String path, Function<Object, List<T>> mapper);
|
||||
public <T> List<T> get(String path, Function<Object, List<T>> mapper) {
|
||||
Object value = this.document.get(path);
|
||||
if (value == null) return Collections.emptyList();
|
||||
try {
|
||||
return Collections.unmodifiableList(mapper.apply(value));
|
||||
} catch (ClassCastException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getStringList(String path) {
|
||||
return get(path, o -> (List<String>) o);
|
||||
@@ -93,7 +145,7 @@ public abstract class YMLWrapper<M, ST, W> {
|
||||
return get(path, o -> (List<Integer>) o);
|
||||
}
|
||||
|
||||
public final List<ST> getSchematicTypeList(String path) {
|
||||
public List<ST> getSchematicTypeList(String path) {
|
||||
List<String> list = getStringList(path);
|
||||
if (list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
@@ -102,7 +154,7 @@ public abstract class YMLWrapper<M, ST, W> {
|
||||
}
|
||||
}
|
||||
|
||||
public final List<M> getMaterialList(String path) {
|
||||
public List<M> getMaterialList(String path) {
|
||||
List<String> list = getStringList(path);
|
||||
if (list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
@@ -111,5 +163,12 @@ public abstract class YMLWrapper<M, ST, W> {
|
||||
}
|
||||
}
|
||||
|
||||
public abstract List<Map<?, ?>> getMapList(String path);
|
||||
public List<Map<?, ?>> getMapList(String path) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof List) {
|
||||
return Collections.unmodifiableList((List<Map<?, ?>>) value);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,8 @@
|
||||
package de.steamwar.fightsystem;
|
||||
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.data.YMLWrapperUtils;
|
||||
import de.steamwar.fightsystem.utils.Region;
|
||||
import de.steamwar.fightsystem.winconditions.Winconditions;
|
||||
import de.steamwar.sql.*;
|
||||
@@ -109,7 +110,7 @@ public class Config {
|
||||
Bukkit.getLogger().log(Level.SEVERE, "Arenaconfig fehlt!");
|
||||
Bukkit.shutdown();
|
||||
}
|
||||
GameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofTyped(new File(FightSystem.getPlugin().getDataFolder(), configFile), Winconditions::valueOf));
|
||||
GameModeConfig = new GameModeConfig<>(new File(FightSystem.getPlugin().getDataFolder(), configFile), YMLWrapperUtils.ToMaterial, YMLWrapper.ToSchematicType, Winconditions::valueOf, YMLWrapper.ToStaticWarGear);
|
||||
|
||||
File worldConfigFile = new File(world.getWorldFolder(), "config.yml");
|
||||
if(!worldConfigFile.exists()) {
|
||||
|
||||
+3
-2
@@ -20,7 +20,8 @@
|
||||
package de.steamwar.schematicsystem;
|
||||
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.data.YMLWrapperUtils;
|
||||
import de.steamwar.sql.SchematicType;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
@@ -38,7 +39,7 @@ public class CheckSchemTypeManager {
|
||||
File folder = new File(SchematicSystem.getInstance().getDataFolder().getParentFile(), "FightSystem");
|
||||
if(folder.exists()) {
|
||||
for(File configFile : folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))) {
|
||||
GameModeConfig<Material, SchematicType, String> gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofTyped(configFile));
|
||||
GameModeConfig<Material, SchematicType, String> gameModeConfig = new GameModeConfig<>(configFile, YMLWrapperUtils.ToMaterial, YMLWrapper.ToSchematicType, YMLWrapper.ToString, YMLWrapper.ToStaticWarGear);
|
||||
if (gameModeConfig.CheckQuestions.isEmpty() && gameModeConfig.Schematic.ManualCheck)
|
||||
continue;
|
||||
types.put(gameModeConfig.Schematic.Type, gameModeConfig);
|
||||
|
||||
@@ -1,132 +0,0 @@
|
||||
/*
|
||||
* 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 <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
|
||||
if (config == null) return defaultValue;
|
||||
try {
|
||||
return mapper.apply(config.get(path, defaultValue));
|
||||
} catch (ClassCastException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> get(String path, Function<Object, List<T>> mapper) {
|
||||
if (config == null) return Collections.emptyList();
|
||||
List<?> list = config.getList(pathPrefix + path);
|
||||
if (list == null || list.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
try {
|
||||
return Collections.unmodifiableList(mapper.apply(list));
|
||||
} catch (ClassCastException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<?, ?>> getMapList(String path) {
|
||||
if (config == null) return Collections.emptyList();
|
||||
return config.getMapList(pathPrefix + path);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 lombok.experimental.UtilityClass;
|
||||
import org.bukkit.Material;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
@UtilityClass
|
||||
public class YMLWrapperUtils {
|
||||
|
||||
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;
|
||||
};
|
||||
}
|
||||
@@ -21,7 +21,7 @@ package de.steamwar.sql;
|
||||
|
||||
import de.steamwar.core.Core;
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
@@ -39,7 +39,7 @@ public class SQLWrapperImpl implements SQLWrapper {
|
||||
File folder = new File(Core.getInstance().getDataFolder().getParentFile(), "FightSystem");
|
||||
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));
|
||||
GameModeConfig<String, String, String> gameModeConfig = new GameModeConfig<>(configFile, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToStaticWarGear);
|
||||
if (!gameModeConfig.Schematic.loaded) continue;
|
||||
String type = gameModeConfig.Schematic.Type;
|
||||
assert type != null;
|
||||
@@ -56,9 +56,7 @@ public class SQLWrapperImpl implements SQLWrapper {
|
||||
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);
|
||||
SchematicType current = new SchematicType(type, shortcut, gameModeConfig.Server.loaded ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, gameModeConfig.Deadline, gameModeConfig.Schematic.ManualCheck);
|
||||
tmpTypes.add(current);
|
||||
tmpFromDB.put(type.toLowerCase(), current);
|
||||
}
|
||||
|
||||
@@ -1,121 +0,0 @@
|
||||
/*
|
||||
* 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.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class YMLWrapperImpl<ST> extends YMLWrapper<String, ST, String> {
|
||||
|
||||
public static YMLWrapperImpl<SchematicType> ofTyped(File file) {
|
||||
return new YMLWrapperImpl<>(file, ToSchematicType);
|
||||
}
|
||||
|
||||
public static YMLWrapperImpl<String> ofRaw(File file) {
|
||||
return new YMLWrapperImpl<>(file, ToString);
|
||||
}
|
||||
|
||||
private final boolean canLoad;
|
||||
private final Map<String, Object> document;
|
||||
|
||||
private YMLWrapperImpl(File file, Function<String, ST> schematicTypeMapper) {
|
||||
super(file, ToString, schematicTypeMapper, ToString);
|
||||
if (file == null || !file.exists()) {
|
||||
canLoad = false;
|
||||
document = new HashMap<>();
|
||||
} else {
|
||||
canLoad = true;
|
||||
Yaml yaml = new Yaml();
|
||||
try {
|
||||
document = yaml.load(new FileInputStream(file));
|
||||
} catch (IOException e) {
|
||||
throw new SecurityException(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private YMLWrapperImpl(boolean canLoad, Map<String, Object> document, Function<String, ST> schematicTypeMapper) {
|
||||
super(null, ToString, schematicTypeMapper, ToString);
|
||||
this.canLoad = canLoad;
|
||||
this.document = document;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canLoad() {
|
||||
return canLoad;
|
||||
}
|
||||
|
||||
@Override
|
||||
public YMLWrapper<String, ST, String> with(String path) {
|
||||
if (document.containsKey(path)) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof Map) {
|
||||
return new YMLWrapperImpl(this.canLoad, (Map) value, schematicTypeMapper);
|
||||
}
|
||||
}
|
||||
return new YMLWrapperImpl(false, Collections.emptyMap(), schematicTypeMapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDefaultGameName() {
|
||||
return file.getName().replace(".yml", "");
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T get(String path, T defaultValue, Function<Object, T> mapper) {
|
||||
Object value = this.document.get(path);
|
||||
if (value == null) return defaultValue;
|
||||
try {
|
||||
return mapper.apply(value);
|
||||
} catch (ClassCastException e) {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> List<T> get(String path, Function<Object, List<T>> mapper) {
|
||||
Object value = this.document.get(path);
|
||||
if (value == null) return Collections.emptyList();
|
||||
try {
|
||||
return Collections.unmodifiableList(mapper.apply(value));
|
||||
} catch (ClassCastException e) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Map<?, ?>> getMapList(String path) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof List) {
|
||||
return Collections.unmodifiableList((List<Map<?, ?>>) value);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,7 @@ package de.steamwar.sql;
|
||||
import com.velocitypowered.api.proxy.Player;
|
||||
import com.velocitypowered.api.proxy.server.RegisteredServer;
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.velocitycore.VelocityCore;
|
||||
import de.steamwar.velocitycore.commands.CheckCommand;
|
||||
|
||||
@@ -40,7 +40,7 @@ public class SQLWrapperImpl implements SQLWrapper {
|
||||
return;
|
||||
|
||||
for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) {
|
||||
GameModeConfig<String, String, String> gameModeConfig = new GameModeConfig<>(YMLWrapperImpl.ofRaw(file));
|
||||
GameModeConfig<String, String, String> gameModeConfig = new GameModeConfig<>(file, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToInternalName);
|
||||
if (!gameModeConfig.Schematic.loaded) continue;
|
||||
if (tmpFromDB.containsKey(gameModeConfig.Schematic.Type.toLowerCase())) continue;
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
package de.steamwar.velocitycore;
|
||||
|
||||
import de.steamwar.data.GameModeConfig;
|
||||
import de.steamwar.data.YMLWrapperImpl;
|
||||
import de.steamwar.data.YMLWrapper;
|
||||
import de.steamwar.sql.SchematicType;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.UtilityClass;
|
||||
@@ -52,7 +52,7 @@ public class ArenaMode {
|
||||
return;
|
||||
|
||||
for(File file : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) {
|
||||
de.steamwar.data.GameModeConfig<String, SchematicType, String> gameModeConfig = new de.steamwar.data.GameModeConfig<>(YMLWrapperImpl.ofTyped(file));
|
||||
GameModeConfig<String, SchematicType, String> gameModeConfig = new GameModeConfig<>(file, YMLWrapper.ToString, YMLWrapper.ToSchematicType, YMLWrapper.ToString, YMLWrapper.ToInternalName);
|
||||
if (!gameModeConfig.Server.loaded) continue;
|
||||
|
||||
allModes.add(gameModeConfig);
|
||||
|
||||
@@ -19,7 +19,8 @@
|
||||
|
||||
package de.steamwar.sql
|
||||
|
||||
import org.bspfsystems.yamlconfiguration.file.YamlConfiguration
|
||||
import de.steamwar.data.GameModeConfig
|
||||
import de.steamwar.data.YMLWrapper
|
||||
import java.io.File
|
||||
import java.util.*
|
||||
import java.util.stream.Collectors
|
||||
@@ -29,32 +30,26 @@ fun loadSchematicTypes(tmpTypes: MutableList<SchematicType>?, tmpFromDB: Mutable
|
||||
if (folder.exists()) {
|
||||
for (configFile in Arrays.stream(folder.listFiles { _, name -> name.endsWith(".yml") && !name.endsWith(".kits.yml") })
|
||||
.sorted().collect(Collectors.toList())) {
|
||||
val config: YamlConfiguration = YamlConfiguration.loadConfiguration(configFile)
|
||||
if (!config.isConfigurationSection("Schematic")) continue
|
||||
val type: String = config.getString("Schematic.Type")!!
|
||||
val shortcut = config.getString("Schematic.Shortcut")
|
||||
if (shortcut == null) {
|
||||
println("No shortcut for $type")
|
||||
continue
|
||||
}
|
||||
val gameModeConfig = GameModeConfig(configFile, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToString, YMLWrapper.ToStaticWarGear)
|
||||
|
||||
if (!gameModeConfig.Schematic.loaded) continue
|
||||
val type = gameModeConfig.Schematic.Type
|
||||
checkNotNull(type)
|
||||
val shortcut = gameModeConfig.Schematic.Shortcut
|
||||
if (tmpFromDB!!.containsKey(type.lowercase(Locale.getDefault()))) continue
|
||||
|
||||
var checktype: SchematicType? = null
|
||||
val material: String = config.getString("Schematic.Material", "STONE_BUTTON")!!
|
||||
if (!config.getStringList("CheckQuestions").isEmpty()) {
|
||||
checktype = SchematicType("C$type", "C$shortcut", SchematicType.Type.CHECK_TYPE, null, material, false)
|
||||
val material = gameModeConfig.Schematic.Material
|
||||
|
||||
if (!gameModeConfig.CheckQuestions.isEmpty()) {
|
||||
checktype = SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true)
|
||||
tmpTypes!!.add(checktype)
|
||||
tmpFromDB[checktype.toDB()] = checktype
|
||||
tmpFromDB.put(checktype.toDB(), checktype)
|
||||
}
|
||||
val current = SchematicType(
|
||||
type,
|
||||
shortcut,
|
||||
if (config.isConfigurationSection("Server")) SchematicType.Type.FIGHT_TYPE else SchematicType.Type.NORMAL,
|
||||
checktype,
|
||||
material,
|
||||
false
|
||||
)
|
||||
|
||||
val current = SchematicType(type, shortcut, if (gameModeConfig.Server.loaded) SchematicType.Type.FIGHT_TYPE else SchematicType.Type.NORMAL, checktype, material, gameModeConfig.Deadline, gameModeConfig.Schematic.ManualCheck)
|
||||
tmpTypes!!.add(current)
|
||||
tmpFromDB[type.lowercase(Locale.getDefault())] = current
|
||||
tmpFromDB.put(type.lowercase(Locale.getDefault()), current)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user