156 lines
5.7 KiB
Java
156 lines
5.7 KiB
Java
package de.nexuslobby.utils;
|
|
|
|
import org.bukkit.configuration.file.FileConfiguration;
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Validiert Config-Werte und setzt Default-Werte wenn nötig
|
|
*/
|
|
public class ConfigValidator {
|
|
|
|
private final JavaPlugin plugin;
|
|
private final FileConfiguration config;
|
|
private final List<String> warnings = new ArrayList<>();
|
|
private boolean modified = false;
|
|
|
|
public ConfigValidator(JavaPlugin plugin, FileConfiguration config) {
|
|
this.plugin = plugin;
|
|
this.config = config;
|
|
}
|
|
|
|
/**
|
|
* Validiert alle Config-Werte und gibt Warnings aus
|
|
*/
|
|
public void validate() {
|
|
validateSpawn();
|
|
validateLobbySettings();
|
|
validateTablist();
|
|
validateBall();
|
|
validateModules();
|
|
|
|
// Warnings ausgeben
|
|
if (!warnings.isEmpty()) {
|
|
plugin.getLogger().warning("=== Config-Validierung ===");
|
|
warnings.forEach(msg -> plugin.getLogger().warning(" " + msg));
|
|
plugin.getLogger().warning("=========================");
|
|
}
|
|
|
|
// Config speichern wenn Änderungen vorgenommen wurden
|
|
if (modified) {
|
|
plugin.saveConfig();
|
|
plugin.getLogger().info("Config wurde mit Default-Werten aktualisiert.");
|
|
}
|
|
}
|
|
|
|
private void validateSpawn() {
|
|
ensureDefault("spawn.world", "world");
|
|
ensureDefault("spawn.x", 0.5);
|
|
ensureDefault("spawn.y", 64.0);
|
|
ensureDefault("spawn.z", 0.5);
|
|
ensureDefault("spawn.yaw", 0.0);
|
|
ensureDefault("spawn.pitch", 0.0);
|
|
|
|
// Validiere Y-Koordinate
|
|
double y = config.getDouble("spawn.y", 64.0);
|
|
if (y < -64 || y > 320) {
|
|
warnings.add("spawn.y (" + y + ") außerhalb des gültigen Bereichs (-64 bis 320)");
|
|
}
|
|
}
|
|
|
|
private void validateLobbySettings() {
|
|
ensureDefault("lobby.allow-fly", false);
|
|
ensureDefault("lobby.pvp-enabled", false);
|
|
ensureDefault("lobby.build-enabled", false);
|
|
ensureDefault("lobby.default-gamemode", "Adventure");
|
|
ensureDefault("lobby.clear-inventory-on-join", true);
|
|
|
|
// Validiere GameMode
|
|
String gamemode = config.getString("lobby.default-gamemode", "Adventure");
|
|
if (!isValidGameMode(gamemode)) {
|
|
warnings.add("lobby.default-gamemode '" + gamemode + "' ist ungültig. Nutze: Survival, Creative, Adventure oder Spectator");
|
|
}
|
|
}
|
|
|
|
private void validateTablist() {
|
|
ensureDefault("tablist.enabled", true);
|
|
ensureDefault("tablist.header", "&6Willkommen auf &eNexusLobby");
|
|
ensureDefault("tablist.footer", "&7Viel Spaß!");
|
|
ensureDefault("tablist.refresh-interval", 40);
|
|
|
|
// Validiere Refresh-Interval
|
|
int interval = config.getInt("tablist.refresh-interval", 40);
|
|
if (interval < 1) {
|
|
warnings.add("tablist.refresh-interval (" + interval + ") ist zu klein. Minimum ist 1 Tick.");
|
|
config.set("tablist.refresh-interval", 1);
|
|
modified = true;
|
|
} else if (interval > 200) {
|
|
warnings.add("tablist.refresh-interval (" + interval + ") ist sehr hoch. Empfohlen: 20-100 Ticks.");
|
|
}
|
|
}
|
|
|
|
private void validateBall() {
|
|
ensureDefault("ball.respawn_delay", 60);
|
|
|
|
// Validiere Respawn-Delay
|
|
long delay = config.getLong("ball.respawn_delay", 60);
|
|
if (delay < 5) {
|
|
warnings.add("ball.respawn_delay (" + delay + "s) ist zu kurz. Minimum empfohlen: 5 Sekunden.");
|
|
} else if (delay > 600) {
|
|
warnings.add("ball.respawn_delay (" + delay + "s) ist sehr lang. Empfohlen: 30-120 Sekunden.");
|
|
}
|
|
|
|
// Prüfe ob Spawn gesetzt ist
|
|
if (!config.contains("ball.spawn")) {
|
|
warnings.add("ball.spawn nicht gesetzt. Nutze /nexuslobby ball setspawn");
|
|
}
|
|
}
|
|
|
|
private void validateModules() {
|
|
// Worldborder
|
|
if (config.contains("worldborder.enabled")) {
|
|
ensureDefault("worldborder.type", "SQUARE");
|
|
ensureDefault("worldborder.radius", 50.0);
|
|
|
|
double radius = config.getDouble("worldborder.radius", 50.0);
|
|
if (radius < 10) {
|
|
warnings.add("worldborder.radius (" + radius + ") ist sehr klein. Minimum empfohlen: 10 Blöcke.");
|
|
} else if (radius > 10000) {
|
|
warnings.add("worldborder.radius (" + radius + ") ist extrem groß. Performance-Warnung!");
|
|
}
|
|
|
|
String type = config.getString("worldborder.type", "SQUARE");
|
|
if (!type.equalsIgnoreCase("SQUARE") && !type.equalsIgnoreCase("CIRCLE")) {
|
|
warnings.add("worldborder.type '" + type + "' ist ungültig. Nutze: SQUARE oder CIRCLE");
|
|
}
|
|
}
|
|
|
|
// Items Modul
|
|
if (config.contains("items.lobby-tools")) {
|
|
// Prüfe ob Slot-Nummern gültig sind (0-8)
|
|
for (String toolKey : config.getConfigurationSection("items.lobby-tools").getKeys(false)) {
|
|
int slot = config.getInt("items.lobby-tools." + toolKey + ".slot", -1);
|
|
if (slot < 0 || slot > 8) {
|
|
warnings.add("items.lobby-tools." + toolKey + ".slot (" + slot + ") ist ungültig (0-8)");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ensureDefault(String path, Object defaultValue) {
|
|
if (!config.contains(path)) {
|
|
config.set(path, defaultValue);
|
|
modified = true;
|
|
}
|
|
}
|
|
|
|
private boolean isValidGameMode(String mode) {
|
|
return mode.equalsIgnoreCase("Survival") ||
|
|
mode.equalsIgnoreCase("Creative") ||
|
|
mode.equalsIgnoreCase("Adventure") ||
|
|
mode.equalsIgnoreCase("Spectator");
|
|
}
|
|
}
|