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 deleted town/nation bank refund duplication #7375

Merged
merged 1 commit into from
Apr 22, 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 @@ -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
Loading