Files
KekWar/VelocityCore/src/de/steamwar/data/YMLWrapperImpl.java
T
2025-10-25 22:08:58 +02:00

122 lines
3.9 KiB
Java

/*
* 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();
}
}
}