diff --git a/src/main/java/eu/m724/tweaks/TweaksPlugin.java b/src/main/java/eu/m724/tweaks/TweaksPlugin.java index 6143750..7e1e450 100644 --- a/src/main/java/eu/m724/tweaks/TweaksPlugin.java +++ b/src/main/java/eu/m724/tweaks/TweaksPlugin.java @@ -96,4 +96,8 @@ public class TweaksPlugin extends JavaPlugin { getLogger().info("Took %.3f milliseconds".formatted((System.nanoTime() - start) / 1000000.0)); } + + public boolean hasResource(String resource) { + return this.getClassLoader().getResource(resource) != null; + } } diff --git a/src/main/java/eu/m724/tweaks/motd/MotdManager.java b/src/main/java/eu/m724/tweaks/motd/MotdManager.java index 8494c2f..4ef367b 100644 --- a/src/main/java/eu/m724/tweaks/motd/MotdManager.java +++ b/src/main/java/eu/m724/tweaks/motd/MotdManager.java @@ -12,6 +12,7 @@ import com.comphenix.protocol.events.*; import com.comphenix.protocol.reflect.StructureModifier; import com.google.gson.JsonElement; import eu.m724.tweaks.TweaksConfig; +import eu.m724.tweaks.TweaksPlugin; import net.md_5.bungee.api.chat.TextComponent; import net.md_5.bungee.chat.ComponentSerializer; import net.minecraft.SharedConstants; @@ -28,11 +29,11 @@ import java.util.Optional; import java.util.concurrent.ThreadLocalRandom; public class MotdManager { - private final Plugin plugin; + private final TweaksPlugin plugin; private Component[] motds; - public MotdManager(Plugin plugin) { + public MotdManager(TweaksPlugin plugin) { this.plugin = plugin; } @@ -40,14 +41,18 @@ public class MotdManager { // TODO adding more MOTD features would require checking whether to enable set 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(); + // 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()) { - 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());