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`.
This commit is contained in:
parent
cbe9f77cca
commit
7de46b879b
3 changed files with 15 additions and 18 deletions
|
@ -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");
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Add table
Reference in a new issue