diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index e0b7cd40276..16920a80bd2 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,11 +271,19 @@ public void payUser(final User reciever, final BigDecimal value, final UserBalan } if (canAfford(value)) { - 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); + // Call an event for pre-transaction + final PreTransactionEvent preTransactionEvent = new PreTransactionEvent(this.getSource(), reciever, value); + ess.getServer().getPluginManager().callEvent(preTransactionEvent); + if (preTransactionEvent.isCancelled()) { + return; + } + final BigDecimal transactionAmount = preTransactionEvent.getAmount(); + + 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 new file mode 100644 index 00000000000..e772a7239e8 --- /dev/null +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/PreTransactionEvent.java @@ -0,0 +1,59 @@ +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.HandlerList; + +import java.math.BigDecimal; + +/** + * Fired when a transaction (e.g. /pay) is about to be handled. + */ +public class PreTransactionEvent extends TransactionEvent implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private boolean cancelled; + + public PreTransactionEvent(final CommandSource requester, final IUser target, final BigDecimal amount) { + super(!Bukkit.isPrimaryThread(), requester, target, amount); + } + + /** + * 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) { + Preconditions.checkNotNull(decimal, "decimal cannot be null"); + Preconditions.checkArgument(decimal.compareTo(BigDecimal.ZERO) >= 0, "decimal cannot be negative"); + + this.amount = decimal; + } + + @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; + } +} 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 */