Skip to content

Commit

Permalink
Alter Account & Transaction and fix TownyTransactionEvent. (#7431)
Browse files Browse the repository at this point in the history
* Fix the TownyTransactionEvent assuming a player is involved.

Alters Transactions to no longer use a Player and instead try to use a
TownyObject.
Rethink Transaction to adopt a Account instead of a TownyObject and
Name.
Fix javadoc
Fix Account initializing TownyObject null.
Add helper method to Transaction.
Clean up EconomyAccount, move the TownyServerAccount instance to
Account.
Alter Transaction again in order to add both Receiving and Sending
accounts.
Remove getName/getTownyObject from Transaction, change TownyObject in
Account over to EconomyHandler. Return government field to BankAccount.
Throw the TownyTransactionEvent earlier, so that we can better support
the new sender and receiver fields in the Transaction.
Fix flipped sender/receiver.
Notify observers of payTo/payFromServer only when the transaction is a
success.
A very hacky way to get the TownyServerAccount to implement
EconomyHandler.
Change transaction package, adopt new builder pattern, deprecate
existing Transaction and TransactionType classes.
Reduce diff and a couple better javadocs

* Rebased and accounted for recent changes in TownyAdminCommand, also add
annotations and helper methods to Transaction.

* Update deprecated in version number.

* Change Transaction#getPlayer to address review.
  • Loading branch information
LlmDl authored Jul 12, 2024
1 parent 73ef2c5 commit 46ee594
Show file tree
Hide file tree
Showing 28 changed files with 331 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

import com.palmergames.bukkit.config.ConfigNodes;
import com.palmergames.bukkit.towny.event.economy.TownyPreTransactionEvent;
import com.palmergames.bukkit.towny.event.economy.TownyTransactionEvent;
import com.palmergames.bukkit.towny.object.economy.adapter.ReserveEconomyAdapter;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Resident;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.TransactionType;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.object.economy.TownyServerAccount;
import com.palmergames.bukkit.towny.object.economy.TownyServerAccountEconomyHandler;
import com.palmergames.bukkit.towny.object.economy.adapter.EconomyAdapter;
import com.palmergames.bukkit.towny.object.economy.adapter.VaultEconomyAdapter;
import com.palmergames.bukkit.util.BukkitTools;
Expand All @@ -18,9 +18,7 @@
import net.milkbowl.vault.economy.Economy;
import net.tnemc.core.Reserve;

import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -98,6 +96,12 @@ public static void initialize(Towny plugin) {
TownyEconomyHandler.plugin = plugin;
}

public static TownyServerAccount initializeTownyServerAccount() {
TownyServerAccountEconomyHandler economyHandler = new TownyServerAccount();
TownyServerAccount account = new TownyServerAccount(economyHandler);
return account;
}

/**
* @return the economy type we have detected.
*/
Expand Down Expand Up @@ -211,36 +215,30 @@ public static boolean hasEnough(String accountName, double amount, World world)

private static boolean runPreChecks(Transaction transaction, String accountName) {
TownyPreTransactionEvent preEvent = new TownyPreTransactionEvent(transaction);
if (BukkitTools.isEventCancelled(preEvent)) {
TownyMessaging.sendErrorMsg(transaction.getPlayer(), preEvent.getCancelMessage());
if (BukkitTools.isEventCancelled(preEvent) && transaction.getSendingPlayer() != null) {
TownyMessaging.sendErrorMsg(transaction.getSendingPlayer(), preEvent.getCancelMessage());
return false;
}

checkNewAccount(accountName);
return true;
}


/**
* Attempts to remove an amount from an account
*
* @param accountName name of the account to modify
* @param account the Account losing money.
* @param amount amount of currency to remove from the account
* @param world name of the world in which to check in (TNE Reserve)
* @return true if successful
*/
public static boolean subtract(String accountName, double amount, World world) {
public static boolean subtract(Account account, double amount, World world) {

Player player = Bukkit.getServer().getPlayerExact(accountName);
Transaction transaction = new Transaction(TransactionType.SUBTRACT, player, amount);
TownyTransactionEvent event = new TownyTransactionEvent(transaction);

if (!runPreChecks(transaction, accountName)) {
if (!runPreChecks(Transaction.subtract(amount).paidBy(account).build(), account.getName())) {
return false;
}

if (economy.subtract(accountName, amount, world)) {
BukkitTools.fireEvent(event);
if (economy.subtract(account.getName(), amount, world)) {
return true;
}

Expand All @@ -250,23 +248,18 @@ public static boolean subtract(String accountName, double amount, World world) {
/**
* Add funds to an account.
*
* @param accountName account to add funds to
* @param account the Account receiving money.
* @param amount amount of currency to add
* @param world name of world (for TNE Reserve)
* @return true if successful
*/
public static boolean add(String accountName, double amount, World world) {
public static boolean add(Account account, double amount, World world) {

Player player = Bukkit.getServer().getPlayerExact(accountName);
Transaction transaction = new Transaction(TransactionType.ADD, player, amount);
TownyTransactionEvent event = new TownyTransactionEvent(transaction);

if (!runPreChecks(transaction, accountName)) {
if (!runPreChecks(Transaction.add(amount).paidTo(account).build(), account.getName())) {
return false;
}

if (economy.add(accountName, amount, world)) {
BukkitTools.fireEvent(event);
if (economy.add(account.getName(), amount, world)) {
return true;
}

Expand Down Expand Up @@ -297,27 +290,6 @@ public static String getFormattedBalance(double balance) {

}

/**
* Adds money to the server account (used for towny closed economy.)
*
* @param amount The amount to deposit.
* @param world The world of the deposit.
* @return A boolean indicating success.
*/
public static boolean addToServer(double amount, World world) {
return add(getServerAccount(), amount, world);
}

/**
* Removes money to the server account (used for towny closed economy.)
*
* @param amount The amount to withdraw.
* @param world The world of the withdraw.
* @return A boolean indicating success.
*/
public static boolean subtractFromServer(double amount, World world) {
return subtract(getServerAccount(), amount, world);
}

private static void checkNewAccount(String accountName) {
// Check if the account exists, if not create one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@
import com.palmergames.bukkit.towny.object.TownBlockType;
import com.palmergames.bukkit.towny.object.TownBlockTypeHandler;
import com.palmergames.bukkit.towny.object.TownyWorld;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.TransactionType;
import com.palmergames.bukkit.towny.object.Translatable;
import com.palmergames.bukkit.towny.object.WorldCoord;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.jail.UnJailReason;
import com.palmergames.bukkit.towny.object.metadata.CustomDataField;
import com.palmergames.bukkit.towny.permissions.PermissionNodes;
Expand Down Expand Up @@ -1293,7 +1292,7 @@ public void parseAdminTownCommand(CommandSender sender, String[] split) throws T

int deposit = MathUtil.getIntOrThrow(split[2]);
if (town.getAccount().deposit(deposit, "Admin Deposit")) {
BukkitTools.fireEvent(new TownTransactionEvent(town, new Transaction(TransactionType.DEPOSIT, sender, deposit)));
BukkitTools.fireEvent(new TownTransactionEvent(town, Transaction.deposit(deposit).paidTo(town).build()));
// Send notifications
Translatable depositMessage = Translatable.of("msg_xx_deposited_xx", getSenderFormatted(sender), deposit, Translatable.of("town_sing"));
TownyMessaging.sendMsg(sender, depositMessage);
Expand All @@ -1314,7 +1313,7 @@ public void parseAdminTownCommand(CommandSender sender, String[] split) throws T

int withdraw = MathUtil.getIntOrThrow(split[2]);
if (town.getAccount().withdraw(withdraw, "Admin Withdraw")) {
BukkitTools.fireEvent(new TownTransactionEvent(town, new Transaction(TransactionType.WITHDRAW, sender, withdraw)));
BukkitTools.fireEvent(new TownTransactionEvent(town, Transaction.withdraw(withdraw).paidBy(town).build()));
// Send notifications
Translatable withdrawMessage = Translatable.of("msg_xx_withdrew_xx", getSenderFormatted(sender), withdraw, Translatable.of("town_sing"));
TownyMessaging.sendMsg(sender, withdrawMessage);
Expand Down Expand Up @@ -1737,7 +1736,7 @@ public void parseAdminNationCommand(CommandSender sender, String[] split) throws

int deposit = MathUtil.getPositiveIntOrThrow(split[2]);

BukkitTools.fireEvent(new NationTransactionEvent(nation, new Transaction(TransactionType.DEPOSIT, sender, deposit)));
BukkitTools.fireEvent(new NationTransactionEvent(nation, Transaction.deposit(deposit).paidTo(nation).build()));

nation.getAccount().deposit(deposit, "Admin Deposit");
// Send notifications
Expand All @@ -1755,7 +1754,7 @@ public void parseAdminNationCommand(CommandSender sender, String[] split) throws

int withdraw = MathUtil.getPositiveIntOrThrow(split[2]);

BukkitTools.fireEvent(new NationTransactionEvent(nation, new Transaction(TransactionType.WITHDRAW, sender, withdraw)));
BukkitTools.fireEvent(new NationTransactionEvent(nation, Transaction.withdraw(withdraw).paidBy(nation).build()));

nation.getAccount().withdraw(withdraw, "Admin Withdraw");
// Send notifications
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.object.economy.BankAccount;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.BankAccount;

import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.palmergames.bukkit.towny.event.economy;

import com.palmergames.bukkit.towny.object.Nation;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.BankAccount;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.BankAccount;

import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.palmergames.bukkit.towny.event.economy;

import com.palmergames.bukkit.towny.object.Town;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.BankAccount;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.palmergames.bukkit.towny.event.economy;

import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.event.CancellableTownyEvent;
import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import com.palmergames.bukkit.towny.object.economy.Account;

import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -40,19 +39,11 @@ public Transaction getTransaction() {
* @return the future balance of the {@link Account} if this event is not
* cancelled.
*/
public int getNewBalance() {
switch (transaction.getType()) {
case ADD:
return (int) (TownyEconomyHandler.getBalance(transaction.getPlayer().getName(),
transaction.getPlayer().getWorld()) + transaction.getAmount());
case SUBTRACT:
return (int) (TownyEconomyHandler.getBalance(transaction.getPlayer().getName(),
transaction.getPlayer().getWorld()) - transaction.getAmount());
default:
break;
}

return 0;
public double getNewBalance() {
return switch (transaction.getType()) {
case ADD, DEPOSIT -> transaction.getReceivingAccount().getHoldingBalance() + transaction.getAmount();
case SUBTRACT, WITHDRAW -> transaction.getReceivingAccount().getHoldingBalance() - transaction.getAmount();
};
}

public static HandlerList getHandlerList() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.palmergames.bukkit.towny.event.economy;

import com.palmergames.bukkit.towny.object.Transaction;
import com.palmergames.bukkit.towny.object.economy.transaction.Transaction;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@

import com.palmergames.bukkit.towny.TownyEconomyHandler;
import com.palmergames.bukkit.towny.object.economy.Account;
import com.palmergames.bukkit.towny.object.economy.TownyServerAccount;
import com.palmergames.bukkit.towny.object.economy.BankAccount;
import org.bukkit.World;

/**
* Economy object which provides an interface with the Economy Handler.
* An Account object representing a Player's account. In contrast to the
* {@link BankAccount} that represents Towns' and Nations' Accounts.
*
* @author ElgarL
* @author Shade
* @author Suneet Tipirneni (Siris)
* @author LlmDl
*/
public class EconomyAccount extends Account {
public static final TownyServerAccount SERVER_ACCOUNT = new TownyServerAccount();
private World world;

protected EconomyAccount(String name, World world) {
super(name);
protected EconomyAccount(Resident resident, String name, World world) {
super(resident, name);
this.world = world;
}

@Override
protected synchronized boolean addMoney(double amount) {
return TownyEconomyHandler.add(getName(), amount, world);

return TownyEconomyHandler.add(this, amount, world);

}

@Override
protected synchronized boolean subtractMoney(double amount) {
return TownyEconomyHandler.subtract(getName(), amount, world);
}

protected EconomyAccount(String name) {
super(name);
return TownyEconomyHandler.subtract(this, amount, world);
}

public World getWorld() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ public final synchronized void depositToBank(Resident resident, int amount) thro
if (!TownyEconomyHandler.isActive()) {
throw new TownyException(Translation.of("msg_err_no_economy"));
}
if (!resident.getAccount().payTo(amount, getAccount(), "Deposit from " + resident.getName())) {
if (!resident.getAccount().payTo(amount, this, "Deposit from " + resident.getName())) {
throw new TownyException(Translation.of("msg_insuf_funds"));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public Account getAccount() {
world = BukkitTools.getWorlds().get(0);
}

account = new EconomyAccount(accountName, world);
account = new EconomyAccount(this, accountName, world);
}

return account;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import org.bukkit.entity.Player;
import org.jetbrains.annotations.Nullable;


/**
* @deprecated since 0.100.4.0 use {@link com.palmergames.bukkit.towny.object.economy.transaction.Transaction} instead.
*/
@Deprecated
public class Transaction {
private final TransactionType type;
private final CommandSender sender;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.palmergames.bukkit.towny.object;


/**
* @deprecated since 0.100.4.0 use {@link com.palmergames.bukkit.towny.object.economy.transaction.TransactionType} instead.
*/
@Deprecated
public enum TransactionType {
DEPOSIT("Deposit"), WITHDRAW("Withdraw"), ADD("Add"), SUBTRACT("Subtract");

Expand Down
Loading

0 comments on commit 46ee594

Please sign in to comment.