Improve YMLWrapper

This commit is contained in:
2025-10-26 09:14:34 +01:00
parent 4405d9c25d
commit aec03e41a1
14 changed files with 163 additions and 314 deletions
@@ -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);