From 9b0d7bd8561d13d6ad415f0febf1438009ec083c Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sun, 16 Jun 2024 20:32:57 +0200 Subject: [PATCH] Port SQLWrapperImpl --- src/de/steamwar/bungeecore/ArenaMode.java | 14 +--- .../steamwar/bungeecore/util/YamlUtils.java | 82 +++++++++++++++++++ src/de/steamwar/sql/SQLWrapperImpl.java | 55 ++++++------- 3 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 src/de/steamwar/bungeecore/util/YamlUtils.java diff --git a/src/de/steamwar/bungeecore/ArenaMode.java b/src/de/steamwar/bungeecore/ArenaMode.java index 0dd0ff9e..caba2ed5 100644 --- a/src/de/steamwar/bungeecore/ArenaMode.java +++ b/src/de/steamwar/bungeecore/ArenaMode.java @@ -28,6 +28,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.util.*; +import static de.steamwar.bungeecore.util.YamlUtils.*; + public class ArenaMode { private static final Random random = new Random(); @@ -48,12 +50,11 @@ public class ArenaMode { bySchemType.clear(); allModes.clear(); - Yaml yaml = new Yaml(); File folder = new File(VelocityCore.get().getDataDirectory().getParent().toFile(), "FightSystem"); for(File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().toList()) { Map config; try { - config = yaml.load(new FileInputStream(configFile)); + config = loadYaml(configFile); } catch (IOException e) { throw new SecurityException("Could not load SchematicTypes", e); } @@ -130,7 +131,7 @@ public class ArenaMode { this.historic = get(server, "Historic", false); maps.forEach(map -> lowerToRealMapNames.put(map.toLowerCase(), map)); - this.schemType = get(get(config, "Schematic"), "Type", "").toLowerCase(); + this.schemType = get(config, "Schematic.Type", "").toLowerCase(); allModes.add(this); byInternal.put(internalName, this); @@ -142,13 +143,6 @@ public class ArenaMode { bySchemType.put(SchematicType.fromDB(this.schemType), this); } - private T get(Map config, String key) { - return (T) config.get(key); - } - private T get(Map config, String key, T def) { - return (T) config.getOrDefault(key, def); - } - public String hasMap(String map){ for(String m : maps){ if(m.equalsIgnoreCase(map)) diff --git a/src/de/steamwar/bungeecore/util/YamlUtils.java b/src/de/steamwar/bungeecore/util/YamlUtils.java new file mode 100644 index 00000000..ccf169e2 --- /dev/null +++ b/src/de/steamwar/bungeecore/util/YamlUtils.java @@ -0,0 +1,82 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2024 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 . + */ + +package de.steamwar.bungeecore.util; + +import lombok.experimental.UtilityClass; +import org.yaml.snakeyaml.Yaml; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@UtilityClass +public class YamlUtils { + + public T get(Map config, String key) { + if (key.contains(".")) { + return getRecursive(config, key.split("\\."), null); + } + + return (T) config.get(key); + } + + public T get(Map config, String key, T def) { + if (key.contains(".")) { + return getRecursive(config, key.split("\\."), def); + } + + return (T) config.getOrDefault(key, def); + } + + public T getRecursive(Map config, String[] keys, T def) { + if (keys.length == 1) { + return get(config, keys[0], def); + } else { + Object o = get(config, keys[0], null); + if (o == null) { + if (def == null) { + throw new IllegalArgumentException("Key " + keys[0] + " not found"); + } + return def; + } + + if (o instanceof Map) { + return getRecursive((Map) o, Arrays.copyOfRange(keys, 1, keys.length), def); + } else { + throw new IllegalArgumentException("Key " + keys[0] + " is not a map"); + } + } + } + + public List getList(Map config, String key) { + return (List) config.get(key); + } + + public T loadYaml(File f) throws FileNotFoundException { + return loadYaml(f, new Yaml()); + } + + public T loadYaml(File f, Yaml yaml) throws FileNotFoundException { + return yaml.load(new FileInputStream(f)); + } +} diff --git a/src/de/steamwar/sql/SQLWrapperImpl.java b/src/de/steamwar/sql/SQLWrapperImpl.java index 8130873c..8e7bf8be 100644 --- a/src/de/steamwar/sql/SQLWrapperImpl.java +++ b/src/de/steamwar/sql/SQLWrapperImpl.java @@ -19,27 +19,23 @@ package de.steamwar.sql; +import com.velocitypowered.api.proxy.Player; +import com.velocitypowered.api.proxy.server.RegisteredServer; import de.steamwar.bungeecore.VelocityCore; import de.steamwar.bungeecore.commands.CheckCommand; -import net.md_5.bungee.api.ProxyServer; -import net.md_5.bungee.api.config.ServerInfo; -import net.md_5.bungee.api.connection.ProxiedPlayer; -import net.md_5.bungee.config.Configuration; -import net.md_5.bungee.config.ConfigurationProvider; -import net.md_5.bungee.config.YamlConfiguration; +import org.yaml.snakeyaml.Yaml; import java.io.File; -import java.io.IOException; +import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.text.ParseException; import java.text.SimpleDateFormat; -import java.util.Arrays; -import java.util.Date; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; -public class SQLWrapperImpl implements SQLWrapper { +import static de.steamwar.bungeecore.util.YamlUtils.*; +public class SQLWrapperImpl implements SQLWrapper { private static final SimpleDateFormat deadlineFormat = new SimpleDateFormat("dd.MM.yyyy HH:mm"); private static Date parseDeadline(String deadline) { if(deadline == null) @@ -54,37 +50,38 @@ public class SQLWrapperImpl implements SQLWrapper { @Override public void loadSchemTypes(List tmpTypes, Map tmpFromDB) { - File folder = new File(VelocityCore.get().getDataFolder().getParentFile(), "FightSystem"); + File folder = new File(VelocityCore.get().getDataDirectory().getParent().toFile(), "FightSystem"); if(folder.exists()) { for(File configFile : Arrays.stream(folder.listFiles((file, name) -> name.endsWith(".yml") && !name.endsWith(".kits.yml"))).sorted().collect(Collectors.toList())) { - Configuration config; + Map config; + Yaml yaml = new Yaml(); try { - config = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile); - } catch (IOException e) { - throw new SecurityException("Could not load SchematicTypes", e); + config = yaml.load(new FileInputStream(configFile)); + } catch (FileNotFoundException e) { + throw new SecurityException("Could not load config.yml", e); } - if(!config.contains("Schematic")) + if(!config.containsKey("Schematic")) continue; - String type = config.getString("Schematic.Type"); + String type = get(config, "Schematic.Type"); assert type != null; - String shortcut = config.getString("Schematic.Shortcut"); + String shortcut = get(config,"Schematic.Shortcut"); if(tmpFromDB.containsKey(type.toLowerCase())) continue; SchematicType checktype = null; - String material = config.getString("Schematic.Material"); + String material = get(config,"Schematic.Material"); - if(!config.getStringList("CheckQuestions").isEmpty()) { + if(!getList(config,"CheckQuestions").isEmpty()) { checktype = new SchematicType("C" + type, "C" + shortcut, SchematicType.Type.CHECK_TYPE, null, material, true); tmpTypes.add(checktype); tmpFromDB.put(checktype.toDB(), checktype); - CheckCommand.setCheckQuestions(checktype, config); + CheckCommand.setCheckQuestions(checktype, get(config, "CheckQuestions")); } - boolean manualCheck = config.getBoolean("Schematic.ManualCheck", true); + boolean manualCheck = get(config, "Schematic.ManualCheck", true); - SchematicType current = new SchematicType(type, shortcut, !config.contains("Server") ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, parseDeadline(config.getString("deadline", null)), manualCheck); + SchematicType current = new SchematicType(type, shortcut, !config.containsKey("Server") ? SchematicType.Type.FIGHT_TYPE : SchematicType.Type.NORMAL, checktype, material, parseDeadline(get(config,"deadline", null)), manualCheck); tmpTypes.add(current); tmpFromDB.put(type.toLowerCase(), current); if(checktype != null) @@ -96,10 +93,10 @@ public class SQLWrapperImpl implements SQLWrapper { @Override public void additionalExceptionMetadata(StringBuilder builder) { builder.append("\nServers: "); - for(Map.Entry server : ProxyServer.getInstance().getServers().entrySet()) { - builder.append(server.getKey()).append("("); - for(ProxiedPlayer player : server.getValue().getPlayers()) { - builder.append(player.getName()).append(" "); + for(RegisteredServer server : VelocityCore.getProxy().getAllServers()) { + builder.append(server.getServerInfo().getName()).append("("); + for(Player player : server.getPlayersConnected()) { + builder.append(player.getUsername()).append(" "); } builder.append(") "); }