diff --git a/src/main/java/eu/m724/tweaks/TweaksConfig.java b/src/main/java/eu/m724/tweaks/TweaksConfig.java deleted file mode 100644 index 584d0fe..0000000 --- a/src/main/java/eu/m724/tweaks/TweaksConfig.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Copyright (C) 2025 Minecon724 - * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file - * in the project root for the full license text. - */ - -package eu.m724.tweaks; - -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.plugin.Plugin; - -import java.util.Map; - -public record TweaksConfig( - boolean metrics, - boolean debug, - String locale, - - boolean worldborderExpand, - boolean worldborderHide, - - boolean brandEnabled, - String brandText, - boolean brandShowPing, - boolean brandShowMspt, - - boolean doorDoubleOpen, - boolean doorKnocking, - - boolean motdEnabled, - String motdSet, - - boolean chatEnabled, - boolean chatLocalEvents, - String chatDefaultName, - int chatRadius, - - boolean compassEnabled, - int compassWidth, - int compassPrecision, - - boolean pomodoroEnabled, - boolean pomodoroForce, - - boolean updaterEnabled, - - boolean hardcoreEnabled, - double hardcoreChance, - - boolean sleepEnabled, - boolean sleepInstant, - double sleepHeal, - - boolean authEnabled, - boolean authForce, - String authDomain, - - boolean redstoneEnabled, - String redstoneListen, - - Map knockbackModifiers, - - boolean killswitchEnabled, - String killswitchListen, - - boolean swingEnabled -) { - public static final int CONFIG_VERSION = 2; - private static TweaksConfig config; - - public static TweaksConfig getConfig() { - return config; - } - - public static TweaksConfig load(Plugin plugin) { - plugin.saveDefaultConfig(); - FileConfiguration config = plugin.getConfig(); - - int configVersion = config.getInt("magic number don't modify this", 0); - RuntimeException exception = new RuntimeException("Config version is %d, expected %d".formatted(configVersion, CONFIG_VERSION)); - - if (configVersion == 0) { - throw exception; - } else if (configVersion < CONFIG_VERSION) { - throw new RuntimeException("Please follow update instructions https://www.spigotmc.org/resources/tweaks724.121057/updates", exception); - } else if (configVersion > CONFIG_VERSION) { - throw new RuntimeException("Did you downgrade the plugin? Remove config.yml and let the plugin re-create it", exception); - } - - boolean metrics = config.getBoolean("metrics"); - boolean debug = config.getBoolean("debug", false); - String locale = config.getString("locale", "US"); - - boolean worldborderExpand = config.getBoolean("worldborder.expand"); - boolean worldborderHide = config.getBoolean("worldborder.hide"); - - boolean brandEnabled = config.getBoolean("brand.enabled"); - String brandText = config.getString("brand.text"); - boolean brandShowPing = config.getBoolean("brand.showPing"); - boolean brandShowMspt = config.getBoolean("brand.showMspt"); - - boolean doorDoubleOpen = config.getBoolean("doors.doubleOpen"); - boolean doorKnocking = config.getBoolean("doors.knocking"); - - String motdSet = config.getString("motd.set"); - boolean motdEnabled = !(motdSet.equals("false") || motdSet.isBlank()); - - boolean chatEnabled = config.getBoolean("chat.enabled"); - boolean chatLocalEvents = config.getBoolean("chat.localEvents"); - String chatDefaultName = config.getString("chat.defaultName"); - int chatRadius = config.getInt("chat.radius"); - - boolean compassEnabled = config.getBoolean("compass.enabled"); - int compassWidth = config.getInt("compass.width"); - int compassPrecision = config.getInt("compass.precision"); - - boolean pomodoroEnabled = config.getBoolean("pomodoro.enabled"); - boolean pomodoroForce = config.getBoolean("pomodoro.force"); - - boolean updaterEnabled = config.getBoolean("updater.enabled"); - - boolean hardcoreEnabled = config.getBoolean("hardcore.enabled"); - double hardcoreChance = config.getDouble("hardcore.chance"); - - boolean sleepEnabled = config.getBoolean("sleep.enabled"); - boolean sleepInstant = config.getBoolean("sleep.instant"); - double sleepHeal = config.getDouble("sleep.heal"); - - boolean authEnabled = config.getBoolean("auth.enabled"); - boolean authForce = config.getBoolean("auth.force"); - String authHostname = config.getString("auth.domain"); - - boolean redstoneEnabled = config.getBoolean("retstone.enabled"); - String redstoneListen = config.getString("retstone.listen"); - - // this is processed when initing - Map knockbackModifiers = config.getConfigurationSection("knockback").getValues(false); - - boolean killswitchEnabled = config.getBoolean("killswitch.enabled"); - String killswitchListen = config.getString("killswitch.listen"); - - boolean swingEnabled = config.getBoolean("swing.enabled"); - - TweaksConfig.config = new TweaksConfig( - metrics, debug, locale, - worldborderExpand, worldborderHide, - brandEnabled, brandText, brandShowPing, brandShowMspt, - doorDoubleOpen, doorKnocking, - motdEnabled, motdSet, - chatEnabled, chatLocalEvents, chatDefaultName, chatRadius, - compassEnabled, compassWidth, compassPrecision, - pomodoroEnabled, pomodoroForce, - updaterEnabled, - hardcoreEnabled, hardcoreChance, - sleepEnabled, sleepInstant, sleepHeal, - authEnabled, authForce, authHostname, - redstoneEnabled, redstoneListen, - knockbackModifiers, - killswitchEnabled, killswitchListen, - swingEnabled - ); - - return TweaksConfig.config; - } -} \ No newline at end of file diff --git a/src/main/java/eu/m724/tweaks/TweaksPlugin.java b/src/main/java/eu/m724/tweaks/TweaksPlugin.java index 1f25ca3..8c6430e 100644 --- a/src/main/java/eu/m724/tweaks/TweaksPlugin.java +++ b/src/main/java/eu/m724/tweaks/TweaksPlugin.java @@ -7,6 +7,7 @@ package eu.m724.tweaks; import eu.m724.mstats.MStatsPlugin; +import eu.m724.tweaks.config.TweaksConfig; import eu.m724.tweaks.module.TweaksModule; import eu.m724.tweaks.module.alert.AlertModule; import eu.m724.tweaks.module.auth.AuthModule; @@ -46,7 +47,12 @@ public class TweaksPlugin extends MStatsPlugin { return; } - TweaksConfig config = TweaksConfig.load(this); + TweaksConfig config; + try { + config = TweaksConfig.load(this); + } catch (Exception e) { + throw new RuntimeException("Exception loading config", e); + } getLogger().setLevel(config.debug() ? Level.FINEST : Level.INFO); DebugLogger.logger = getLogger(); @@ -84,7 +90,6 @@ public class TweaksPlugin extends MStatsPlugin { if (config.worldborderExpand()) { TweaksModule.init(WorldBorderExpandModule.class); - } if (config.chatEnabled()) { diff --git a/src/main/java/eu/m724/tweaks/config/ConfigLoader.java b/src/main/java/eu/m724/tweaks/config/ConfigLoader.java new file mode 100644 index 0000000..36d7cd4 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/config/ConfigLoader.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2025 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.config; + +import org.bukkit.configuration.file.FileConfiguration; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +class ConfigLoader { + private final FileConfiguration configuration; + private final List missing = new ArrayList<>(); + + ConfigLoader(FileConfiguration configuration) { + this.configuration = configuration; + } + + List getMissing() { + return missing; + } + + TweaksConfig load() { + boolean metrics = configuration.getBoolean("metrics", false); + boolean debug = configuration.getBoolean("debug", false); + String locale = configuration.getString("locale", "US"); + + boolean worldborderExpand = getBoolean("worldborder.expand"); + boolean worldborderHide = getBoolean("worldborder.hide"); + + boolean brandEnabled = getBoolean("brand.enabled"); + String brandText = getString("brand.text"); + boolean brandShowPing = getBoolean("brand.showPing"); + boolean brandShowMspt = getBoolean("brand.showMspt"); + + boolean doorDoubleOpen = getBoolean("doors.doubleOpen"); + boolean doorKnocking = getBoolean("doors.knocking"); + + String motdSet = getString("motd.set"); + boolean motdEnabled = !(motdSet.equals("false") || motdSet.isBlank()); + + boolean chatEnabled = getBoolean("chat.enabled"); + boolean chatLocalEvents = getBoolean("chat.localEvents"); + String chatDefaultName = getString("chat.defaultName"); + int chatRadius = getInt("chat.radius"); + + boolean compassEnabled = getBoolean("compass.enabled"); + int compassWidth = getInt("compass.width"); + int compassPrecision = getInt("compass.precision"); + + boolean pomodoroEnabled = getBoolean("pomodoro.enabled"); + boolean pomodoroForce = getBoolean("pomodoro.force"); + + boolean updaterEnabled = getBoolean("updater.enabled"); + + boolean hardcoreEnabled = getBoolean("hardcore.enabled"); + double hardcoreChance = getDouble("hardcore.chance"); + + boolean sleepEnabled = getBoolean("sleep.enabled"); + boolean sleepInstant = getBoolean("sleep.instant"); + double sleepHeal = getDouble("sleep.heal"); + + boolean authEnabled = getBoolean("auth.enabled"); + boolean authForce = getBoolean("auth.force"); + String authHostname = getString("auth.domain"); + + boolean redstoneEnabled = getBoolean("retstone.enabled"); + String redstoneListen = getString("retstone.listen"); + + // this is processed when initing knockback module + Map knockbackModifiers = getValues("knockback"); + + boolean killswitchEnabled = getBoolean("killswitch.enabled"); + String killswitchListen = getString("killswitch.listen"); + + boolean swingEnabled = getBoolean("swing.enabled"); + + return new TweaksConfig( + metrics, debug, locale, + worldborderExpand, worldborderHide, + brandEnabled, brandText, brandShowPing, brandShowMspt, + doorDoubleOpen, doorKnocking, + motdEnabled, motdSet, + chatEnabled, chatLocalEvents, chatDefaultName, chatRadius, + compassEnabled, compassWidth, compassPrecision, + pomodoroEnabled, pomodoroForce, + updaterEnabled, + hardcoreEnabled, hardcoreChance, + sleepEnabled, sleepInstant, sleepHeal, + authEnabled, authForce, authHostname, + redstoneEnabled, redstoneListen, + knockbackModifiers, + killswitchEnabled, killswitchListen, + swingEnabled + ); + } + + private double getDouble(String key) { + if (!configuration.contains(key)) + missing.add(key); + + // we return the whatever default value + return configuration.getDouble(key); + } + + private int getInt(String key) { + if (!configuration.contains(key)) + missing.add(key); + + return configuration.getInt(key); + } + + private boolean getBoolean(String key) { + if (!configuration.contains(key)) + missing.add(key); + + return configuration.getBoolean(key); + } + + private String getString(String key) { + if (!configuration.contains(key)) + missing.add(key); + + return configuration.getString(key); + } + + private Map getValues(String key) { + var cs = configuration.getConfigurationSection(key); + + if (cs == null) { + missing.add(key); + // the default is null, which is bad + return new HashMap<>(); + } + + return cs.getValues(false); + } +} diff --git a/src/main/java/eu/m724/tweaks/config/MissingFieldsException.java b/src/main/java/eu/m724/tweaks/config/MissingFieldsException.java new file mode 100644 index 0000000..24f1be0 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/config/MissingFieldsException.java @@ -0,0 +1,22 @@ +/* + * Copyright (C) 2025 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.config; + +import java.util.List; + +class MissingFieldsException extends Exception { + private final List missing; + + MissingFieldsException(List missing) { + this.missing = missing; + } + + @Override + public String getMessage() { + return String.join(", ", missing); + } +} diff --git a/src/main/java/eu/m724/tweaks/config/TweaksConfig.java b/src/main/java/eu/m724/tweaks/config/TweaksConfig.java new file mode 100644 index 0000000..7442813 --- /dev/null +++ b/src/main/java/eu/m724/tweaks/config/TweaksConfig.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2025 Minecon724 + * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file + * in the project root for the full license text. + */ + +package eu.m724.tweaks.config; + +import org.bukkit.plugin.Plugin; + +import java.util.Map; + +public record TweaksConfig( + boolean metrics, + boolean debug, + String locale, + + boolean worldborderExpand, + boolean worldborderHide, + + boolean brandEnabled, + String brandText, + boolean brandShowPing, + boolean brandShowMspt, + + boolean doorDoubleOpen, + boolean doorKnocking, + + boolean motdEnabled, + String motdSet, + + boolean chatEnabled, + boolean chatLocalEvents, + String chatDefaultName, + int chatRadius, + + boolean compassEnabled, + int compassWidth, + int compassPrecision, + + boolean pomodoroEnabled, + boolean pomodoroForce, + + boolean updaterEnabled, + + boolean hardcoreEnabled, + double hardcoreChance, + + boolean sleepEnabled, + boolean sleepInstant, + double sleepHeal, + + boolean authEnabled, + boolean authForce, + String authDomain, + + boolean redstoneEnabled, + String redstoneListen, + + Map knockbackModifiers, + + boolean killswitchEnabled, + String killswitchListen, + + boolean swingEnabled +) { + public static final int CONFIG_VERSION = 2; + private static TweaksConfig config; + + public static TweaksConfig getConfig() { + return config; + } + + public static TweaksConfig load(Plugin plugin) throws Exception { + plugin.saveDefaultConfig(); + var pluginConfig = plugin.getConfig(); + + var configVersion = pluginConfig.getInt("magic number don't modify this", 0); + var exception = new RuntimeException("Config version is %d, expected %d".formatted(configVersion, CONFIG_VERSION)); + + if (configVersion == 0) { + throw exception; + } else if (configVersion < CONFIG_VERSION) { + throw new Exception("Please follow update instructions https://www.spigotmc.org/resources/tweaks724.121057/updates", exception); + } else if (configVersion > CONFIG_VERSION) { + throw new Exception("Did you downgrade the plugin? Delete config.yml and let the plugin re-create it", exception); + } + + var loader = new ConfigLoader(pluginConfig); + var config = loader.load(); + + if (loader.getMissing().isEmpty()) { + TweaksConfig.config = config; + return config; + } else { + throw new Exception( + "One or more fields are missing from config.yml. Did you follow the update instructions? https://www.spigotmc.org/resources/tweaks724.121057/updates", + new MissingFieldsException(loader.getMissing()) + ); + } + } + +} \ No newline at end of file diff --git a/src/main/java/eu/m724/tweaks/module/TweaksModule.java b/src/main/java/eu/m724/tweaks/module/TweaksModule.java index c6e83c1..e3f4de0 100644 --- a/src/main/java/eu/m724/tweaks/module/TweaksModule.java +++ b/src/main/java/eu/m724/tweaks/module/TweaksModule.java @@ -12,7 +12,7 @@ import com.comphenix.protocol.events.ListenerPriority; import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketEvent; import eu.m724.tweaks.DebugLogger; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import eu.m724.tweaks.TweaksPlugin; import org.bukkit.command.CommandExecutor; import org.bukkit.event.Listener; diff --git a/src/main/java/eu/m724/tweaks/module/auth/AuthCommands.java b/src/main/java/eu/m724/tweaks/module/auth/AuthCommands.java index 88f3874..5aae83f 100644 --- a/src/main/java/eu/m724/tweaks/module/auth/AuthCommands.java +++ b/src/main/java/eu/m724/tweaks/module/auth/AuthCommands.java @@ -7,7 +7,7 @@ package eu.m724.tweaks.module.auth; import eu.m724.tweaks.Language; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; diff --git a/src/main/java/eu/m724/tweaks/module/auth/AuthListener.java b/src/main/java/eu/m724/tweaks/module/auth/AuthListener.java index f61cb8c..bc4634c 100644 --- a/src/main/java/eu/m724/tweaks/module/auth/AuthListener.java +++ b/src/main/java/eu/m724/tweaks/module/auth/AuthListener.java @@ -7,7 +7,7 @@ package eu.m724.tweaks.module.auth; import eu.m724.tweaks.Language; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; diff --git a/src/main/java/eu/m724/tweaks/module/chat/ChatListener.java b/src/main/java/eu/m724/tweaks/module/chat/ChatListener.java index 20d8c2c..21b65f9 100644 --- a/src/main/java/eu/m724/tweaks/module/chat/ChatListener.java +++ b/src/main/java/eu/m724/tweaks/module/chat/ChatListener.java @@ -6,7 +6,7 @@ package eu.m724.tweaks.module.chat; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; diff --git a/src/main/java/eu/m724/tweaks/module/motd/MotdModule.java b/src/main/java/eu/m724/tweaks/module/motd/MotdModule.java index e02e28a..b0534cd 100644 --- a/src/main/java/eu/m724/tweaks/module/motd/MotdModule.java +++ b/src/main/java/eu/m724/tweaks/module/motd/MotdModule.java @@ -13,7 +13,7 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import eu.m724.tweaks.DebugLogger; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import eu.m724.tweaks.module.TweaksModule; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.chat.ComponentSerializer; diff --git a/src/main/java/eu/m724/tweaks/module/ping/F3NameListener.java b/src/main/java/eu/m724/tweaks/module/ping/F3NameListener.java index c4f7e00..bf43d14 100644 --- a/src/main/java/eu/m724/tweaks/module/ping/F3NameListener.java +++ b/src/main/java/eu/m724/tweaks/module/ping/F3NameListener.java @@ -10,7 +10,7 @@ import com.comphenix.protocol.PacketType; import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.*; import com.comphenix.protocol.reflect.StructureModifier; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import net.minecraft.network.protocol.common.custom.BrandPayload; import org.bukkit.entity.Player; import org.bukkit.plugin.Plugin; diff --git a/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroListener.java b/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroListener.java index bc04be2..077e36e 100644 --- a/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroListener.java +++ b/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroListener.java @@ -6,7 +6,7 @@ package eu.m724.tweaks.module.pomodoro; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import net.md_5.bungee.api.chat.ComponentBuilder; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; diff --git a/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroRunnable.java b/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroRunnable.java index 89faf34..6612d6d 100644 --- a/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroRunnable.java +++ b/src/main/java/eu/m724/tweaks/module/pomodoro/PomodoroRunnable.java @@ -6,7 +6,7 @@ package eu.m724.tweaks.module.pomodoro; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import net.md_5.bungee.api.ChatMessageType; import org.bukkit.Bukkit; import org.bukkit.Sound; diff --git a/src/main/java/eu/m724/tweaks/module/redstone/RedstoneModule.java b/src/main/java/eu/m724/tweaks/module/redstone/RedstoneModule.java index 36e4f86..b574786 100644 --- a/src/main/java/eu/m724/tweaks/module/redstone/RedstoneModule.java +++ b/src/main/java/eu/m724/tweaks/module/redstone/RedstoneModule.java @@ -7,7 +7,7 @@ package eu.m724.tweaks.module.redstone; import eu.m724.tweaks.DebugLogger; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import eu.m724.tweaks.module.TweaksModule; import java.io.IOException; diff --git a/src/main/java/eu/m724/tweaks/module/sleep/SleepListener.java b/src/main/java/eu/m724/tweaks/module/sleep/SleepListener.java index 9dc5986..b1d0f85 100644 --- a/src/main/java/eu/m724/tweaks/module/sleep/SleepListener.java +++ b/src/main/java/eu/m724/tweaks/module/sleep/SleepListener.java @@ -6,7 +6,7 @@ package eu.m724.tweaks.module.sleep; -import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.config.TweaksConfig; import org.bukkit.GameRule; import org.bukkit.attribute.Attribute; import org.bukkit.entity.Player;