From a20d20574e8cd8493fd2fd744ac8137659aa2512 Mon Sep 17 00:00:00 2001 From: Olivia Date: Fri, 1 Jan 2021 21:45:52 -0600 Subject: [PATCH] Add TransactionEvent for successful transactions (#3649) --- .../java/com/earth2me/essentials/User.java | 3 + .../api/v2/events/TransactionEvent.java | 57 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/User.java b/Essentials/src/main/java/com/earth2me/essentials/User.java index 6d399d734..83ba35ba4 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/User.java +++ b/Essentials/src/main/java/com/earth2me/essentials/User.java @@ -15,6 +15,7 @@ import net.ess3.api.events.AfkStatusChangeEvent; import net.ess3.api.events.JailStatusChangeEvent; import net.ess3.api.events.MuteStatusChangeEvent; import net.ess3.api.events.UserBalanceUpdateEvent; +import net.essentialsx.api.v2.events.TransactionEvent; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -199,6 +200,8 @@ public class User extends UserData implements Comparable, IMessageRecipien reciever.setMoney(reciever.getMoney().add(value), cause); sendMessage(tl("moneySentTo", NumberUtil.displayCurrency(value, ess), reciever.getDisplayName())); reciever.sendMessage(tl("moneyRecievedFrom", NumberUtil.displayCurrency(value, ess), getDisplayName())); + final TransactionEvent transactionEvent = new TransactionEvent(this.getSource(), reciever, value); + ess.getServer().getPluginManager().callEvent(transactionEvent); } else { throw new ChargeException(tl("notEnoughMoney", NumberUtil.displayCurrency(value, ess))); } 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 new file mode 100644 index 000000000..9e09c4f58 --- /dev/null +++ b/Essentials/src/main/java/net/essentialsx/api/v2/events/TransactionEvent.java @@ -0,0 +1,57 @@ +package net.essentialsx.api.v2.events; + +import com.earth2me.essentials.CommandSource; +import net.ess3.api.IUser; +import org.bukkit.Bukkit; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +import java.math.BigDecimal; + +/** + * Fired when a transaction (e.g. /pay) is successfully handled. + */ +public class TransactionEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + + private final CommandSource requester; + private final IUser target; + private final BigDecimal amount; + + public TransactionEvent(CommandSource requester, IUser target, 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 HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +}