Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nation deleted message showing when a predeletenationevent is cancelled. #7383

Merged
merged 4 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -780,9 +783,14 @@ 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)));
List<Player> onlinePlayers = TownyAPI.getInstance().getOnlinePlayersInNation(nation);
universe.getDataSource().removeNation(nation);
removedNations.add(nation.getName());
if (!nation.exists()) { // The PreDeleteNationEvent was not cancelled.
String formattedUpkeep = prettyMoney(upkeep);
onlinePlayers.forEach(p -> TownyMessaging.sendMsg(p, Translatable.of("msg_your_nation_couldnt_pay_upkeep", formattedUpkeep)));
removedNations.add(nation.getName());
return;
}
}
} else if (upkeep < 0) {
nation.getAccount().withdraw(upkeep, "Negative Nation Upkeep");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,10 +55,14 @@ public static void checkNationResidentsRequirementsOfTown(Town town) {
if (findNewCapital(town, nation))
return;

List<Player> onlinePlayers = TownyAPI.getInstance().getOnlinePlayersInNation(nation);
// No new capital found, delete the nation and potentially refund the capital town.
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 (nation.exists()) // The PreDeleteNationEvent was cancelled.
return;

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");
Expand Down
4 changes: 3 additions & 1 deletion Towny/src/main/resources/lang/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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."
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."
Loading