From 0349b64cd026c0b42217599c66c65eb77e289c2b Mon Sep 17 00:00:00 2001 From: LlmDl Date: Wed, 1 May 2024 13:28:43 -0500 Subject: [PATCH] Add ability to limit setting spawn points lower than a configurable Y level. Closes #7306 --- .../bukkit/config/ConfigNodes.java | 10 +++++++ .../bukkit/towny/TownySettings.java | 8 +++++ .../towny/listeners/TownyCustomListener.java | 30 +++++++++++++++++++ Towny/src/main/resources/lang/en-US.yml | 4 ++- 4 files changed, 51 insertions(+), 1 deletion(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/config/ConfigNodes.java b/Towny/src/main/java/com/palmergames/bukkit/config/ConfigNodes.java index ac94146ec8..7c70aeb363 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/config/ConfigNodes.java +++ b/Towny/src/main/java/com/palmergames/bukkit/config/ConfigNodes.java @@ -1427,6 +1427,16 @@ public enum ConfigNodes { "", "# Number of seconds that must pass before a player who is not a member or ally can use /n spawn."), + 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_RESPAWN_ROOT("spawning.respawning","",""), SPAWNING_TOWN_RESPAWN( "spawning.respawning.town_respawn", diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/TownySettings.java b/Towny/src/main/java/com/palmergames/bukkit/towny/TownySettings.java index c328cbd10d..d094f571dc 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/TownySettings.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/TownySettings.java @@ -3780,5 +3780,13 @@ public static String getDefaultResidentAbout() { public static double maxBuyTownPrice() { return getDouble(ConfigNodes.GTOWN_SETTINGS_MAX_BUYTOWN_PRICE); } + + public static boolean isSpawnYLevelLimitingEnabled() { + return getBoolean(ConfigNodes.SPAWNING_Y_LIMITS_ENABLED); + } + + public static double getSpawningLowestYLevelAllowed() { + return getDouble(ConfigNodes.SPAWNING_Y_LIMITS_LOWEST_Y_ALLOWED); + } } diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/listeners/TownyCustomListener.java b/Towny/src/main/java/com/palmergames/bukkit/towny/listeners/TownyCustomListener.java index bc63b19d8a..da5a01207b 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/listeners/TownyCustomListener.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/listeners/TownyCustomListener.java @@ -21,7 +21,10 @@ import com.palmergames.bukkit.towny.event.TownRemoveResidentEvent; import com.palmergames.bukkit.towny.event.damage.TownyPlayerDamagePlayerEvent; import com.palmergames.bukkit.towny.event.nation.NationPreTownLeaveEvent; +import com.palmergames.bukkit.towny.event.nation.NationSetSpawnEvent; import com.palmergames.bukkit.towny.event.town.TownPreUnclaimCmdEvent; +import com.palmergames.bukkit.towny.event.town.TownSetOutpostSpawnEvent; +import com.palmergames.bukkit.towny.event.town.TownSetSpawnEvent; import com.palmergames.bukkit.towny.exceptions.TownyException; import com.palmergames.bukkit.towny.object.CellSurface; import com.palmergames.bukkit.towny.object.PlayerCache; @@ -322,4 +325,31 @@ private void attemptPlayerCacheReset(Player player, WorldCoord worldCoord) { return; Towny.getPlugin().resetCache(player); } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onTownSetSpawn(TownSetSpawnEvent event) { + if (!TownySettings.isSpawnYLevelLimitingEnabled() + || event.getNewSpawn().getY() >= TownySettings.getSpawningLowestYLevelAllowed()) + return; + event.setCancelMessage(Translatable.of("msg_err_you_cannot_set_this_spawn_point_below", TownySettings.getSpawningLowestYLevelAllowed()).forLocale(event.getPlayer())); + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onTownSetOutpostSpawn(TownSetOutpostSpawnEvent event) { + if (!TownySettings.isSpawnYLevelLimitingEnabled() + || event.getNewSpawn().getY() >= TownySettings.getSpawningLowestYLevelAllowed()) + return; + event.setCancelMessage(Translatable.of("msg_err_you_cannot_set_this_spawn_point_below", TownySettings.getSpawningLowestYLevelAllowed()).forLocale(event.getPlayer())); + event.setCancelled(true); + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onNationSetSpawn(NationSetSpawnEvent event) { + if (!TownySettings.isSpawnYLevelLimitingEnabled() + || event.getNewSpawn().getY() >= TownySettings.getSpawningLowestYLevelAllowed()) + return; + event.setCancelMessage(Translatable.of("msg_err_you_cannot_set_this_spawn_point_below", TownySettings.getSpawningLowestYLevelAllowed()).forLocale(event.getPlayer())); + event.setCancelled(true); + } } diff --git a/Towny/src/main/resources/lang/en-US.yml b/Towny/src/main/resources/lang/en-US.yml index 59adb31e20..10c7a8d385 100644 --- a/Towny/src/main/resources/lang/en-US.yml +++ b/Towny/src/main/resources/lang/en-US.yml @@ -2524,4 +2524,6 @@ msg_recieved_refund_for_deleted_object: "You have received the bank balance of y msg_err_you_already_own_this_plot: "You already own this plot." -msg_err_you_cannot_delete_this_nation: "You have been prevented from deleting this nation." \ No newline at end of file +msg_err_you_cannot_delete_this_nation: "You have been prevented from deleting this nation." + +msg_err_you_cannot_set_this_spawn_point_below: "You cannot set this spawn point below y level %s." \ No newline at end of file