Skip to content

Commit

Permalink
Handle renaming and adds debug spam to remove later.
Browse files Browse the repository at this point in the history
  • Loading branch information
LlmDl committed Oct 25, 2024
1 parent 0b7aebb commit 18b166b
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Towny/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultUnlockedAPI</artifactId>
<version>2.6</version>
<version>2.7</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.palmergames.bukkit.config.ConfigNodes;
import com.palmergames.bukkit.towny.event.economy.TownyPreTransactionEvent;
import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.object.Government;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
Expand Down Expand Up @@ -266,11 +267,12 @@ public static boolean subtract(String accountName, double amount, World world) {
* @return true if successful
*/
public static boolean subtract(Account account, double amount) {

System.out.println("TownyEconomyHandler#subtract 1");
if (!runPreChecks(Transaction.subtract(amount).paidBy(account).build())) {
System.out.println("TownyEconomyHandler#subtract 2");
return false;
}

System.out.println("TownyEconomyHandler#subtract 3");
return economy.subtract(account, amount);
}

Expand Down Expand Up @@ -300,6 +302,7 @@ public static boolean add(Account account, double amount) {
return false;
}

System.out.println("TownyEconomyHandler#add " + account.getName());
return economy.add(account, amount);
}

Expand Down Expand Up @@ -402,4 +405,14 @@ public static UUID modifyNPCUUID(final UUID uuid) {

return JavaUtil.changeUUIDVersion(uuid, version);
}

public static boolean canRenameAccounts() {
return getType().equals(EcoType.VAULTUNLOCKED);
}

public static void rename(Government gov, String newName) {
if (getType().equals(EcoType.VAULTUNLOCKED)) {
economy.renameAccount(gov.getAccount(), newName);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -626,13 +626,18 @@ public void renameTown(Town town, String newName) throws AlreadyRegisteredExcept
// Save the towns bank balance to set in the new account.
// Clear accounts
if (TownyEconomyHandler.isActive())
try {
townBalance = town.getAccount().getHoldingBalance();
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.");
if (TownyEconomyHandler.canRenameAccounts()) {
TownyEconomyHandler.rename(town, newName);
} else {
try {
townBalance = town.getAccount().getHoldingBalance();
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.");
}
}


UUID oldUUID = town.getUUID();
long oldregistration = town.getRegistered();

Expand Down Expand Up @@ -672,7 +677,8 @@ public void renameTown(Town town, String newName) throws AlreadyRegisteredExcept
town.setRegistered(oldregistration);
if (TownyEconomyHandler.isActive()) {
town.getAccount().setName(TownySettings.getTownAccountPrefix() + town.getName());
town.getAccount().setBalance(townBalance, "Rename Town - Transfer to new account");
if (!TownyEconomyHandler.canRenameAccounts())
town.getAccount().setBalance(townBalance, "Rename Town - Transfer to new account");
}

for (Resident resident : toSave) {
Expand Down Expand Up @@ -729,11 +735,15 @@ public void renameNation(Nation nation, String newName) throws AlreadyRegistered
// Save the nations bank balance to set in the new account.
// Clear accounts
if (TownyEconomyHandler.isActive())
try {
nationBalance = nation.getAccount().getHoldingBalance();
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.");
if (TownyEconomyHandler.canRenameAccounts()) {
TownyEconomyHandler.rename(nation, newName);
} else {
try {
nationBalance = nation.getAccount().getHoldingBalance();
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.");
}
}

//Tidy up old files
Expand All @@ -750,7 +760,8 @@ public void renameNation(Nation nation, String newName) throws AlreadyRegistered

if (TownyEconomyHandler.isActive()) {
nation.getAccount().setName(TownySettings.getNationAccountPrefix() + nation.getName());
nation.getAccount().setBalance(nationBalance, "Rename Nation - Transfer to new account");
if (!TownyEconomyHandler.canRenameAccounts())
nation.getAccount().setBalance(nationBalance, "Rename Nation - Transfer to new account");
}

for (Town town : toSave) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ protected synchronized boolean addMoney(double amount) {

@Override
protected synchronized boolean subtractMoney(double amount) {
System.out.println("EconomyAccount#subtractMoney 1");
return TownyEconomyHandler.subtract(this, amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,12 @@ public final synchronized void depositToBank(Resident resident, int amount) thro
if (!TownyEconomyHandler.isActive()) {
throw new TownyException(Translation.of("msg_err_no_economy"));
}

System.out.println("Government#depositToBank 1");
if (!resident.getAccount().payTo(amount, this, "Deposit from " + resident.getName())) {
throw new TownyException(Translation.of("msg_insuf_funds"));
}
System.out.println("Government#depositToBank 1");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public synchronized boolean withdraw(double amount, String reason) {
return payToServer(amount, reason);

BukkitTools.fireEvent(Transaction.subtract(amount).paidBy(this).asTownyTransactionEvent());
System.out.println("Account#withdraw 1");
return true;
}

Expand Down Expand Up @@ -185,11 +186,12 @@ protected synchronized boolean payFromServer(double amount, String reason) {
* @return boolean indicating success.
*/
public synchronized boolean payTo(double amount, Account collector, String reason) {

System.out.println("Account#payTo 1");
if (amount > getHoldingBalance()) {
System.out.println("Account#payTo 2");
return false;
}

System.out.println("Account#payTo 3");
boolean success = withdraw(amount, reason) && collector.deposit(amount, reason);
if (success)
BukkitTools.fireEvent(Transaction.add(amount).paidBy(this).paidTo(collector).asTownyTransactionEvent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ protected synchronized boolean addMoney(double amount) {
if (isBankrupt())
return removeDebt(amount);

System.out.println("BankAccount#addMoney 1");
// Otherwise continue like normal.
return TownyEconomyHandler.add(this, amount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public interface EconomyAdapter {
*/
void deleteAccount(Account account);

/**
* Renames an account.
*
* @param account the Account to rename.
* @param newName the name to give the Account.
*/
void renameAccount(Account account, String newName);

/**
* Sets the balance of the account.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ public String getFormattedBalance(double balance) {
BigDecimal bd = BigDecimal.valueOf(balance);
return economy.format(bd);
}


@Override
public void renameAccount(Account account, String newName) {
// Unused in Reserve.
}

public static class Legacy extends ReserveEconomyAdapter {
public Legacy(EconomyAPI economy) {
super(economy);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ public String getFormattedBalance(double balance) {
return economy.format(balance);
}

@Override
public void renameAccount(Account account, String newName) {
// Unused in Vault.
}

@SuppressWarnings("deprecation")
public static class Legacy extends VaultEconomyAdapter {
public Legacy(Economy economy) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.math.BigDecimal;

import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.utils.TownyComponents;

import net.milkbowl.vault2.economy.Economy;
import net.milkbowl.vault2.economy.EconomyResponse;

Expand Down Expand Up @@ -51,12 +49,12 @@ public void newAccount(Account account) {

@Override
public void deleteAccount(Account account) {
// Attempt to zero the account as Vault provides no delete method.
if (!hasAccount(account)) {
return;
}
economy.deleteAccount(TOWNY, account.getUUID());
}

subtract(account, getBalance(account));
@Override
public void renameAccount(Account account, String newName) {
economy.renameAccount(TOWNY, account.getUUID(), newName);
}

@Override
Expand All @@ -77,6 +75,6 @@ public boolean setBalance(Account account, double amount) {
@Override
public String getFormattedBalance(double balance) {
BigDecimal bd = BigDecimal.valueOf(balance);
return economy.format(bd);
return economy.format(TOWNY, bd);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ public static void townWithdraw(Player player, Resident resident, Town town, int
public static void townDeposit(Player player, Resident resident, Town town, Nation nation, int amount) {

try {
System.out.println("MoneyUtil#townDeposit 1");
commonTests(amount, resident, town, player.getLocation(), false, false);

System.out.println("MoneyUtil#townDeposit 2");
Transaction transaction = Transaction.deposit(amount).paidBy(resident).paidTo(town).build();

BukkitTools.ifCancelledThenThrow(new TownPreTransactionEvent(town, transaction));
Expand Down Expand Up @@ -159,9 +160,10 @@ private static void commonTests(int amount, Resident resident, Town town, Locati
if (amount < 0)
throw new TownyException(Translatable.of("msg_err_negative_money"));

System.out.println("MoneyUtil#commonTests 1");
if (!withdraw && !resident.getAccount().canPayFromHoldings(amount))
throw new TownyException(Translatable.of("msg_insuf_funds"));

System.out.println("MoneyUtil#commonTests 2");
if (!nation && town.isRuined())
throw new TownyException(Translatable.of("msg_err_cannot_use_command_because_town_ruined"));

Expand Down

0 comments on commit 18b166b

Please sign in to comment.