From fd3e8ac902a2a9955eb8242179bd926c967bd3a7 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Fri, 26 Apr 2024 10:05:08 -0500 Subject: [PATCH 1/4] Fix nation deleted message showing when a predeletenationevent iscancelled. Uses the nation.exists to determine if the removal of the nation was successful or not, and displays messages to match the outcome. Closes #7380. Supercedes #7381. --- .../bukkit/towny/command/NationCommand.java | 12 ++++++++++-- .../bukkit/towny/command/TownyAdminCommand.java | 9 ++++++--- .../bukkit/towny/listeners/TownyCustomListener.java | 7 +++++++ .../com/palmergames/bukkit/towny/object/Town.java | 3 ++- .../bukkit/towny/tasks/DailyTimerTask.java | 7 +++++-- .../com/palmergames/bukkit/towny/utils/TownUtil.java | 5 ++++- Towny/src/main/resources/lang/en-US.yml | 4 +++- 7 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java index 4023aa28a2..07011c4b34 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/NationCommand.java @@ -1062,8 +1062,12 @@ public void nationDelete(Player player, String[] split) throws TownyException { } Confirmation.runOnAccept(() -> { - TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation.getName())); TownyUniverse.getInstance().getDataSource().removeNation(nation); + if (nation.exists()) { // The PreDeleteNationEvent was cancelled. + TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_you_cannot_delete_this_nation")); + return; + } + TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation.getName())); if (tooManyResidents) ResidentUtil.reduceResidentCountToFitTownMaxPop(town); }) @@ -1076,8 +1080,12 @@ public void nationDelete(Player player, String[] split) throws TownyException { Nation nation = getNationOrThrow(split[0]); Confirmation.runOnAccept(() -> { - TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation.getName())); TownyUniverse.getInstance().getDataSource().removeNation(nation); + if (nation.exists()) {// The PreDeleteNationEvent was cancelled. + TownyMessaging.sendErrorMsg(player, Translatable.of("msg_err_you_cannot_delete_this_nation")); + return; + } + TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation.getName())); }).sendTo(player); } diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java index 6a2961d055..430b0a763d 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/command/TownyAdminCommand.java @@ -1663,11 +1663,14 @@ public void parseAdminNationCommand(CommandSender sender, String[] split) throws case "delete": checkPermOrThrow(sender, PermissionNodes.TOWNY_COMMAND_TOWNYADMIN_NATION_DELETE.getNode()); Confirmation.runOnAccept(() -> { - if (sender instanceof Player) - TownyMessaging.sendMsg(sender, Translatable.of("nation_deleted_by_admin", nation.getName())); - TownyUniverse.getInstance().getDataSource().removeNation(nation); + if (nation.exists()) { + TownyMessaging.sendErrorMsg(sender, Translatable.of("msg_err_you_cannot_delete_this_nation")); + return; + } TownyMessaging.sendGlobalMessage(Translatable.of("MSG_DEL_NATION", nation.getName())); + if (sender instanceof Player) + TownyMessaging.sendMsg(sender, Translatable.of("nation_deleted_by_admin", nation.getName())); }).sendTo(sender); break; case "meta": 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..11f56cf3cf 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 @@ -14,6 +14,7 @@ import com.palmergames.bukkit.towny.event.ChunkNotificationEvent; import com.palmergames.bukkit.towny.event.NewTownEvent; import com.palmergames.bukkit.towny.event.PlayerChangePlotEvent; +import com.palmergames.bukkit.towny.event.PreDeleteNationEvent; import com.palmergames.bukkit.towny.event.SpawnEvent; import com.palmergames.bukkit.towny.event.TownAddResidentEvent; import com.palmergames.bukkit.towny.event.TownBlockPermissionChangeEvent; @@ -59,6 +60,12 @@ public TownyCustomListener(Towny instance) { plugin = instance; } + @EventHandler + public void onNationDelete(PreDeleteNationEvent event) { + System.out.println("Cancelling nation delete."); + event.setCancelled(true); + } + @EventHandler(priority = EventPriority.NORMAL) public void onPlayerChangePlotEvent(PlayerChangePlotEvent event) { diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java index 94aad0ac7e..b7ecc7d42d 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Town.java @@ -292,7 +292,8 @@ public void removeNation() { oldNation.removeTown(this); } catch (EmptyNationException e) { TownyUniverse.getInstance().getDataSource().removeNation(oldNation); - TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", e.getNation().getName())); + if (!nation.exists()) // The PreDeleteNationEvent was not cancelled. + TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", e.getNation().getName())); } try { diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java index 1d626b51ba..1da02763f9 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java @@ -780,9 +780,12 @@ private void processNationUpkeep(Nation nation) { totalNationUpkeep = totalNationUpkeep + upkeep; TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_your_nation_payed_upkeep", prettyMoney(upkeep))); } else { - TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_your_nation_couldnt_pay_upkeep", prettyMoney(upkeep))); universe.getDataSource().removeNation(nation); - removedNations.add(nation.getName()); + if (!nation.exists()) { // The PreDeleteNationEvent was not cancelled. + TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_your_nation_couldnt_pay_upkeep", prettyMoney(upkeep))); + removedNations.add(nation.getName()); + return; + } } } else if (upkeep < 0) { nation.getAccount().withdraw(upkeep, "Negative Nation Upkeep"); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java index 923bf3ebec..8c328b0be1 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java @@ -53,9 +53,12 @@ public static void checkNationResidentsRequirementsOfTown(Town town) { return; // No new capital found, delete the nation and potentially refund the capital town. + TownyUniverse.getInstance().getDataSource().removeNation(nation); + if (nation.exists()) // The PreDeleteNationEvent was cancelled. + return; + TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_nation_disbanded_town_not_enough_residents", town.getName())); TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation)); - TownyUniverse.getInstance().getDataSource().removeNation(nation); if (TownyEconomyHandler.isActive() && TownySettings.isRefundNationDisbandLowResidents()) { town.getAccount().deposit(TownySettings.getNewNationPrice(), "nation refund"); diff --git a/Towny/src/main/resources/lang/en-US.yml b/Towny/src/main/resources/lang/en-US.yml index cfb091f656..4ca62120a1 100644 --- a/Towny/src/main/resources/lang/en-US.yml +++ b/Towny/src/main/resources/lang/en-US.yml @@ -2524,4 +2524,6 @@ msg_new_resident_spawn_to_town_prompt: "Did you want to spawn to the town you ju msg_recieved_refund_for_deleted_object: "You have received the bank balance of your deleted town or nation, a sum of %s." -msg_err_you_already_own_this_plot: "You already own this plot." \ No newline at end of file +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 From 203c6b1abc6555fe45959ce7250fec8185d9bd0a Mon Sep 17 00:00:00 2001 From: LlmDl Date: Fri, 26 Apr 2024 10:13:30 -0500 Subject: [PATCH 2/4] Remove event handler used in testing. --- .../bukkit/towny/listeners/TownyCustomListener.java | 7 ------- 1 file changed, 7 deletions(-) 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 11f56cf3cf..bc63b19d8a 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 @@ -14,7 +14,6 @@ import com.palmergames.bukkit.towny.event.ChunkNotificationEvent; import com.palmergames.bukkit.towny.event.NewTownEvent; import com.palmergames.bukkit.towny.event.PlayerChangePlotEvent; -import com.palmergames.bukkit.towny.event.PreDeleteNationEvent; import com.palmergames.bukkit.towny.event.SpawnEvent; import com.palmergames.bukkit.towny.event.TownAddResidentEvent; import com.palmergames.bukkit.towny.event.TownBlockPermissionChangeEvent; @@ -60,12 +59,6 @@ public TownyCustomListener(Towny instance) { plugin = instance; } - @EventHandler - public void onNationDelete(PreDeleteNationEvent event) { - System.out.println("Cancelling nation delete."); - event.setCancelled(true); - } - @EventHandler(priority = EventPriority.NORMAL) public void onPlayerChangePlotEvent(PlayerChangePlotEvent event) { From 509ffff584823aeb4664462871b7ab3fa1794436 Mon Sep 17 00:00:00 2001 From: LlmDl Date: Fri, 26 Apr 2024 10:18:20 -0500 Subject: [PATCH 3/4] Fix sending message to the non-existant nation. --- .../com/palmergames/bukkit/towny/tasks/DailyTimerTask.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java index 1da02763f9..51a7d469de 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/tasks/DailyTimerTask.java @@ -1,6 +1,7 @@ package com.palmergames.bukkit.towny.tasks; import com.palmergames.bukkit.towny.Towny; +import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.TownyEconomyHandler; import com.palmergames.bukkit.towny.TownyMessaging; import com.palmergames.bukkit.towny.TownySettings; @@ -24,6 +25,8 @@ import java.util.List; import java.util.ListIterator; +import org.bukkit.entity.Player; + public class DailyTimerTask extends TownyTimerTask { private double totalTownUpkeep = 0.0; @@ -780,9 +783,11 @@ private void processNationUpkeep(Nation nation) { totalNationUpkeep = totalNationUpkeep + upkeep; TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_your_nation_payed_upkeep", prettyMoney(upkeep))); } else { + List onlinePlayers = TownyAPI.getInstance().getOnlinePlayersInNation(nation); universe.getDataSource().removeNation(nation); if (!nation.exists()) { // The PreDeleteNationEvent was not cancelled. - TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_your_nation_couldnt_pay_upkeep", prettyMoney(upkeep))); + String formattedUpkeep = prettyMoney(upkeep); + onlinePlayers.forEach(p -> TownyMessaging.sendMsg(p, Translatable.of("msg_your_nation_couldnt_pay_upkeep", formattedUpkeep))); removedNations.add(nation.getName()); return; } From df89aaa58fa049e39e3c4aba937edc3ea5928ccc Mon Sep 17 00:00:00 2001 From: LlmDl Date: Fri, 26 Apr 2024 10:25:22 -0500 Subject: [PATCH 4/4] Do the same for TownUtil, removing call to sendPrefixedNationMessage. --- .../java/com/palmergames/bukkit/towny/utils/TownUtil.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java index 8c328b0be1..504bcc42e7 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/TownUtil.java @@ -3,6 +3,9 @@ import java.util.List; import java.util.stream.Collectors; +import org.bukkit.entity.Player; + +import com.palmergames.bukkit.towny.TownyAPI; import com.palmergames.bukkit.towny.TownyEconomyHandler; import com.palmergames.bukkit.towny.TownyMessaging; import com.palmergames.bukkit.towny.TownySettings; @@ -52,13 +55,14 @@ public static void checkNationResidentsRequirementsOfTown(Town town) { if (findNewCapital(town, nation)) return; + List onlinePlayers = TownyAPI.getInstance().getOnlinePlayersInNation(nation); // No new capital found, delete the nation and potentially refund the capital town. TownyUniverse.getInstance().getDataSource().removeNation(nation); if (nation.exists()) // The PreDeleteNationEvent was cancelled. return; - TownyMessaging.sendPrefixedNationMessage(nation, Translatable.of("msg_nation_disbanded_town_not_enough_residents", town.getName())); - TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation)); + onlinePlayers.forEach(p -> TownyMessaging.sendMsg(p, Translatable.of("msg_nation_disbanded_town_not_enough_residents", town.getName()))); + TownyMessaging.sendGlobalMessage(Translatable.of("msg_del_nation", nation.getName())); if (TownyEconomyHandler.isActive() && TownySettings.isRefundNationDisbandLowResidents()) { town.getAccount().deposit(TownySettings.getNewNationPrice(), "nation refund");