diff options
3 files changed, 71 insertions, 25 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 ff1b55f..bc1bdf8 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 @@ -22,6 +22,7 @@ package me.lucko.spark.bukkit; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Maps; import com.google.gson.GsonBuilder; import com.google.gson.JsonSerializer; @@ -29,6 +30,8 @@ import me.lucko.spark.common.platform.serverconfig.AbstractServerConfigProvider; import me.lucko.spark.common.platform.serverconfig.ConfigParser; import me.lucko.spark.common.platform.serverconfig.PropertiesConfigParser; +import org.bukkit.Bukkit; +import org.bukkit.World; import org.bukkit.configuration.MemorySection; import org.bukkit.configuration.file.YamlConfiguration; @@ -36,6 +39,9 @@ import co.aikar.timings.TimingsManager; import java.io.BufferedReader; import java.io.IOException; +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; @@ -58,15 +64,8 @@ public class BukkitServerConfigProvider extends AbstractServerConfigProvider { gson.registerTypeAdapter(MemorySection.class, (JsonSerializer<MemorySection>) (obj, type, ctx) -> ctx.serialize(obj.getValues(false))); } - @Override - protected String rewriteConfigPath(String path) { - return path.startsWith("config/") - ? path.substring("config/".length()) - : path; - } - - private enum YamlConfigParser implements ConfigParser { - INSTANCE; + private static class YamlConfigParser implements ConfigParser { + public static final YamlConfigParser INSTANCE = new YamlConfigParser(); @Override public Map<String, Object> parse(BufferedReader reader) throws IOException { @@ -75,14 +74,52 @@ public class BukkitServerConfigProvider extends AbstractServerConfigProvider { } } + // Paper 1.19+ split config layout + private static class SplitYamlConfigParser extends YamlConfigParser { + public static final SplitYamlConfigParser INSTANCE = new SplitYamlConfigParser(); + + @Override + public Map<String, Object> parse(String prefix) throws IOException { + Path configDir = Paths.get("config"); + if (!Files.exists(configDir)) { + return null; + } + + Map<String, Object> configs = Maps.newHashMap(); + + parseIfExists(configs, + "global.yml", + configDir.resolve(prefix + "-global.yml") + ); + parseIfExists(configs, + "world-defaults.yml", + configDir.resolve(prefix + "-world-defaults.yml") + ); + for (World world : Bukkit.getWorlds()) { + parseIfExists(configs, + world.getName() + ".yml", + world.getWorldFolder().toPath().resolve(prefix + "-world.yml") + ); + } + + return configs; + } + + private void parseIfExists(Map<String, Object> configs, String name, Path path) throws IOException { + Map<String, Object> values = parse(path); + if (values != null) { + configs.put(name, values); + } + } + } + static { ImmutableMap.Builder<String, ConfigParser> files = ImmutableMap.<String, ConfigParser>builder() .put("server.properties", PropertiesConfigParser.INSTANCE) .put("bukkit.yml", YamlConfigParser.INSTANCE) .put("spigot.yml", YamlConfigParser.INSTANCE) .put("paper.yml", YamlConfigParser.INSTANCE) - .put("config/paper-global.yml", YamlConfigParser.INSTANCE) - .put("config/paper-world-defaults.yml", YamlConfigParser.INSTANCE) + .put("paper", SplitYamlConfigParser.INSTANCE) .put("purpur.yml", YamlConfigParser.INSTANCE); for (String config : getSystemPropertyList("spark.serverconfigs.extra")) { 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 0eef111..5a14382 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 @@ -73,9 +73,7 @@ public abstract class AbstractServerConfigProvider implements ServerConfigProvid } delete(json, this.hiddenPaths); - - String name = rewriteConfigPath(path); - builder.put(name, json); + builder.put(path, json); } catch (Exception e) { e.printStackTrace(); } @@ -85,25 +83,18 @@ public abstract class AbstractServerConfigProvider implements ServerConfigProvid } private JsonElement load(String path, ConfigParser parser) throws IOException { - Path filePath = Paths.get(path); - if (!Files.exists(filePath)) { + Map<String, Object> values = parser.parse(path); + if (values == null) { return null; } - try (BufferedReader reader = Files.newBufferedReader(filePath, StandardCharsets.UTF_8)) { - Map<String, Object> values = parser.parse(reader); - return this.gson.toJsonTree(values); - } + return this.gson.toJsonTree(values); } protected void customiseGson(GsonBuilder gson) { } - protected String rewriteConfigPath(String path) { - return path; - } - /** * Deletes the given paths from the json element. * diff --git a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java index 2dd15fe..dfbf816 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java +++ b/spark-common/src/main/java/me/lucko/spark/common/platform/serverconfig/ConfigParser.java @@ -22,10 +22,28 @@ package me.lucko.spark.common.platform.serverconfig; 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.Map; public interface ConfigParser { - Map<String, Object> parse(BufferedReader reader) throws IOException; + default Map<String, Object> parse(String file) throws IOException { + return parse(Paths.get(file)); + } + + default Map<String, Object> parse(Path file) throws IOException { + if (!Files.exists(file)) { + return null; + } + + try (BufferedReader reader = Files.newBufferedReader(file, StandardCharsets.UTF_8)) { + return this.parse(reader); + } + } + + Map<String, Object> parse(BufferedReader reader) throws IOException; } |