forked from SteamWar/SteamWar
Add Unified GameModeConfig
This commit is contained in:
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* 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 String getString(String path, String defaultValue) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof String) {
|
||||
return (String) value;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path, int defaultValue) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof Integer) {
|
||||
return (Integer) value;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(String path, double defaultValue) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof Double) {
|
||||
return (Double) value;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String path, boolean defaultValue) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof Boolean) {
|
||||
return (Boolean) value;
|
||||
} else {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getStringList(String path) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof List) {
|
||||
return Collections.unmodifiableList((List<String>) value);
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Integer> getIntList(String path) {
|
||||
Object value = this.document.get(path);
|
||||
if (value instanceof List) {
|
||||
return Collections.unmodifiableList((List<Integer>) value);
|
||||
} else {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user