mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-09-27 15:22:37 +02:00
Add a cause enum to UserBalanceUpdateEvent (#2824)
Basically, I just needed this for one of my plugins; otherwise, this could be useful for people using this event who want to see where the user's balance is being updated from. --- * Add UserBalanceUpdateEvent.Cause * Add special cause enum * Add API Cause
This commit is contained in:
parent
843ecb4a42
commit
0ebd64d314
@ -162,10 +162,14 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void giveMoney(final BigDecimal value, final CommandSource initiator) throws MaxMoneyException {
|
public void giveMoney(final BigDecimal value, final CommandSource initiator) throws MaxMoneyException {
|
||||||
|
giveMoney(value, initiator, UserBalanceUpdateEvent.Cause.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void giveMoney(final BigDecimal value, final CommandSource initiator, UserBalanceUpdateEvent.Cause cause) throws MaxMoneyException {
|
||||||
if (value.signum() == 0) {
|
if (value.signum() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setMoney(getMoney().add(value));
|
setMoney(getMoney().add(value), cause);
|
||||||
sendMessage(tl("addedToAccount", NumberUtil.displayCurrency(value, ess)));
|
sendMessage(tl("addedToAccount", NumberUtil.displayCurrency(value, ess)));
|
||||||
if (initiator != null) {
|
if (initiator != null) {
|
||||||
initiator.sendMessage(tl("addedToOthersAccount", NumberUtil.displayCurrency(value, ess), this.getDisplayName(), NumberUtil.displayCurrency(getMoney(), ess)));
|
initiator.sendMessage(tl("addedToOthersAccount", NumberUtil.displayCurrency(value, ess), this.getDisplayName(), NumberUtil.displayCurrency(getMoney(), ess)));
|
||||||
@ -174,6 +178,10 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void payUser(final User reciever, final BigDecimal value) throws Exception {
|
public void payUser(final User reciever, final BigDecimal value) throws Exception {
|
||||||
|
payUser(reciever, value, UserBalanceUpdateEvent.Cause.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void payUser(final User reciever, final BigDecimal value, UserBalanceUpdateEvent.Cause cause) throws Exception {
|
||||||
if (value.compareTo(BigDecimal.ZERO) < 1) {
|
if (value.compareTo(BigDecimal.ZERO) < 1) {
|
||||||
throw new Exception(tl("payMustBePositive"));
|
throw new Exception(tl("payMustBePositive"));
|
||||||
}
|
}
|
||||||
@ -195,11 +203,15 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void takeMoney(final BigDecimal value, final CommandSource initiator) {
|
public void takeMoney(final BigDecimal value, final CommandSource initiator) {
|
||||||
|
takeMoney(value, initiator, UserBalanceUpdateEvent.Cause.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void takeMoney(final BigDecimal value, final CommandSource initiator, UserBalanceUpdateEvent.Cause cause) {
|
||||||
if (value.signum() == 0) {
|
if (value.signum() == 0) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
setMoney(getMoney().subtract(value));
|
setMoney(getMoney().subtract(value), cause);
|
||||||
} catch (MaxMoneyException ex) {
|
} catch (MaxMoneyException ex) {
|
||||||
ess.getLogger().log(Level.WARNING, "Invalid call to takeMoney, total balance can't be more than the max-money limit.", ex);
|
ess.getLogger().log(Level.WARNING, "Invalid call to takeMoney, total balance can't be more than the max-money limit.", ex);
|
||||||
}
|
}
|
||||||
@ -440,6 +452,10 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setMoney(final BigDecimal value) throws MaxMoneyException {
|
public void setMoney(final BigDecimal value) throws MaxMoneyException {
|
||||||
|
setMoney(value, UserBalanceUpdateEvent.Cause.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMoney(final BigDecimal value, UserBalanceUpdateEvent.Cause cause) throws MaxMoneyException {
|
||||||
if (ess.getSettings().isEcoDisabled()) {
|
if (ess.getSettings().isEcoDisabled()) {
|
||||||
if (ess.getSettings().isDebug()) {
|
if (ess.getSettings().isDebug()) {
|
||||||
ess.getLogger().info("Internal economy functions disabled, aborting balance change.");
|
ess.getLogger().info("Internal economy functions disabled, aborting balance change.");
|
||||||
@ -447,11 +463,11 @@ public class User extends UserData implements Comparable<User>, IMessageRecipien
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final BigDecimal oldBalance = _getMoney();
|
final BigDecimal oldBalance = _getMoney();
|
||||||
|
|
||||||
UserBalanceUpdateEvent updateEvent = new UserBalanceUpdateEvent(this.getBase(), oldBalance, value);
|
UserBalanceUpdateEvent updateEvent = new UserBalanceUpdateEvent(this.getBase(), oldBalance, value, cause);
|
||||||
ess.getServer().getPluginManager().callEvent(updateEvent);
|
ess.getServer().getPluginManager().callEvent(updateEvent);
|
||||||
BigDecimal newBalance = updateEvent.getNewBalance();
|
BigDecimal newBalance = updateEvent.getNewBalance();
|
||||||
|
|
||||||
if (Methods.hasMethod()) {
|
if (Methods.hasMethod()) {
|
||||||
try {
|
try {
|
||||||
final Method method = Methods.getMethod();
|
final Method method = Methods.getMethod();
|
||||||
|
@ -8,6 +8,7 @@ import com.earth2me.essentials.utils.StringUtil;
|
|||||||
import com.google.common.base.Charsets;
|
import com.google.common.base.Charsets;
|
||||||
import net.ess3.api.IEssentials;
|
import net.ess3.api.IEssentials;
|
||||||
import net.ess3.api.MaxMoneyException;
|
import net.ess3.api.MaxMoneyException;
|
||||||
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@ -144,7 +145,7 @@ public class Economy {
|
|||||||
throw new NoLoanPermittedException();
|
throw new NoLoanPermittedException();
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
user.setMoney(balance);
|
user.setMoney(balance, UserBalanceUpdateEvent.Cause.API);
|
||||||
} catch (MaxMoneyException ex) {
|
} catch (MaxMoneyException ex) {
|
||||||
//TODO: Update API to show max balance errors
|
//TODO: Update API to show max balance errors
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import com.earth2me.essentials.User;
|
|||||||
import com.earth2me.essentials.utils.NumberUtil;
|
import com.earth2me.essentials.utils.NumberUtil;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import net.ess3.api.MaxMoneyException;
|
import net.ess3.api.MaxMoneyException;
|
||||||
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@ -54,7 +55,7 @@ public class Commandeco extends EssentialsLoopCommand {
|
|||||||
protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) throws NotEnoughArgumentsException, ChargeException, MaxMoneyException {
|
protected void updatePlayer(final Server server, final CommandSource sender, final User player, final String[] args) throws NotEnoughArgumentsException, ChargeException, MaxMoneyException {
|
||||||
switch (cmd) {
|
switch (cmd) {
|
||||||
case GIVE:
|
case GIVE:
|
||||||
player.giveMoney(amount, sender);
|
player.giveMoney(amount, sender, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case TAKE:
|
case TAKE:
|
||||||
@ -72,10 +73,10 @@ public class Commandeco extends EssentialsLoopCommand {
|
|||||||
BigDecimal money = player.getMoney();
|
BigDecimal money = player.getMoney();
|
||||||
BigDecimal minBalance = ess.getSettings().getMinMoney();
|
BigDecimal minBalance = ess.getSettings().getMinMoney();
|
||||||
if (money.subtract(amount).compareTo(minBalance) >= 0) {
|
if (money.subtract(amount).compareTo(minBalance) >= 0) {
|
||||||
player.takeMoney(amount, sender);
|
player.takeMoney(amount, sender, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
||||||
} else if (sender == null) {
|
} else if (sender == null) {
|
||||||
try {
|
try {
|
||||||
player.setMoney(minBalance);
|
player.setMoney(minBalance, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
||||||
} catch (MaxMoneyException ex) {
|
} catch (MaxMoneyException ex) {
|
||||||
// Take shouldn't be able to throw a max money exception
|
// Take shouldn't be able to throw a max money exception
|
||||||
}
|
}
|
||||||
@ -90,7 +91,7 @@ public class Commandeco extends EssentialsLoopCommand {
|
|||||||
BigDecimal maxBalance = ess.getSettings().getMaxMoney();
|
BigDecimal maxBalance = ess.getSettings().getMaxMoney();
|
||||||
boolean underMinimum = (amount.compareTo(minBalance) < 0);
|
boolean underMinimum = (amount.compareTo(minBalance) < 0);
|
||||||
boolean aboveMax = (amount.compareTo(maxBalance) > 0);
|
boolean aboveMax = (amount.compareTo(maxBalance) > 0);
|
||||||
player.setMoney(underMinimum ? minBalance : aboveMax ? maxBalance : amount);
|
player.setMoney(underMinimum ? minBalance : aboveMax ? maxBalance : amount, UserBalanceUpdateEvent.Cause.COMMAND_ECO);
|
||||||
player.sendMessage(tl("setBal", NumberUtil.displayCurrency(player.getMoney(), ess)));
|
player.sendMessage(tl("setBal", NumberUtil.displayCurrency(player.getMoney(), ess)));
|
||||||
if (sender != null) {
|
if (sender != null) {
|
||||||
sender.sendMessage(tl("setBalOthers", player.getDisplayName(), NumberUtil.displayCurrency(player.getMoney(), ess)));
|
sender.sendMessage(tl("setBalOthers", player.getDisplayName(), NumberUtil.displayCurrency(player.getMoney(), ess)));
|
||||||
|
@ -9,6 +9,7 @@ import com.earth2me.essentials.utils.StringUtil;
|
|||||||
|
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
import net.ess3.api.MaxMoneyException;
|
import net.ess3.api.MaxMoneyException;
|
||||||
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
@ -73,7 +74,7 @@ public class Commandpay extends EssentialsLoopCommand {
|
|||||||
user.getConfirmingPayments().put(player, amount);
|
user.getConfirmingPayments().put(player, amount);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user.payUser(player, amount);
|
user.payUser(player, amount, UserBalanceUpdateEvent.Cause.COMMAND_PAY);
|
||||||
user.getConfirmingPayments().remove(player);
|
user.getConfirmingPayments().remove(player);
|
||||||
Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
|
Trade.log("Command", "Pay", "Player", user.getName(), new Trade(amount, ess), player.getName(), new Trade(amount, ess), user.getLocation(), ess);
|
||||||
} catch (MaxMoneyException ex) {
|
} catch (MaxMoneyException ex) {
|
||||||
|
@ -4,6 +4,7 @@ import com.earth2me.essentials.Trade;
|
|||||||
import com.earth2me.essentials.User;
|
import com.earth2me.essentials.User;
|
||||||
import com.earth2me.essentials.utils.NumberUtil;
|
import com.earth2me.essentials.utils.NumberUtil;
|
||||||
import com.google.common.collect.Lists;
|
import com.google.common.collect.Lists;
|
||||||
|
import net.ess3.api.events.UserBalanceUpdateEvent;
|
||||||
import org.bukkit.Server;
|
import org.bukkit.Server;
|
||||||
import org.bukkit.inventory.ItemStack;
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
@ -94,7 +95,7 @@ public class Commandsell extends EssentialsCommand {
|
|||||||
user.getBase().getInventory().removeItem(ris);
|
user.getBase().getInventory().removeItem(ris);
|
||||||
user.getBase().updateInventory();
|
user.getBase().updateInventory();
|
||||||
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess);
|
Trade.log("Command", "Sell", "Item", user.getName(), new Trade(ris, ess), user.getName(), new Trade(result, ess), user.getLocation(), ess);
|
||||||
user.giveMoney(result);
|
user.giveMoney(result, null, UserBalanceUpdateEvent.Cause.COMMAND_SELL);
|
||||||
user.sendMessage(tl("itemSold", NumberUtil.displayCurrency(result, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)));
|
user.sendMessage(tl("itemSold", NumberUtil.displayCurrency(result, ess), amount, is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(worth, ess)));
|
||||||
logger.log(Level.INFO, tl("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
|
logger.log(Level.INFO, tl("itemSoldConsole", user.getDisplayName(), is.getType().toString().toLowerCase(Locale.ENGLISH), NumberUtil.displayCurrency(result, ess), amount, NumberUtil.displayCurrency(worth, ess)));
|
||||||
return result;
|
return result;
|
||||||
|
@ -15,12 +15,19 @@ public class UserBalanceUpdateEvent extends Event {
|
|||||||
private final Player player;
|
private final Player player;
|
||||||
private final BigDecimal originalBalance;
|
private final BigDecimal originalBalance;
|
||||||
private BigDecimal balance;
|
private BigDecimal balance;
|
||||||
|
private Cause cause;
|
||||||
|
|
||||||
|
@Deprecated
|
||||||
public UserBalanceUpdateEvent(Player player, BigDecimal originalBalance, BigDecimal balance) {
|
public UserBalanceUpdateEvent(Player player, BigDecimal originalBalance, BigDecimal balance) {
|
||||||
|
this(player, originalBalance, balance, Cause.UNKNOWN);
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserBalanceUpdateEvent(Player player, BigDecimal originalBalance, BigDecimal balance, Cause cause) {
|
||||||
super(!Bukkit.getServer().isPrimaryThread());
|
super(!Bukkit.getServer().isPrimaryThread());
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.originalBalance = originalBalance;
|
this.originalBalance = originalBalance;
|
||||||
this.balance = balance;
|
this.balance = balance;
|
||||||
|
this.cause = cause;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -48,4 +55,17 @@ public class UserBalanceUpdateEvent extends Event {
|
|||||||
public BigDecimal getOldBalance() {
|
public BigDecimal getOldBalance() {
|
||||||
return originalBalance;
|
return originalBalance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Cause getCause() {
|
||||||
|
return cause;
|
||||||
|
}
|
||||||
|
|
||||||
|
public enum Cause {
|
||||||
|
COMMAND_ECO,
|
||||||
|
COMMAND_PAY,
|
||||||
|
COMMAND_SELL,
|
||||||
|
API,
|
||||||
|
SPECIAL, // Reserved for API usage
|
||||||
|
UNKNOWN
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user