Search in sources :

Example 76 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class Commandworth method run.

@Override
public void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws Exception {
    if (args.length < 1) {
        throw new NotEnoughArgumentsException();
    }
    ItemStack stack = ess.getItemDb().get(args[0]);
    itemWorth(sender, null, stack, args);
}
Also used : ItemStack(org.bukkit.inventory.ItemStack)

Example 77 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class SignSell method onSignInteract.

@Override
protected boolean onSignInteract(final ISign sign, final User player, final String username, final IEssentials ess) throws SignException, ChargeException, MaxMoneyException {
    Trade charge = getTrade(sign, 1, 2, player, ess);
    Trade money = getTrade(sign, 3, ess);
    // Check if the player is trying to sell in bulk.
    if (ess.getSettings().isAllowBulkBuySell() && player.getBase().isSneaking()) {
        ItemStack heldItem = player.getItemInHand();
        if (charge.getItemStack().isSimilar(heldItem)) {
            int initialItemAmount = charge.getItemStack().getAmount();
            int newItemAmount = heldItem.getAmount();
            ItemStack item = charge.getItemStack();
            item.setAmount(newItemAmount);
            charge = new Trade(item, ess);
            BigDecimal chargeAmount = money.getMoney();
            BigDecimal pricePerSingleItem = chargeAmount.divide(new BigDecimal(initialItemAmount));
            pricePerSingleItem = pricePerSingleItem.multiply(new BigDecimal(newItemAmount));
            money = new Trade(pricePerSingleItem, ess);
        }
    }
    charge.isAffordableFor(player);
    money.pay(player, OverflowType.DROP);
    charge.charge(player);
    Trade.log("Sign", "Sell", "Interact", username, charge, username, money, sign.getBlock().getLocation(), ess);
    return true;
}
Also used : Trade(com.earth2me.essentials.Trade) ItemStack(org.bukkit.inventory.ItemStack) BigDecimal(java.math.BigDecimal)

Example 78 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class EssentialsSign method getItemMeta.

protected final ItemStack getItemMeta(final ItemStack item, final String meta, final IEssentials ess) throws SignException {
    ItemStack stack = item;
    try {
        if (!meta.isEmpty()) {
            MetaItemStack metaStack = new MetaItemStack(stack);
            final boolean allowUnsafe = ess.getSettings().allowUnsafeEnchantments();
            metaStack.addStringMeta(null, allowUnsafe, meta, ess);
            stack = metaStack.getItemStack();
        }
    } catch (Exception ex) {
        throw new SignException(ex.getMessage(), ex);
    }
    return stack;
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) MaxMoneyException(net.ess3.api.MaxMoneyException)

Example 79 with ItemStack

use of org.bukkit.inventory.ItemStack 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 80 with ItemStack

use of org.bukkit.inventory.ItemStack in project Essentials by drtshock.

the class Commandsell method run.

@Override
public void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception {
    BigDecimal totalWorth = BigDecimal.ZERO;
    String type = "";
    if (args.length < 1) {
        throw new NotEnoughArgumentsException();
    }
    if (args[0].equalsIgnoreCase("hand") && !user.isAuthorized("essentials.sell.hand")) {
        throw new Exception(tl("sellHandPermission"));
    } else if ((args[0].equalsIgnoreCase("inventory") || args[0].equalsIgnoreCase("invent") || args[0].equalsIgnoreCase("all")) && !user.isAuthorized("essentials.sell.bulk")) {
        throw new Exception(tl("sellBulkPermission"));
    }
    List<ItemStack> is = ess.getItemDb().getMatching(user, args);
    int count = 0;
    boolean isBulk = is.size() > 1;
    for (ItemStack stack : is) {
        try {
            if (stack.getAmount() > 0) {
                totalWorth = totalWorth.add(sellItem(user, stack, args, isBulk));
                stack = stack.clone();
                count++;
                for (ItemStack zeroStack : is) {
                    if (zeroStack.isSimilar(stack)) {
                        zeroStack.setAmount(0);
                    }
                }
            }
        } catch (Exception e) {
            if (!isBulk) {
                throw e;
            }
        }
    }
    if (count != 1) {
        if (args[0].equalsIgnoreCase("blocks")) {
            user.sendMessage(tl("totalWorthBlocks", type, NumberUtil.displayCurrency(totalWorth, ess)));
        } else {
            user.sendMessage(tl("totalWorthAll", type, NumberUtil.displayCurrency(totalWorth, ess)));
        }
    }
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) BigDecimal(java.math.BigDecimal)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)1420 Player (org.bukkit.entity.Player)270 EventHandler (org.bukkit.event.EventHandler)197 ItemMeta (org.bukkit.inventory.meta.ItemMeta)192 ArrayList (java.util.ArrayList)151 Inventory (org.bukkit.inventory.Inventory)127 Material (org.bukkit.Material)103 Location (org.bukkit.Location)96 PlayerInventory (org.bukkit.inventory.PlayerInventory)84 Entity (org.bukkit.entity.Entity)67 Block (org.bukkit.block.Block)54 LivingEntity (org.bukkit.entity.LivingEntity)51 TagCompound (de.keyle.knbt.TagCompound)47 HashMap (java.util.HashMap)47 Map (java.util.Map)46 Vector (org.bukkit.util.Vector)44 Enchantment (org.bukkit.enchantments.Enchantment)41 SkullMeta (org.bukkit.inventory.meta.SkullMeta)41 Mage (com.elmakers.mine.bukkit.api.magic.Mage)39 Test (org.junit.Test)38