Skip to content

Commit

Permalink
Fix the TownyTransactionEvent assuming a player is involved.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
LlmDl committed Jun 3, 2024
1 parent 51b8afe commit bfc77f7
Show file tree
Hide file tree
Showing 27 changed files with 306 additions and 115 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)) {
if (BukkitTools.isEventCancelled(preEvent) && transaction.getPlayer() != null) {
TownyMessaging.sendErrorMsg(transaction.getPlayer(), 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 @@ -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 @@ -818,7 +818,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 @@ -2,6 +2,11 @@

import org.bukkit.entity.Player;


/**
* @deprecated since 0.100.3.1 use {@link com.palmergames.bukkit.towny.object.economy.transaction.Transaction} instead.
*/
@Deprecated
public class Transaction {
private final TransactionType type;
private final Player player;
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.3.1 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 bfc77f7

Please sign in to comment.