Search in sources :

Example 66 with Trade

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);
}
Also used : Trade(com.earth2me.essentials.Trade)

Example 67 with Trade

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);
}
Also used : Trade(com.earth2me.essentials.Trade) BigDecimal(java.math.BigDecimal)

Example 68 with Trade

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);
}
Also used : Trade(com.earth2me.essentials.Trade) BigDecimal(java.math.BigDecimal)

Example 69 with Trade

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;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) ChargeException(com.earth2me.essentials.ChargeException) BigDecimal(java.math.BigDecimal)

Example 70 with Trade

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;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) Enchantment(org.bukkit.enchantments.Enchantment) ChargeException(com.earth2me.essentials.ChargeException)

Aggregations

Trade (com.earth2me.essentials.Trade)86 ItemStack (org.bukkit.inventory.ItemStack)26 ChargeException (com.earth2me.essentials.ChargeException)20 BigDecimal (java.math.BigDecimal)18 Location (org.bukkit.Location)18 User (com.earth2me.essentials.User)14 NoChargeException (com.earth2me.essentials.commands.NoChargeException)4 MaxMoneyException (net.ess3.api.MaxMoneyException)4 World (org.bukkit.World)4 Kit (com.earth2me.essentials.Kit)2 Mob (com.earth2me.essentials.Mob)2 Teleport (com.earth2me.essentials.Teleport)2 Commandrepair (com.earth2me.essentials.commands.Commandrepair)2 NotEnoughArgumentsException (com.earth2me.essentials.commands.NotEnoughArgumentsException)2 IText (com.earth2me.essentials.textreader.IText)2 KeywordReplacer (com.earth2me.essentials.textreader.KeywordReplacer)2 TextInput (com.earth2me.essentials.textreader.TextInput)2 TextPager (com.earth2me.essentials.textreader.TextPager)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2