use of com.earth2me.essentials.Trade in project Essentials by EssentialsX.
the class Economy method resetBalance.
/**
* Resets the balance of a user to the starting balance
*
* @param name Name of the user
*
* @throws UserDoesNotExistException If a user by that name does not exists
* @throws NoLoanPermittedException If the user is not allowed to have a negative balance
*/
public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException {
if (ess == null) {
throw new RuntimeException(noCallBeforeLoad);
}
setMoney(name, ess.getSettings().getStartingBalance());
Trade.log("API", "Reset", "API", name, new Trade(BigDecimal.ZERO, ess), null, null, null, ess);
}
use of com.earth2me.essentials.Trade in project Essentials by EssentialsX.
the class Economy method divide.
public static void divide(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
BigDecimal result = getMoneyExact(name).divide(amount, MATH_CONTEXT);
setMoney(name, result);
Trade.log("API", "Divide", "API", name, new Trade(amount, ess), null, null, null, ess);
}
use of com.earth2me.essentials.Trade in project Essentials by EssentialsX.
the class Economy method add.
public static void add(String name, BigDecimal amount) throws UserDoesNotExistException, NoLoanPermittedException, ArithmeticException {
BigDecimal result = getMoneyExact(name).add(amount, MATH_CONTEXT);
setMoney(name, result);
Trade.log("API", "Add", "API", name, new Trade(amount, ess), null, null, null, ess);
}
use of com.earth2me.essentials.Trade in project Essentials by EssentialsX.
the class SignBuy method onSignInteract.
@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
Trade items = getTrade(sign, 1, 2, player, ess);
Trade charge = getTrade(sign, 3, ess);
// Check if the player is trying to buy in bulk.
if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) {
ItemStack heldItem = player.getItemInHand();
if (items.getItemStack().isSimilar(heldItem)) {
int initialItemAmount = items.getItemStack().getAmount();
int newItemAmount = heldItem.getAmount();
ItemStack item = items.getItemStack();
item.setAmount(newItemAmount);
items = new Trade(item, ess);
BigDecimal chargeAmount = charge.getMoney();
BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
charge = new Trade(pricePerSingleItem, ess);
}
}
charge.isAffordableFor(player);
if (!items.pay(player)) {
// TODO: TL
throw new ChargeException("Inventory full");
}
charge.charge(player);
Trade.log("Sign", "Buy", "Interact", username, charge, username, items, sign.getBlock().getLocation(), ess);
return true;
}
use of com.earth2me.essentials.Trade in project Essentials by EssentialsX.
the class SignEnchant method onSignInteract.
@Override
protected boolean onSignInteract(ISign sign, User player, String username, IEssentials ess) throws SignException, ChargeException {
final ItemStack search = sign.getLine(1).equals("*") || sign.getLine(1).equalsIgnoreCase("any") ? null : getItemStack(sign.getLine(1), 1, ess);
final Trade charge = getTrade(sign, 3, ess);
charge.isAffordableFor(player);
final String[] enchantLevel = sign.getLine(2).split(":");
if (enchantLevel.length != 2) {
throw new SignException(tl("invalidSignLine", 3));
}
final Enchantment enchantment = Enchantments.getByName(enchantLevel[0]);
if (enchantment == null) {
throw new SignException(tl("enchantmentNotFound"));
}
int level;
try {
level = Integer.parseInt(enchantLevel[1]);
} catch (NumberFormatException ex) {
level = enchantment.getMaxLevel();
}
final ItemStack playerHand = player.getBase().getItemInHand();
if (playerHand == null || playerHand.getAmount() != 1 || (playerHand.containsEnchantment(enchantment) && playerHand.getEnchantmentLevel(enchantment) == level)) {
throw new SignException(tl("missingItems", 1, sign.getLine(1)));
}
if (search != null && playerHand.getType() != search.getType()) {
throw new SignException(tl("missingItems", 1, search.getType().toString().toLowerCase(Locale.ENGLISH).replace('_', ' ')));
}
final ItemStack toEnchant = playerHand;
try {
if (level == 0) {
toEnchant.removeEnchantment(enchantment);
} else {
if (ess.getSettings().allowUnsafeEnchantments() && player.isAuthorized("essentials.signs.enchant.allowunsafe")) {
toEnchant.addUnsafeEnchantment(enchantment, level);
} else {
toEnchant.addEnchantment(enchantment, level);
}
}
} catch (Exception ex) {
throw new SignException(ex.getMessage(), ex);
}
final String enchantmentName = enchantment.getName().toLowerCase(Locale.ENGLISH);
if (level == 0) {
player.sendMessage(tl("enchantmentRemoved", enchantmentName.replace('_', ' ')));
} else {
player.sendMessage(tl("enchantmentApplied", enchantmentName.replace('_', ' ')));
}
charge.charge(player);
Trade.log("Sign", "Enchant", "Interact", username, charge, username, charge, sign.getBlock().getLocation(), ess);
player.getBase().updateInventory();
return true;
}
Aggregations