From 188d541c4d01e30b604ea03feaf9c4711a6bfea8 Mon Sep 17 00:00:00 2001 From: xPlumpOrange <31327360+xPlumpOrange@users.noreply.github.com> Date: Sat, 8 May 2021 20:22:42 -0400 Subject: [PATCH] Added Node Checking for Smarter Configs Implemented node checks during config loading to stop the config api from providing null and causing other unrelated initialization errors and stopping auto config reset. --- .../net/tcpshield/tcpshield/bukkit/BukkitConfig.java | 10 ++++++++++ .../net/tcpshield/tcpshield/bungee/BungeeConfig.java | 10 ++++++++++ .../tcpshield/tcpshield/provider/ConfigProvider.java | 8 ++++++++ .../tcpshield/tcpshield/velocity/VelocityConfig.java | 10 ++++++++++ 4 files changed, 38 insertions(+) diff --git a/src/main/java/net/tcpshield/tcpshield/bukkit/BukkitConfig.java b/src/main/java/net/tcpshield/tcpshield/bukkit/BukkitConfig.java index 156b158..d20a6e8 100644 --- a/src/main/java/net/tcpshield/tcpshield/bukkit/BukkitConfig.java +++ b/src/main/java/net/tcpshield/tcpshield/bukkit/BukkitConfig.java @@ -35,6 +35,14 @@ public class BukkitConfig extends ConfigProvider { } + @Override + protected void checkNodes(String... nodes) { + for (String node : nodes) { + if (!loadedConfiguration.contains(node)) + throw new ConfigException("The node \"" + node + "\" does not exist in the config."); + } + } + @Override protected void reset() throws ConfigResetException { try { @@ -59,6 +67,8 @@ public class BukkitConfig extends ConfigProvider { try { loadedConfiguration = YamlConfiguration.loadConfiguration(configFile); + checkNodes("only-allow-proxy-connections", "timestamp-validation", "debug-mode"); + this.onlyProxy = loadedConfiguration.getBoolean("only-allow-proxy-connections"); this.timestampValidationMode = loadedConfiguration.getString("timestamp-validation"); this.doDebug = loadedConfiguration.getBoolean("debug-mode"); diff --git a/src/main/java/net/tcpshield/tcpshield/bungee/BungeeConfig.java b/src/main/java/net/tcpshield/tcpshield/bungee/BungeeConfig.java index 4f2a394..d62f01e 100644 --- a/src/main/java/net/tcpshield/tcpshield/bungee/BungeeConfig.java +++ b/src/main/java/net/tcpshield/tcpshield/bungee/BungeeConfig.java @@ -38,6 +38,14 @@ public class BungeeConfig extends ConfigProvider { } + @Override + protected void checkNodes(String... nodes) { + for (String node : nodes) { + if (!loadedConfiguration.contains(node)) + throw new ConfigException("The node \"" + node + "\" does not exist in the config."); + } + } + @Override protected void reset() throws ConfigResetException { try { @@ -63,6 +71,8 @@ public class BungeeConfig extends ConfigProvider { ConfigurationProvider configProvider = ConfigurationProvider.getProvider(YamlConfiguration.class); loadedConfiguration = configProvider.load(configFile); + checkNodes("only-allow-proxy-connections", "timestamp-validation", "debug-mode"); + this.onlyProxy = loadedConfiguration.getBoolean("only-allow-proxy-connections"); this.timestampValidationMode = loadedConfiguration.getString("timestamp-validation"); this.doDebug = loadedConfiguration.getBoolean("debug-mode"); diff --git a/src/main/java/net/tcpshield/tcpshield/provider/ConfigProvider.java b/src/main/java/net/tcpshield/tcpshield/provider/ConfigProvider.java index 67cbe77..192d192 100644 --- a/src/main/java/net/tcpshield/tcpshield/provider/ConfigProvider.java +++ b/src/main/java/net/tcpshield/tcpshield/provider/ConfigProvider.java @@ -3,6 +3,7 @@ package net.tcpshield.tcpshield.provider; import net.tcpshield.tcpshield.util.exception.config.ConfigLoadException; import net.tcpshield.tcpshield.util.exception.config.ConfigReloadException; import net.tcpshield.tcpshield.util.exception.config.ConfigResetException; +import net.tcpshield.tcpshield.util.exception.phase.ConfigException; import java.io.File; @@ -75,4 +76,11 @@ public abstract class ConfigProvider { */ public abstract void reload() throws ConfigReloadException; + /** + * Checks the provided nodes to see if they exist in the config + * @param nodes The nodes to check + * @throws ConfigException Thrown when a node isnt found + */ + protected abstract void checkNodes(String... nodes) throws ConfigException; + } diff --git a/src/main/java/net/tcpshield/tcpshield/velocity/VelocityConfig.java b/src/main/java/net/tcpshield/tcpshield/velocity/VelocityConfig.java index 19c147b..acc67c1 100644 --- a/src/main/java/net/tcpshield/tcpshield/velocity/VelocityConfig.java +++ b/src/main/java/net/tcpshield/tcpshield/velocity/VelocityConfig.java @@ -34,6 +34,14 @@ public class VelocityConfig extends ConfigProvider { } + @Override + protected void checkNodes(String... nodes) { + for (String node : nodes) { + if (!loadedToml.contains(node)) + throw new ConfigException("The node \"" + node + "\" does not exist in the config."); + } + } + @Override protected void reset() throws ConfigResetException { try { @@ -58,6 +66,8 @@ public class VelocityConfig extends ConfigProvider { try { loadedToml = new Toml().read(configFile); + checkNodes("only-allow-proxy-connections", "timestamp-validation", "debug-mode"); + this.onlyProxy = loadedToml.getBoolean("only-allow-proxy-connections"); this.timestampValidationMode = loadedToml.getString("timestamp-validation"); this.doDebug = loadedToml.getBoolean("debug-mode");