diff options
author | Ben Kerllenevich <ben@omega24.dev> | 2022-01-21 15:53:51 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-21 20:53:51 +0000 |
commit | 1642af9bacc88c20614234296fd596932b14d8df (patch) | |
tree | b95355e087aaa007ebd4b683945d52c4aab8e8d8 | |
parent | fe9b39b500ee2633db18ea14774daf01e2946824 (diff) | |
download | spark-1642af9bacc88c20614234296fd596932b14d8df.tar.gz spark-1642af9bacc88c20614234296fd596932b14d8df.tar.bz2 spark-1642af9bacc88c20614234296fd596932b14d8df.zip |
BukkitServerConfigProvider improvements (#159)
2 files changed, 95 insertions, 22 deletions
diff --git a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java index b1d2a60..5fdd178 100644 --- a/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java +++ b/spark-bukkit/src/main/java/me/lucko/spark/bukkit/BukkitServerConfigProvider.java @@ -20,8 +20,8 @@ package me.lucko.spark.bukkit; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonElement; @@ -32,31 +32,31 @@ import me.lucko.spark.common.platform.serverconfig.AbstractServerConfigProvider; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.YamlConfiguration; +import co.aikar.timings.TimingsManager; + import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Properties; public class BukkitServerConfigProvider extends AbstractServerConfigProvider<BukkitServerConfigProvider.FileType> { private static final Gson GSON = new GsonBuilder() .registerTypeAdapter(MemorySection.class, (JsonSerializer<MemorySection>) (obj, type, ctx) -> ctx.serialize(obj.getValues(false))) .create(); - private static final Map<String, FileType> FILES = ImmutableMap.of( - "bukkit.yml", FileType.YAML, - "spigot.yml", FileType.YAML, - "paper.yml", FileType.YAML - ); - - // todo: make configurable? - private static final List<String> HIDDEN_PATHS = ImmutableList.of( - "database", - "settings.bungeecord-addresses", - "settings.velocity-support.secret" - ); + /** A map of provided files and their type */ + private static final Map<String, FileType> FILES; + /** A collection of paths to be excluded from the files */ + private static final Collection<String> HIDDEN_PATHS; public BukkitServerConfigProvider() { super(FILES, HIDDEN_PATHS); @@ -64,15 +64,86 @@ public class BukkitServerConfigProvider extends AbstractServerConfigProvider<Buk @Override protected JsonElement load(String path, FileType type) throws IOException { - try (BufferedReader reader = Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8)) { - YamlConfiguration config = YamlConfiguration.loadConfiguration(reader); - Map<String, Object> values = config.getValues(false); + Path filePath = Paths.get(path); + if (!Files.exists(filePath)) { + return null; + } + + try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { + Map<String, Object> values; + + if (type == FileType.PROPERTIES) { + Properties properties = new Properties(); + properties.load(reader); + + values = new HashMap<>(); + properties.forEach((k, v) -> { + String key = k.toString(); + String value = v.toString(); + + if ("true".equals(value) || "false".equals(value)) { + values.put(key, Boolean.parseBoolean(value)); + } else if (value.matches("\\d+")) { + values.put(key, Integer.parseInt(value)); + } else { + values.put(key, value); + } + }); + } else if (type == FileType.YAML) { + YamlConfiguration config = YamlConfiguration.loadConfiguration(reader); + values = config.getValues(false); + } else { + throw new IllegalArgumentException("Unknown file type: " + type); + } + return GSON.toJsonTree(values); } } enum FileType { + PROPERTIES, YAML } + static { + ImmutableMap.Builder<String, FileType> files = ImmutableMap.<String, FileType>builder() + .put("server.properties", FileType.PROPERTIES) + .put("bukkit.yml", FileType.YAML) + .put("spigot.yml", FileType.YAML) + .put("paper.yml", FileType.YAML) + .put("purpur.yml", FileType.YAML); + + for (String config : getSystemPropertyList("spark.serverconfigs.extra")) { + files.put(config, FileType.YAML); + } + + ImmutableSet.Builder<String> hiddenPaths = ImmutableSet.<String>builder() + .add("database") + .add("settings.bungeecord-addresses") + .add("settings.velocity-support.secret") + .add("server-ip") + .add("motd") + .add("resource-pack") + .addAll(getTimingsHiddenConfigs()) + .addAll(getSystemPropertyList("spark.serverconfigs.hiddenpaths")); + + FILES = files.build(); + HIDDEN_PATHS = hiddenPaths.build(); + } + + private static List<String> getSystemPropertyList(String property) { + String value = System.getProperty(property); + return value == null + ? Collections.emptyList() + : Arrays.asList(value.split(",")); + } + + private static List<String> getTimingsHiddenConfigs() { + try { + return TimingsManager.hiddenConfigs; + } catch (NoClassDefFoundError e) { + return Collections.emptyList(); + } + } + } diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java index 98db960..1e6f1b4 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/AbstractServerConfigProvider.java @@ -26,9 +26,9 @@ import com.google.gson.JsonObject; import java.io.IOException; import java.util.Arrays; +import java.util.Collection; import java.util.Deque; import java.util.LinkedList; -import java.util.List; import java.util.Map; /** @@ -41,9 +41,9 @@ import java.util.Map; */ public abstract class AbstractServerConfigProvider<T extends Enum<T>> implements ServerConfigProvider { private final Map<String, T> files; - private final List<String> hiddenPaths; + private final Collection<String> hiddenPaths; - protected AbstractServerConfigProvider(Map<String, T> files, List<String> hiddenPaths) { + protected AbstractServerConfigProvider(Map<String, T> files, Collection<String> hiddenPaths) { this.files = files; this.hiddenPaths = hiddenPaths; } @@ -55,8 +55,10 @@ public abstract class AbstractServerConfigProvider<T extends Enum<T>> implements this.files.forEach((path, type) -> { try { JsonElement json = load(path, type); - delete(json, this.hiddenPaths); - builder.put(path, json); + if (json != null) { + delete(json, this.hiddenPaths); + builder.put(path, json); + } } catch (Exception e) { e.printStackTrace(); } @@ -81,7 +83,7 @@ public abstract class AbstractServerConfigProvider<T extends Enum<T>> implements * @param json the json element * @param paths the paths to delete */ - private static void delete(JsonElement json, List<String> paths) { + private static void delete(JsonElement json, Collection<String> paths) { for (String path : paths) { Deque<String> pathDeque = new LinkedList<>(Arrays.asList(path.split("\\."))); delete(json, pathDeque); |