tweaks724/src/main/java/eu/m724/tweaks/config/ConfigLoader.java
Minecon724 7de46b879b
Refactor MOTD configuration and improve file handling
Revised the MOTD configuration setup to use a direct boolean flag for enabling/disabling instead of relying on string checks. Updated file handling to use `Path` and modernized directory creation logic for better error handling and clarity. Removed unused code and cleaned up resource management in `MotdModule`.
2025-02-02 14:12:19 +01:00

143 lines
4.8 KiB
Java

/*
* 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");
boolean motdEnabled = getBoolean("motd.enabled");
String motdSet = getString("motd.set");
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);
}
}