Search in sources :

Example 1 with ChargeException

use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.

the class Commandeco method take.

private void take(BigDecimal amount, final User player, final CommandSource sender) throws ChargeException {
    BigDecimal money = player.getMoney();
    BigDecimal minBalance = ess.getSettings().getMinMoney();
    if (money.subtract(amount).compareTo(minBalance) > 0) {
        player.takeMoney(amount, sender);
    } else if (sender == null) {
        try {
            player.setMoney(minBalance);
        } catch (MaxMoneyException ex) {
        // Take shouldn't be able to throw a max money exception
        }
        player.sendMessage(tl("takenFromAccount", NumberUtil.displayCurrency(player.getMoney(), ess)));
    } else {
        throw new ChargeException(tl("insufficientFunds"));
    }
}
Also used : MaxMoneyException(net.ess3.api.MaxMoneyException) ChargeException(com.earth2me.essentials.ChargeException) BigDecimal(java.math.BigDecimal)

Example 2 with ChargeException

use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.

the class SignKit method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException {
    final String kitName = sign.getLine(1).toLowerCase(Locale.ENGLISH).trim();
    final String group = sign.getLine(2).trim();
    if ((!group.isEmpty() && ("ยง2Everyone".equals(group) || player.inGroup(group))) || (group.isEmpty() && (player.isAuthorized("essentials.kits." + kitName)))) {
        final Trade charge = getTrade(sign, 3, ess);
        charge.isAffordableFor(player);
        try {
            final Kit kit = new Kit(kitName, ess);
            kit.checkDelay(player);
            kit.setTime(player);
            kit.expandItems(player);
            charge.charge(player);
            Trade.log("Sign", "Kit", "Interact", username, null, username, charge, sign.getBlock().getLocation(), ess);
        } catch (NoChargeException ex) {
            return false;
        } catch (Exception ex) {
            throw new SignException(ex.getMessage(), ex);
        }
        return true;
    } else {
        if (group.isEmpty()) {
            throw new SignException(tl("noKitPermission", "essentials.kits." + kitName));
        } else {
            throw new SignException(tl("noKitGroup", group));
        }
    }
}
Also used : Trade(com.earth2me.essentials.Trade) NoChargeException(com.earth2me.essentials.commands.NoChargeException) Kit(com.earth2me.essentials.Kit) NoChargeException(com.earth2me.essentials.commands.NoChargeException) ChargeException(com.earth2me.essentials.ChargeException)

Example 3 with ChargeException

use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.

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 4 with ChargeException

use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.

the class Commandrepair method repairItems.

private void repairItems(final ItemStack[] items, final IUser user, final List<String> repaired) {
    for (ItemStack item : items) {
        if (item == null || item.getType().isBlock() || item.getDurability() == 0) {
            continue;
        }
        final String itemName = item.getType().toString().toLowerCase(Locale.ENGLISH);
        final Trade charge = new Trade("repair-" + itemName.replace('_', '-'), new Trade("repair-" + item.getTypeId(), new Trade("repair-item", ess), ess), ess);
        try {
            charge.isAffordableFor(user);
        } catch (ChargeException ex) {
            user.sendMessage(ex.getMessage());
            continue;
        }
        if (!item.getEnchantments().isEmpty() && !ess.getSettings().getRepairEnchanted() && !user.isAuthorized("essentials.repair.enchanted")) {
            continue;
        }
        try {
            repairItem(item);
        } catch (Exception e) {
            continue;
        }
        try {
            charge.charge(user);
        } catch (ChargeException ex) {
            user.sendMessage(ex.getMessage());
        }
        repaired.add(itemName.replace('_', ' '));
    }
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) ChargeException(com.earth2me.essentials.ChargeException) ChargeException(com.earth2me.essentials.ChargeException)

Example 5 with ChargeException

use of com.earth2me.essentials.ChargeException in project Essentials by drtshock.

the class SignTrade method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
    if (sign.getLine(3).substring(2).equalsIgnoreCase(username)) {
        final Trade store = rechargeSign(sign, ess, player);
        Trade stored;
        try {
            stored = getTrade(sign, 1, AmountType.TOTAL, true, ess);
            subtractAmount(sign, 1, stored, ess);
            Map<Integer, ItemStack> withdraw = stored.pay(player, OverflowType.RETURN);
            if (withdraw == null) {
                Trade.log("Sign", "Trade", "Withdraw", username, store, username, null, sign.getBlock().getLocation(), ess);
            } else {
                setAmount(sign, 1, BigDecimal.valueOf(withdraw.get(0).getAmount()), ess);
                Trade.log("Sign", "Trade", "Withdraw", username, stored, username, new Trade(withdraw.get(0), ess), sign.getBlock().getLocation(), ess);
            }
        } catch (SignException e) {
            if (store == null) {
                throw new SignException(tl("tradeSignEmptyOwner"), e);
            }
        }
        Trade.log("Sign", "Trade", "Deposit", username, store, username, null, sign.getBlock().getLocation(), ess);
    } else {
        final Trade charge = getTrade(sign, 1, AmountType.COST, false, ess);
        final Trade trade = getTrade(sign, 2, AmountType.COST, true, ess);
        charge.isAffordableFor(player);
        addAmount(sign, 1, charge, ess);
        subtractAmount(sign, 2, trade, ess);
        if (!trade.pay(player)) {
            subtractAmount(sign, 1, charge, ess);
            addAmount(sign, 2, trade, ess);
            throw new ChargeException("Full inventory");
        }
        charge.charge(player);
        Trade.log("Sign", "Trade", "Interact", sign.getLine(3), charge, username, trade, sign.getBlock().getLocation(), ess);
    }
    sign.updateSign();
    return true;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) ChargeException(com.earth2me.essentials.ChargeException)

Aggregations

ChargeException (com.earth2me.essentials.ChargeException)10 Trade (com.earth2me.essentials.Trade)9 ItemStack (org.bukkit.inventory.ItemStack)4 BigDecimal (java.math.BigDecimal)2 MaxMoneyException (net.ess3.api.MaxMoneyException)2 Kit (com.earth2me.essentials.Kit)1 User (com.earth2me.essentials.User)1 Commandrepair (com.earth2me.essentials.commands.Commandrepair)1 NoChargeException (com.earth2me.essentials.commands.NoChargeException)1 NotEnoughArgumentsException (com.earth2me.essentials.commands.NotEnoughArgumentsException)1 Enchantment (org.bukkit.enchantments.Enchantment)1