Improve config loader
All checks were successful
/ build (push) Successful in 50s

This commit is contained in:
Minecon724 2025-01-24 18:42:39 +01:00
parent 7b65be86ad
commit 29f79a8771
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
15 changed files with 285 additions and 177 deletions

View file

@ -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<String, Object> 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<String, Object> 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;
}
}

View file

@ -7,6 +7,7 @@
package eu.m724.tweaks; package eu.m724.tweaks;
import eu.m724.mstats.MStatsPlugin; import eu.m724.mstats.MStatsPlugin;
import eu.m724.tweaks.config.TweaksConfig;
import eu.m724.tweaks.module.TweaksModule; import eu.m724.tweaks.module.TweaksModule;
import eu.m724.tweaks.module.alert.AlertModule; import eu.m724.tweaks.module.alert.AlertModule;
import eu.m724.tweaks.module.auth.AuthModule; import eu.m724.tweaks.module.auth.AuthModule;
@ -46,7 +47,12 @@ public class TweaksPlugin extends MStatsPlugin {
return; 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); getLogger().setLevel(config.debug() ? Level.FINEST : Level.INFO);
DebugLogger.logger = getLogger(); DebugLogger.logger = getLogger();
@ -84,7 +90,6 @@ public class TweaksPlugin extends MStatsPlugin {
if (config.worldborderExpand()) { if (config.worldborderExpand()) {
TweaksModule.init(WorldBorderExpandModule.class); TweaksModule.init(WorldBorderExpandModule.class);
} }
if (config.chatEnabled()) { if (config.chatEnabled()) {

View file

@ -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<String> missing = new ArrayList<>();
ConfigLoader(FileConfiguration configuration) {
this.configuration = configuration;
}
List<String> 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<String, Object> 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<String, Object> 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);
}
}

View file

@ -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<String> missing;
MissingFieldsException(List<String> missing) {
this.missing = missing;
}
@Override
public String getMessage() {
return String.join(", ", missing);
}
}

View file

@ -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<String, Object> 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())
);
}
}
}

View file

@ -12,7 +12,7 @@ import com.comphenix.protocol.events.ListenerPriority;
import com.comphenix.protocol.events.PacketAdapter; import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketEvent; import com.comphenix.protocol.events.PacketEvent;
import eu.m724.tweaks.DebugLogger; import eu.m724.tweaks.DebugLogger;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.config.TweaksConfig;
import eu.m724.tweaks.TweaksPlugin; import eu.m724.tweaks.TweaksPlugin;
import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandExecutor;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;

View file

@ -7,7 +7,7 @@
package eu.m724.tweaks.module.auth; package eu.m724.tweaks.module.auth;
import eu.m724.tweaks.Language; 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.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.ClickEvent;

View file

@ -7,7 +7,7 @@
package eu.m724.tweaks.module.auth; package eu.m724.tweaks.module.auth;
import eu.m724.tweaks.Language; import eu.m724.tweaks.Language;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.config.TweaksConfig;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;

View file

@ -6,7 +6,7 @@
package eu.m724.tweaks.module.chat; 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.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ComponentBuilder; import net.md_5.bungee.api.chat.ComponentBuilder;

View file

@ -13,7 +13,7 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonParseException; import com.google.gson.JsonParseException;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import eu.m724.tweaks.DebugLogger; import eu.m724.tweaks.DebugLogger;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.config.TweaksConfig;
import eu.m724.tweaks.module.TweaksModule; import eu.m724.tweaks.module.TweaksModule;
import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.chat.ComponentSerializer; import net.md_5.bungee.chat.ComponentSerializer;

View file

@ -10,7 +10,7 @@ import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.events.*; import com.comphenix.protocol.events.*;
import com.comphenix.protocol.reflect.StructureModifier; 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 net.minecraft.network.protocol.common.custom.BrandPayload;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;

View file

@ -6,7 +6,7 @@
package eu.m724.tweaks.module.pomodoro; 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 net.md_5.bungee.api.chat.ComponentBuilder;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;

View file

@ -6,7 +6,7 @@
package eu.m724.tweaks.module.pomodoro; 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 net.md_5.bungee.api.ChatMessageType;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Sound; import org.bukkit.Sound;

View file

@ -7,7 +7,7 @@
package eu.m724.tweaks.module.redstone; package eu.m724.tweaks.module.redstone;
import eu.m724.tweaks.DebugLogger; import eu.m724.tweaks.DebugLogger;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.config.TweaksConfig;
import eu.m724.tweaks.module.TweaksModule; import eu.m724.tweaks.module.TweaksModule;
import java.io.IOException; import java.io.IOException;

View file

@ -6,7 +6,7 @@
package eu.m724.tweaks.module.sleep; package eu.m724.tweaks.module.sleep;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.config.TweaksConfig;
import org.bukkit.GameRule; import org.bukkit.GameRule;
import org.bukkit.attribute.Attribute; import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;