diff --git a/pom.xml b/pom.xml index ccf98be..746c8e5 100644 --- a/pom.xml +++ b/pom.xml @@ -3,9 +3,9 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 io.github.townyadvanced - SimplePlugin - 0.0.1-SNAPSHOT - simpleplugin + TownySpawnPointLimits9000 + 1.0.0 + townyspawnointlimits9000 17 @@ -41,8 +41,8 @@ com.palmergames.bukkit.towny - Towny - 0.99.5.0 + towny + 0.100.2.7 provided diff --git a/src/main/java/io/github/townyadvanced/simpleplugin/TownyListener.java b/src/main/java/io/github/townyadvanced/simpleplugin/TownyListener.java deleted file mode 100644 index f24cbd0..0000000 --- a/src/main/java/io/github/townyadvanced/simpleplugin/TownyListener.java +++ /dev/null @@ -1,36 +0,0 @@ -package io.github.townyadvanced.simpleplugin; - -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Map; - -import org.bukkit.event.EventHandler; -import org.bukkit.event.Listener; -import org.bukkit.plugin.Plugin; - -import com.palmergames.bukkit.towny.event.TownyLoadedDatabaseEvent; -import com.palmergames.bukkit.towny.event.TranslationLoadEvent; -import com.palmergames.bukkit.towny.object.TranslationLoader; - -public class TownyListener implements Listener { - - /* Handle reloading MetaData when Towny's DB is loaded. */ -// @EventHandler - public void onTownyLoadDB(TownyLoadedDatabaseEvent event) { - } - - /* Handle re-adding the lang string into Towny when Towny reloads the Translation Registry. */ - @EventHandler - public void onTownyLoadLang(TranslationLoadEvent event) { - Plugin plugin = SimplePlugin.getPlugin(); - if (!SimplePlugin.hasLocale()) - return; - Path langFolderPath = Paths.get(plugin.getDataFolder().getPath()).resolve("lang"); - TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, SimplePlugin.class); - loader.load(); - Map> translations = loader.getTranslations(); - for (String language : translations.keySet()) - for (Map.Entry map : translations.get(language).entrySet()) - event.addTranslation(language, map.getKey(), map.getValue()); - } -} diff --git a/src/main/java/io/github/townyadvanced/simpleplugin/util/Messaging.java b/src/main/java/io/github/townyadvanced/simpleplugin/util/Messaging.java deleted file mode 100644 index 4bef3f1..0000000 --- a/src/main/java/io/github/townyadvanced/simpleplugin/util/Messaging.java +++ /dev/null @@ -1,35 +0,0 @@ -package io.github.townyadvanced.simpleplugin.util; - -import org.bukkit.Bukkit; -import org.bukkit.command.CommandSender; -import org.bukkit.entity.Player; - -import com.palmergames.bukkit.towny.TownyAPI; -import com.palmergames.bukkit.util.Colors; - -import io.github.townyadvanced.simpleplugin.SimplePlugin; - -public class Messaging { - - final static String prefix = SimplePlugin.getPrefix(); - - public static void sendErrorMsg(CommandSender sender, String message) { - //Ensure the sender is not null (i.e. is an online player who is not an npc) - if(sender != null) - sender.sendMessage(prefix + Colors.Red + message); - } - - public static void sendMsg(CommandSender sender, String message) { - //Ensure the sender is not null (i.e. is an online player who is not an npc) - if(sender != null) - sender.sendMessage(prefix + Colors.White + message); - } - - public static void sendGlobalMessage(String message) { - SimplePlugin.info(message); - for (Player player : Bukkit.getOnlinePlayers()) { - if (player != null && TownyAPI.getInstance().isTownyWorld(player.getWorld())) - sendMsg(player, message); - } - } -} diff --git a/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownyListener.java b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownyListener.java new file mode 100644 index 0000000..fb65b04 --- /dev/null +++ b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownyListener.java @@ -0,0 +1,79 @@ +package io.github.townyadvanced.townyspawnointlimits9000; + +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Map; + +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.plugin.Plugin; + +import com.palmergames.bukkit.towny.event.CancellableTownyEvent; +import com.palmergames.bukkit.towny.event.TranslationLoadEvent; +import com.palmergames.bukkit.towny.event.nation.NationSetSpawnEvent; +import com.palmergames.bukkit.towny.event.town.TownSetOutpostSpawnEvent; +import com.palmergames.bukkit.towny.event.town.TownSetSpawnEvent; +import com.palmergames.bukkit.towny.object.Translatable; +import com.palmergames.bukkit.towny.object.TranslationLoader; + +import io.github.townyadvanced.townyspawnointlimits9000.settings.Settings; + +public class TownyListener implements Listener { + + /* Handle re-adding the lang string into Towny when Towny reloads the Translation Registry. */ + @EventHandler + public void onTownyLoadLang(TranslationLoadEvent event) { + Plugin plugin = TownySpawnPointLimits9000.getPlugin(); + if (!TownySpawnPointLimits9000.hasLocale()) + return; + Path langFolderPath = Paths.get(plugin.getDataFolder().getPath()).resolve("lang"); + TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, TownySpawnPointLimits9000.class); + loader.load(); + Map> translations = loader.getTranslations(); + for (String language : translations.keySet()) + for (Map.Entry map : translations.get(language).entrySet()) + event.addTranslation(language, map.getKey(), map.getValue()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onTownSetSpawn(TownSetSpawnEvent event) { + if (!Settings.isSpawnYLevelLimitingEnabled()) + return; + testY(event, event.getPlayer(), event.getNewSpawn().getY()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onTownSetOutpostSpawn(TownSetOutpostSpawnEvent event) { + if (!Settings.isSpawnYLevelLimitingEnabled()) + return; + testY(event, event.getPlayer(), event.getNewSpawn().getY()); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onNationSetSpawn(NationSetSpawnEvent event) { + if (!Settings.isSpawnYLevelLimitingEnabled()) + return; + testY(event, event.getPlayer(), event.getNewSpawn().getY()); + } + + private void testY(CancellableTownyEvent event, Player player, double y) { + if (y <= Settings.getSpawningHighestYLevelAllowed()) + cancelEventTooLow(event, player); + else if (y >= Settings.getSpawningLowestYLevelAllowed()) + cancelEventTooHigh(event, player); + } + + private void cancelEventTooLow(CancellableTownyEvent event, Player player) { + event.setCancelMessage(Translatable.of("spawn_points9000_msg_err_you_cannot_set_this_spawn_point_below", + Settings.getSpawningLowestYLevelAllowed()).forLocale(player)); + event.setCancelled(true); + } + + private void cancelEventTooHigh(CancellableTownyEvent event, Player player) { + event.setCancelMessage(Translatable.of("spawn_points9000_msg_err_you_cannot_set_this_spawn_point_above", + Settings.getSpawningLowestYLevelAllowed()).forLocale(player)); + event.setCancelled(true); + } +} diff --git a/src/main/java/io/github/townyadvanced/simpleplugin/SimplePlugin.java b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownySpawnPointLimits9000.java similarity index 83% rename from src/main/java/io/github/townyadvanced/simpleplugin/SimplePlugin.java rename to src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownySpawnPointLimits9000.java index 66db067..9eba2bc 100644 --- a/src/main/java/io/github/townyadvanced/simpleplugin/SimplePlugin.java +++ b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/TownySpawnPointLimits9000.java @@ -1,4 +1,4 @@ -package io.github.townyadvanced.simpleplugin; +package io.github.townyadvanced.townyspawnointlimits9000; import java.nio.file.Path; import java.nio.file.Paths; @@ -16,18 +16,19 @@ import com.palmergames.bukkit.towny.scheduling.TaskScheduler; import com.palmergames.bukkit.towny.scheduling.impl.BukkitTaskScheduler; import com.palmergames.bukkit.towny.scheduling.impl.FoliaTaskScheduler; -import io.github.townyadvanced.simpleplugin.settings.Settings; -public class SimplePlugin extends JavaPlugin { +import io.github.townyadvanced.townyspawnointlimits9000.settings.Settings; - private static SimplePlugin plugin; +public class TownySpawnPointLimits9000 extends JavaPlugin { + + private static TownySpawnPointLimits9000 plugin; private final Object scheduler; - private static String requiredTownyVersion = "0.99.5.0"; - boolean hasConfig = false; - boolean hasLocale = false; + private static String requiredTownyVersion = "0.100.2.7"; + boolean hasConfig = true; + boolean hasLocale = true; boolean hasListeners = true; - public SimplePlugin() { + public TownySpawnPointLimits9000() { plugin = this; this.scheduler = townyVersionCheck() ? isFoliaClassPresent() ? new FoliaTaskScheduler(this) : new BukkitTaskScheduler(this) : null; } @@ -69,7 +70,7 @@ public static boolean loadLocalization(boolean reload) { try { Plugin plugin = getPlugin(); Path langFolderPath = Paths.get(plugin.getDataFolder().getPath()).resolve("lang"); - TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, SimplePlugin.class); + TranslationLoader loader = new TranslationLoader(langFolderPath, plugin, TownySpawnPointLimits9000.class); loader.load(); TownyAPI.getInstance().addTranslations(plugin, loader.getTranslations()); } catch (TownyInitException e) { @@ -91,7 +92,7 @@ public String getVersion() { return this.getDescription().getVersion(); } - public static SimplePlugin getPlugin() { + public static TownySpawnPointLimits9000 getPlugin() { return plugin; } diff --git a/src/main/java/io/github/townyadvanced/simpleplugin/settings/ConfigNodes.java b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/ConfigNodes.java similarity index 56% rename from src/main/java/io/github/townyadvanced/simpleplugin/settings/ConfigNodes.java rename to src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/ConfigNodes.java index f0bbf30..aae7b41 100644 --- a/src/main/java/io/github/townyadvanced/simpleplugin/settings/ConfigNodes.java +++ b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/ConfigNodes.java @@ -1,4 +1,4 @@ -package io.github.townyadvanced.simpleplugin.settings; +package io.github.townyadvanced.townyspawnointlimits9000.settings; public enum ConfigNodes { @@ -7,9 +7,20 @@ public enum ConfigNodes { "version.version", "", "# This is the current version. Please do not edit."), - LANGUAGE("language", - "english.yml", - "# The language file you wish to use"); + SPAWNING_ROOT("spawning.y_limits","",""), + SPAWNING_Y_LIMITS_ROOT("spawning.y_limits","",""), + SPAWNING_Y_LIMITS_ENABLED("spawning.y_limits.enabled", + "false", + "", + "# Should towns, nations be limited in how low they can set a spawn point, or an outpost spawn point?"), + SPAWNING_Y_LIMITS_LOWEST_Y_ALLOWED("spawning.y_limits.lowest_y_allowed", + "0", + "", + "# When limiting is configured, what is the lowest y level allowed?"), + SPAWNING_Y_LIMITS_HIGHEST_Y_ALLOWED("spawning.y_limits.highest_y_allowed", + "100", + "", + "# When limiting is configured, what is the highest y level allowed?"); private final String Root; private final String Default; diff --git a/src/main/java/io/github/townyadvanced/simpleplugin/settings/Settings.java b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/Settings.java similarity index 75% rename from src/main/java/io/github/townyadvanced/simpleplugin/settings/Settings.java rename to src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/Settings.java index a3760b3..a04fe27 100644 --- a/src/main/java/io/github/townyadvanced/simpleplugin/settings/Settings.java +++ b/src/main/java/io/github/townyadvanced/townyspawnointlimits9000/settings/Settings.java @@ -1,4 +1,4 @@ -package io.github.townyadvanced.simpleplugin.settings; +package io.github.townyadvanced.townyspawnointlimits9000.settings; import java.nio.file.Path; @@ -6,11 +6,11 @@ import com.palmergames.bukkit.towny.exceptions.initialization.TownyInitException; import com.palmergames.util.FileMgmt; -import io.github.townyadvanced.simpleplugin.SimplePlugin; +import io.github.townyadvanced.townyspawnointlimits9000.TownySpawnPointLimits9000; public class Settings { private static CommentedConfiguration config, newConfig; - private static Path configPath = SimplePlugin.getPlugin().getDataFolder().toPath().resolve("config.yml"); + private static Path configPath = TownySpawnPointLimits9000.getPlugin().getDataFolder().toPath().resolve("config.yml"); public static void loadConfig() { if (FileMgmt.checkOrCreateFile(configPath.toString())) { @@ -20,7 +20,7 @@ public static void loadConfig() { if (!config.load()) throw new TownyInitException("Failed to load config.yml.", TownyInitException.TownyError.MAIN_CONFIG); - setDefaults(SimplePlugin.getPlugin().getVersion(), configPath); + setDefaults(TownySpawnPointLimits9000.getPlugin().getVersion(), configPath); config.save(); } } @@ -77,7 +77,7 @@ public static String getString(String root, String def) { private static void sendError(String msg) { - SimplePlugin.severe("Error could not read " + msg); + TownySpawnPointLimits9000.severe("Error could not read " + msg); } public static boolean getBoolean(ConfigNodes node) { @@ -110,4 +110,16 @@ public static String getString(ConfigNodes node) { return config.getString(node.getRoot().toLowerCase(), node.getDefault()); } + + public static boolean isSpawnYLevelLimitingEnabled() { + return getBoolean(ConfigNodes.SPAWNING_Y_LIMITS_ENABLED); + } + + public static double getSpawningLowestYLevelAllowed() { + return getDouble(ConfigNodes.SPAWNING_Y_LIMITS_LOWEST_Y_ALLOWED); + } + + public static double getSpawningHighestYLevelAllowed() { + return getDouble(ConfigNodes.SPAWNING_Y_LIMITS_HIGHEST_Y_ALLOWED); + } } diff --git a/src/main/resources/lang/en-US.yml b/src/main/resources/lang/en-US.yml index 5ae2954..0545927 100644 --- a/src/main/resources/lang/en-US.yml +++ b/src/main/resources/lang/en-US.yml @@ -23,3 +23,6 @@ description: > # DarkPurple = &9, LightGreen = &a, LightBlue = &b # Rose = &c, LightPurple = &d, Yellow = &e, White = &f + +spawn_points9000_msg_err_you_cannot_set_this_spawn_point_below: "You cannot set this spawn point below y level %s." +spawn_points9000_msg_err_you_cannot_set_this_spawn_point_above: "You cannot set this spawn point above y level %s." \ No newline at end of file