Skip to content

Commit

Permalink
Fix deleted town/nation bank refund duplication (#7375)
Browse files Browse the repository at this point in the history
  • Loading branch information
Warriorrrr authored Apr 22, 2024
1 parent 09de20a commit e91da40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@ public static double getDeathPriceNation() {
return getDouble(ConfigNodes.ECO_PRICE_DEATH_NATION);
}

public static boolean isDeletedObjectBalancePaidToMayor() {
public static boolean isDeletedObjectBalancePaidToOwner() {
return getBoolean(ConfigNodes.ECO_BANK_IS_DELETED_OBJECT_BALANCE_PAID_TO_OWNER);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,9 @@ public void renameTown(Town town, String newName) throws AlreadyRegisteredExcept
if (TownyEconomyHandler.isActive())
try {
townBalance = town.getAccount().getHoldingBalance();
town.getAccount().removeAccount();
town.getAccount().withdraw(townBalance, "Rename Town - Transfer from old account");
} catch (Exception ignored) {
TownyMessaging.sendErrorMsg("The bank balance for the town " + oldName + ", could not be received from the economy plugin and will not be able to be converted.");
TownyMessaging.sendErrorMsg("The bank balance for the town " + oldName + " could not be received from the economy plugin and will not be able to be converted.");
}

UUID oldUUID = town.getUUID();
Expand Down Expand Up @@ -716,7 +716,7 @@ public void renameNation(Nation nation, String newName) throws AlreadyRegistered
if (TownyEconomyHandler.isActive())
try {
nationBalance = nation.getAccount().getHoldingBalance();
nation.getAccount().removeAccount();
nation.getAccount().setBalance(0, "Rename Nation - Transfer from old account");
} catch (Exception ignored) {
TownyMessaging.sendErrorMsg("The bank balance for the nation " + nation.getName() + ", could not be received from the economy plugin and will not be able to be converted.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,39 +135,24 @@ private Town getTown() {
*/
@Override
public void removeAccount() {
double balance = TownyEconomyHandler.getBalance(getName(), getBukkitWorld());
if (balance > 0) {
if (TownySettings.isDeletedObjectBalancePaidToMayor() && ableToPayBalanceToOwner()) {
payBalanceToOwner(balance);
} else if (TownySettings.isEcoClosedEconomyEnabled()) {
TownyEconomyHandler.addToServer(balance, getBukkitWorld());
if (TownySettings.isDeletedObjectBalancePaidToOwner()) {
final Resident owner = getGovernmentOwner();

if (owner != null && !owner.isNPC()) {
double balance = getHoldingBalance();
if (balance > 0) {
TownyMessaging.sendMsg(owner, Translatable.of("msg_recieved_refund_for_deleted_object", TownyEconomyHandler.getFormattedBalance(balance)));
payTo(balance, owner, "Deleted " + (isTownAccount() ? "Town" : "Nation") + " bank balance refund.");
}
}
}
TownyEconomyHandler.removeAccount(getName());
}

/**
* @return true when the Government still has a mayor or leader, and that leader is not an NPC.
*/
private boolean ableToPayBalanceToOwner() {
if (isTownAccount())
return getTown().hasMayor() && !getTown().getMayor().isNPC();
else
return ((Nation) government).hasKing() && !((Nation) government).getKing().isNPC();
}

private void payBalanceToOwner(double balance) {
Resident owner = getGovernmentOwner();
if (owner.isOnline())
TownyMessaging.sendMsg(owner, Translatable.of("msg_recieved_refund_for_deleted_object", TownyEconomyHandler.getFormattedBalance(balance)));
payTo(balance, owner, "Deleted Town or Nation bank balance refund.");
super.removeAccount();
}

@Nullable
private Resident getGovernmentOwner() {
if (isTownAccount())
return getTown().getMayor();
else
return ((Nation) government).getKing();
return government instanceof Town town ? town.getMayor() : government instanceof Nation nation ? nation.getKing() : null;
}

/*
Expand Down

0 comments on commit e91da40

Please sign in to comment.