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 doorDoubleOpen = getBoolean("doors.doubleOpen");
|
||||||
boolean doorKnocking = getBoolean("doors.knocking");
|
boolean doorKnocking = getBoolean("doors.knocking");
|
||||||
|
|
||||||
|
boolean motdEnabled = getBoolean("motd.enabled");
|
||||||
String motdSet = getString("motd.set");
|
String motdSet = getString("motd.set");
|
||||||
boolean motdEnabled = !(motdSet.equals("false") || motdSet.isBlank());
|
|
||||||
|
|
||||||
boolean chatEnabled = getBoolean("chat.enabled");
|
boolean chatEnabled = getBoolean("chat.enabled");
|
||||||
boolean chatLocalEvents = getBoolean("chat.localEvents");
|
boolean chatLocalEvents = getBoolean("chat.localEvents");
|
||||||
|
|
|
@ -13,7 +13,6 @@ 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.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;
|
||||||
|
@ -22,44 +21,42 @@ import net.minecraft.core.RegistryAccess;
|
||||||
import net.minecraft.network.chat.Component;
|
import net.minecraft.network.chat.Component;
|
||||||
import net.minecraft.network.protocol.status.ServerStatus;
|
import net.minecraft.network.protocol.status.ServerStatus;
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
public class MotdModule extends TweaksModule {
|
public class MotdModule extends TweaksModule {
|
||||||
private Component[] motds;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onInit() {
|
protected void onInit() {
|
||||||
// TODO adding more MOTD features would require checking whether to enable set
|
Path motdSetPath = getPlugin().getDataFolder().toPath().resolve("motd sets").resolve(getConfig().motdSet() + ".txt");
|
||||||
|
|
||||||
String motdSetName = TweaksConfig.getConfig().motdSet();
|
|
||||||
String motdSetPath = "motd sets/" + motdSetName + ".txt";
|
|
||||||
File motdSetsFile = new File(getPlugin().getDataFolder(), motdSetPath);
|
|
||||||
|
|
||||||
// create "motd sets" directory
|
// 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 this is a builtin set
|
||||||
if (!motdSetsFile.exists() && getPlugin().hasResource(motdSetPath))
|
if (!Files.exists(motdSetPath) && getPlugin().hasResource("motd sets/" + motdSetPath.getFileName()))
|
||||||
getPlugin().saveResource(motdSetPath, false);
|
getPlugin().saveResource("motd sets/" + motdSetPath.getFileName(), false);
|
||||||
|
|
||||||
if (!motdSetsFile.exists()) {
|
if (!Files.exists(motdSetPath)) {
|
||||||
throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(motdSetName));
|
throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(getConfig().motdSet()));
|
||||||
}
|
}
|
||||||
|
|
||||||
String fileContent;
|
String fileContent;
|
||||||
try {
|
try {
|
||||||
fileContent = Files.readString(motdSetsFile.toPath());
|
fileContent = Files.readString(motdSetPath);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException("Reading motd set", e);
|
throw new RuntimeException("Reading motd set", e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// MOTDs are split with an empty line
|
// MOTDs are split with an empty line
|
||||||
motds = Arrays.stream(fileContent.split("\n\n"))
|
Component[] motds = Arrays.stream(fileContent.split("\n\n"))
|
||||||
.map(entry -> {
|
.map(entry -> {
|
||||||
entry = entry.strip();
|
entry = entry.strip();
|
||||||
JsonElement json = null;
|
JsonElement json = null;
|
||||||
|
|
|
@ -36,9 +36,9 @@ doors:
|
||||||
knocking: true
|
knocking: true
|
||||||
|
|
||||||
motd:
|
motd:
|
||||||
|
enabled: true
|
||||||
# Name of the set containing the MOTDs
|
# Name of the set containing the MOTDs
|
||||||
# (random displayed every ping)
|
# (random displayed every ping)
|
||||||
# "" or false to disable
|
|
||||||
set: "example"
|
set: "example"
|
||||||
|
|
||||||
chat:
|
chat:
|
||||||
|
|
Loading…
Add table
Reference in a new issue