Fix MOTD sets
All checks were successful
/ deploy (push) Successful in 2m8s

This commit is contained in:
Minecon724 2024-12-02 17:55:15 +01:00
parent afa69c059b
commit 2ded40ca0f
Signed by: Minecon724
GPG key ID: 3CCC4D267742C8E8
2 changed files with 14 additions and 5 deletions

View file

@ -96,4 +96,8 @@ public class TweaksPlugin extends JavaPlugin {
getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0)); getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0));
} }
public boolean hasResource(String resource) {
return this.getClassLoader().getResource(resource) != null;
}
} }

View file

@ -12,6 +12,7 @@ import com.comphenix.protocol.events.*;
import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.reflect.StructureModifier;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.TweaksConfig;
import eu.m724.tweaks.TweaksPlugin;
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;
import net.minecraft.SharedConstants; import net.minecraft.SharedConstants;
@ -28,11 +29,11 @@ import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
public class MotdManager { public class MotdManager {
private final Plugin plugin; private final TweaksPlugin plugin;
private Component[] motds; private Component[] motds;
public MotdManager(Plugin plugin) { public MotdManager(TweaksPlugin plugin) {
this.plugin = plugin; this.plugin = plugin;
} }
@ -40,14 +41,18 @@ public class MotdManager {
// TODO adding more MOTD features would require checking whether to enable set // TODO adding more MOTD features would require checking whether to enable set
String motdSetName = TweaksConfig.getConfig().motdSet(); String motdSetName = TweaksConfig.getConfig().motdSet();
File motdSetsFile = new File(plugin.getDataFolder() + "/motd sets/" + motdSetName + ".txt"); String motdSetPath = "motd sets/" + motdSetName + ".txt";
File motdSetsFile = new File(plugin.getDataFolder(), motdSetPath);
// create "motd sets" directory
motdSetsFile.getParentFile().mkdirs(); motdSetsFile.getParentFile().mkdirs();
// if this is a builtin set // if this is a builtin set
plugin.saveResource("motd sets/" + motdSetName + ".txt", false); if (!motdSetsFile.exists() && plugin.hasResource(motdSetPath))
plugin.saveResource(motdSetPath, false);
if (!motdSetsFile.exists()) { if (!motdSetsFile.exists()) {
throw new RuntimeException("MOTD set \"%s\" doesn't exist (%s)".formatted(motdSetName, motdSetsFile.getPath())); throw new RuntimeException("MOTD set \"%s\" doesn't exist".formatted(motdSetName));
} }
String fileContent = Files.readString(motdSetsFile.toPath()); String fileContent = Files.readString(motdSetsFile.toPath());