forked from SteamWar/SteamWar
Improve YMLWrapper
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user