From 7de46b879bb07fcdeea02240afa70d3adbd4b6d4 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Sun, 2 Feb 2025 14:12:19 +0100 Subject: [PATCH] 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`. --- .../eu/m724/tweaks/config/ConfigLoader.java | 2 +- .../m724/tweaks/module/motd/MotdModule.java | 29 +++++++++---------- src/main/resources/config.yml | 2 +- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/main/java/eu/m724/tweaks/config/ConfigLoader.java b/src/main/java/eu/m724/tweaks/config/ConfigLoader.java index 36d7cd4..cf725f9 100644 --- a/src/main/java/eu/m724/tweaks/config/ConfigLoader.java +++ b/src/main/java/eu/m724/tweaks/config/ConfigLoader.java @@ -41,8 +41,8 @@ class ConfigLoader { boolean doorDoubleOpen = getBoolean("doors.doubleOpen"); boolean doorKnocking = getBoolean("doors.knocking"); + boolean motdEnabled = getBoolean("motd.enabled"); String motdSet = getString("motd.set"); - boolean motdEnabled = !(motdSet.equals("false") || motdSet.isBlank()); boolean chatEnabled = getBoolean("chat.enabled"); boolean chatLocalEvents = getBoolean("chat.localEvents"); 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 b0534cd..4962f02 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,6 @@ import com.google.gson.JsonElement; import com.google.gson.JsonParseException; import com.google.gson.JsonParser; import eu.m724.tweaks.DebugLogger; -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; @@ -22,44 +21,42 @@ import net.minecraft.core.RegistryAccess; import net.minecraft.network.chat.Component; import net.minecraft.network.protocol.status.ServerStatus; -import java.io.File; import java.io.IOException; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; public class MotdModule extends TweaksModule { - private Component[] motds; - @Override protected void onInit() { - // TODO adding more MOTD features would require checking whether to enable set - - String motdSetName = TweaksConfig.getConfig().motdSet(); - String motdSetPath = "motd sets/" + motdSetName + ".txt"; - File motdSetsFile = new File(getPlugin().getDataFolder(), motdSetPath); + Path motdSetPath = getPlugin().getDataFolder().toPath().resolve("motd sets").resolve(getConfig().motdSet() + ".txt"); // create "motd sets" directory - motdSetsFile.getParentFile().mkdirs(); + try { + Files.createDirectories(motdSetPath); + } catch (IOException e) { + throw new RuntimeException(e); + } // if this is a builtin set - if (!motdSetsFile.exists() && getPlugin().hasResource(motdSetPath)) - getPlugin().saveResource(motdSetPath, false); + if (!Files.exists(motdSetPath) && getPlugin().hasResource("motd sets/" + motdSetPath.getFileName())) + getPlugin().saveResource("motd sets/" + motdSetPath.getFileName(), false); - if (!motdSetsFile.exists()) { - throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(motdSetName)); + if (!Files.exists(motdSetPath)) { + throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(getConfig().motdSet())); } String fileContent; try { - fileContent = Files.readString(motdSetsFile.toPath()); + fileContent = Files.readString(motdSetPath); } catch (IOException e) { throw new RuntimeException("Reading motd set", e); } // MOTDs are split with an empty line - motds = Arrays.stream(fileContent.split("\n\n")) + Component[] motds = Arrays.stream(fileContent.split("\n\n")) .map(entry -> { entry = entry.strip(); JsonElement json = null; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 5083716..6b48d5f 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -36,9 +36,9 @@ doors: knocking: true motd: + enabled: true # Name of the set containing the MOTDs # (random displayed every ping) - # "" or false to disable set: "example" chat: