diff --git a/Towny/pom.xml b/Towny/pom.xml index 81405fa0ef..26b46402a4 100644 --- a/Towny/pom.xml +++ b/Towny/pom.xml @@ -120,7 +120,7 @@ net.milkbowl.vault VaultUnlockedAPI - 2.6 + 2.7 provided diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/TownyEconomyHandler.java b/Towny/src/main/java/com/palmergames/bukkit/towny/TownyEconomyHandler.java index 30a634a5e9..f77b952a8e 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/TownyEconomyHandler.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/TownyEconomyHandler.java @@ -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; @@ -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); } @@ -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); } @@ -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); + } + } } \ No newline at end of file diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java index e79b417531..d3f56b5c46 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/db/TownyDatabaseHandler.java @@ -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(); @@ -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) { @@ -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 @@ -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) { diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/EconomyAccount.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/EconomyAccount.java index 9cef22f4d7..615a745f49 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/EconomyAccount.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/EconomyAccount.java @@ -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); } } diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Government.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Government.java index 568befac8e..25a68b09c7 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/Government.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/Government.java @@ -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 diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/Account.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/Account.java index e895217423..91c9d3ea2b 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/Account.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/Account.java @@ -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; } @@ -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()); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/BankAccount.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/BankAccount.java index 7dfd78addb..5f8c53fd56 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/BankAccount.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/BankAccount.java @@ -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); } diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/EconomyAdapter.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/EconomyAdapter.java index 97903e6b5f..a13bebeac6 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/EconomyAdapter.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/EconomyAdapter.java @@ -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. * diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/ReserveEconomyAdapter.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/ReserveEconomyAdapter.java index ec9ac5a743..c36eb239a6 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/ReserveEconomyAdapter.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/ReserveEconomyAdapter.java @@ -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); diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultEconomyAdapter.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultEconomyAdapter.java index bf47855365..ac4ac1cc28 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultEconomyAdapter.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultEconomyAdapter.java @@ -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) { diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultUnlockedEconomyAdapter.java b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultUnlockedEconomyAdapter.java index 9cb07e8866..d9e62afbae 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultUnlockedEconomyAdapter.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/object/economy/adapter/VaultUnlockedEconomyAdapter.java @@ -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; @@ -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 @@ -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); } } diff --git a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MoneyUtil.java b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MoneyUtil.java index 24b42ec9cd..35611cb944 100644 --- a/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MoneyUtil.java +++ b/Towny/src/main/java/com/palmergames/bukkit/towny/utils/MoneyUtil.java @@ -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)); @@ -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"));