From f9c9436f2064cdf5535de1553af7d0b98f874326 Mon Sep 17 00:00:00 2001 From: Golfing7 Date: Tue, 2 Dec 2025 10:14:10 -0500 Subject: [PATCH 1/3] Add PreTransactionEvent --- .../java/com/earth2me/essentials/User.java | 8 ++ .../api/v2/events/PreTransactionEvent.java | 73 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index e0b7cd40276..9fd9760fda8 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -22,6 +22,7 @@ import net.ess3.api.events.MuteStatusChangeEvent; import net.ess3.api.events.UserBalanceUpdateEvent; import net.ess3.provider.PlayerLocaleProvider; +import net.essentialsx.api.v2.events.PreTransactionEvent; import net.essentialsx.api.v2.events.TransactionEvent; import net.essentialsx.api.v2.services.mail.MailSender; import net.kyori.adventure.text.Component; @@ -270,6 +271,13 @@ public void payUser(final User reciever, final BigDecimal value, final UserBalan } if (canAfford(value)) { + // Call an event for pre-transaction + final PreTransactionEvent preTransactionEvent = new PreTransactionEvent(this.getSource(), reciever, value); + ess.getServer().getPluginManager().callEvent(preTransactionEvent); + if (preTransactionEvent.isCancelled()) { + return; + } + setMoney(getMoney().subtract(value), cause); reciever.setMoney(reciever.getMoney().add(value), cause); sendTl("moneySentTo", AdventureUtil.parsed(NumberUtil.displayCurrency(value, ess)), reciever.getDisplayName()); diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java new file mode 100644 index 00000000000..a959d8d0c21 --- /dev/null +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java @@ -0,0 +1,73 @@ +package net.essentialsx.api.v2.events; + +import com.earth2me.essentials.CommandSource; +import net.ess3.api.IUser; +import org.bukkit.Bukkit; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import java.math.BigDecimal; + +/** + * Fired when a transaction (e.g. /pay) is about to be handled. + */ +public class PreTransactionEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private final CommandSource requester; + private final IUser target; + private final BigDecimal amount; + private boolean cancelled; + + public PreTransactionEvent(final CommandSource requester, final IUser target, final BigDecimal amount) { + super(!Bukkit.isPrimaryThread()); + this.requester = requester; + this.target = target; + this.amount = amount; + } + + /** + * @return the user who initiated the transaction + */ + public CommandSource getRequester() { + return requester; + } + + /** + * @return the user who received the money + */ + public IUser getTarget() { + return target; + } + + /** + * @return the amount of money transacted + */ + public BigDecimal getAmount() { + return amount; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + /** + * If this event should be cancelled. If cancelled, no messages will be displayed to the users involved. + * @param cancelled whether this event should be cancelled + */ + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} From 12bb25eb0a02b064970f06f5fa57ce515d8fb2d8 Mon Sep 17 00:00:00 2001 From: Golfing8 Date: Mon, 15 Dec 2025 10:07:36 -0500 Subject: [PATCH 2/3] Adjust event to extend TransactionEvent --- .../api/v2/events/PreTransactionEvent.java | 35 ++++++------------- .../api/v2/events/TransactionEvent.java | 10 ++++-- 2 files changed, 17 insertions(+), 28 deletions(-) diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java index a959d8d0c21..f964b5b416c 100644 --- a/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java @@ -1,10 +1,10 @@ package net.essentialsx.api.v2.events; import com.earth2me.essentials.CommandSource; +import com.google.common.base.Preconditions; import net.ess3.api.IUser; import org.bukkit.Bukkit; import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; import org.bukkit.event.HandlerList; import java.math.BigDecimal; @@ -12,40 +12,25 @@ /** * Fired when a transaction (e.g. /pay) is about to be handled. */ -public class PreTransactionEvent extends Event implements Cancellable { +public class PreTransactionEvent extends TransactionEvent implements Cancellable { private static final HandlerList handlers = new HandlerList(); - private final CommandSource requester; - private final IUser target; - private final BigDecimal amount; private boolean cancelled; public PreTransactionEvent(final CommandSource requester, final IUser target, final BigDecimal amount) { - super(!Bukkit.isPrimaryThread()); - this.requester = requester; - this.target = target; - this.amount = amount; + super(!Bukkit.isPrimaryThread(), requester, target, amount); } /** - * @return the user who initiated the transaction + * Sets the new amount of this transaction event. + * Note that the new amount will still be subtracted from the requester's bank. + * @param decimal the new amount */ - public CommandSource getRequester() { - return requester; - } + public void setAmount(final BigDecimal decimal) { + Preconditions.checkNotNull(decimal, "decimal cannot be null"); + Preconditions.checkArgument(decimal.compareTo(BigDecimal.ZERO) >= 0, "decimal cannot be negative"); - /** - * @return the user who received the money - */ - public IUser getTarget() { - return target; - } - - /** - * @return the amount of money transacted - */ - public BigDecimal getAmount() { - return amount; + this.amount = decimal; } @Override diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java index 9e09c4f58bb..1a26946d6bd 100644 --- a/Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java @@ -16,15 +16,19 @@ public class TransactionEvent extends Event { private final CommandSource requester; private final IUser target; - private final BigDecimal amount; + protected BigDecimal amount; - public TransactionEvent(CommandSource requester, IUser target, BigDecimal amount) { - super(!Bukkit.isPrimaryThread()); + protected TransactionEvent(boolean async, CommandSource requester, IUser target, BigDecimal amount) { + super(async); this.requester = requester; this.target = target; this.amount = amount; } + public TransactionEvent(CommandSource requester, IUser target, BigDecimal amount) { + this(!Bukkit.isPrimaryThread(), requester, target, amount); + } + /** * @return the user who initiated the transaction */ From 420189e90348884dd89d721b5e6169bf7b5a539b Mon Sep 17 00:00:00 2001 From: Golfing7 Date: Mon, 15 Dec 2025 19:06:53 -0500 Subject: [PATCH 3/3] Update transaction event --- .../src/main/java/com/earth2me/essentials/User.java | 11 ++++++----- .../api/v2/events/PreTransactionEvent.java | 5 +++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index 9fd9760fda8..16920a80bd2 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -277,12 +277,13 @@ public void payUser(final User reciever, final BigDecimal value, final UserBalan if (preTransactionEvent.isCancelled()) { return; } + final BigDecimal transactionAmount = preTransactionEvent.getAmount(); - setMoney(getMoney().subtract(value), cause); - reciever.setMoney(reciever.getMoney().add(value), cause); - sendTl("moneySentTo", AdventureUtil.parsed(NumberUtil.displayCurrency(value, ess)), reciever.getDisplayName()); - reciever.sendTl("moneyRecievedFrom", AdventureUtil.parsed(NumberUtil.displayCurrency(value, ess)), getDisplayName()); - final TransactionEvent transactionEvent = new TransactionEvent(this.getSource(), reciever, value); + setMoney(getMoney().subtract(transactionAmount), cause); + reciever.setMoney(reciever.getMoney().add(transactionAmount), cause); + sendTl("moneySentTo", AdventureUtil.parsed(NumberUtil.displayCurrency(transactionAmount, ess)), reciever.getDisplayName()); + reciever.sendTl("moneyRecievedFrom", AdventureUtil.parsed(NumberUtil.displayCurrency(transactionAmount, ess)), getDisplayName()); + final TransactionEvent transactionEvent = new TransactionEvent(this.getSource(), reciever, transactionAmount); ess.getServer().getPluginManager().callEvent(transactionEvent); } else { throw new ChargeException("notEnoughMoney", AdventureUtil.parsed(NumberUtil.displayCurrency(value, ess))); diff --git a/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java index f964b5b416c..e772a7239e8 100644 --- a/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java @@ -22,8 +22,9 @@ public PreTransactionEvent(final CommandSource requester, final IUser target, fi } /** - * Sets the new amount of this transaction event. - * Note that the new amount will still be subtracted from the requester's bank. + * Sets the amount to be subtracted from the requester's balance. + *

+ * Note: Changing this amount will not verify the requester actually has enough balance to complete the transaction. * @param decimal the new amount */ public void setAmount(final BigDecimal decimal) {