Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions Essentials/src/main/java/com/earth2me/essentials/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should reassign value to getAmount from PreTransactionEvent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done this now. Do you think it should also repeat the check for canAfford?

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)));
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
Loading