Make UserBalanceUpdateEvent's new balance mutable.

This commit makes it reliable to modify user balance from within events.
This commit is contained in:
Ali Moghnieh 2016-06-18 18:33:51 +01:00
parent 3fa810e329
commit dba9dab985
2 changed files with 16 additions and 5 deletions

View File

@ -375,6 +375,11 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
return;
}
final BigDecimal oldBalance = _getMoney();
UserBalanceUpdateEvent updateEvent = new UserBalanceUpdateEvent(this.getBase(), oldBalance, value);
ess.getServer().getPluginManager().callEvent(updateEvent);
BigDecimal newBalance = updateEvent.getNewBalance();
if (Methods.hasMethod()) {
try {
final Method method = Methods.getMethod();
@ -382,13 +387,12 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
throw new Exception();
}
final Method.MethodAccount account = Methods.getMethod().getAccount(this.getName());
account.set(value.doubleValue());
account.set(newBalance.doubleValue());
} catch (Exception ex) {
}
}
super.setMoney(value, true);
ess.getServer().getPluginManager().callEvent(new UserBalanceUpdateEvent(this.getBase(), oldBalance, value));
Trade.log("Update", "Set", "API", getName(), new Trade(value, ess), null, null, null, ess);
super.setMoney(newBalance, true);
Trade.log("Update", "Set", "API", getName(), new Trade(newBalance, ess), null, null, null, ess);
}
public void updateMoneyCache(final BigDecimal value) {

View File

@ -1,5 +1,7 @@
package net.ess3.api.events;
import com.google.common.base.Preconditions;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
@ -11,7 +13,7 @@ public class UserBalanceUpdateEvent extends Event {
private static final HandlerList handlers = new HandlerList();
private final Player player;
private final BigDecimal originalBalance;
private final BigDecimal balance;
private BigDecimal balance;
public UserBalanceUpdateEvent(Player player, BigDecimal originalBalance, BigDecimal balance) {
this.player = player;
@ -35,6 +37,11 @@ public class UserBalanceUpdateEvent extends Event {
public BigDecimal getNewBalance() {
return balance;
}
public void setNewBalance(BigDecimal newBalance) {
Preconditions.checkNotNull(newBalance, "newBalance cannot be null.");
this.balance = newBalance;
}
public BigDecimal getOldBalance() {
return originalBalance;