Search in sources :

Example 21 with ItemStack

use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class TakeCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
    dInventory inventory = (dInventory) scriptEntry.getObject("inventory");
    Element qty = scriptEntry.getElement("qty");
    Element displayname = scriptEntry.getElement("displayname");
    Element slot = scriptEntry.getElement("slot");
    dList titleAuthor = scriptEntry.getdObject("cover");
    Type type = (Type) scriptEntry.getObject("type");
    Object items_object = scriptEntry.getObject("items");
    List<dItem> items = null;
    if (items_object != null) {
        items = (List<dItem>) items_object;
    }
    dB.report(scriptEntry, getName(), aH.debugObj("Type", type.name()) + qty.debug() + (inventory != null ? inventory.debug() : "") + (displayname != null ? displayname.debug() : "") + aH.debugObj("Items", items) + (slot != null ? slot.debug() : "") + (titleAuthor != null ? titleAuthor.debug() : ""));
    switch(type) {
        case INVENTORY:
            inventory.clear();
            break;
        case ITEMINHAND:
            int inHandAmt = ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getAmount();
            int theAmount = (int) qty.asDouble();
            ItemStack newHandItem = new ItemStack(0);
            if (theAmount > inHandAmt) {
                dB.echoDebug(scriptEntry, "...player did not have enough of the item in hand, so Denizen just took as many as it could. To avoid this situation, use an IF <PLAYER.ITEM_IN_HAND.QTY>.");
                ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
            } else {
                // amount is just right!
                if (theAmount == inHandAmt) {
                    ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
                } else {
                    // amount is less than what's in hand, need to make a new itemstack of what's left...
                    newHandItem = new ItemStack(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getType(), inHandAmt - theAmount, ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getData().getData());
                    newHandItem.setItemMeta(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().getItemInHand().getItemMeta());
                    ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().setItemInHand(newHandItem);
                    ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getPlayerEntity().updateInventory();
                }
            }
            break;
        case MONEY:
            if (Depends.economy != null) {
                Depends.economy.withdrawPlayer(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getOfflinePlayer(), qty.asDouble());
            } else {
                dB.echoError(scriptEntry.getResidingQueue(), "No economy loaded! Have you installed Vault and a compatible economy plugin?");
            }
            break;
        case ITEM:
            for (dItem item : items) {
                ItemStack is = item.getItemStack();
                is.setAmount(qty.asInt());
                if (!inventory.removeItem(item, item.getAmount())) {
                    dB.echoDebug(scriptEntry, "Inventory does not contain at least " + qty.asInt() + " of " + item.getFullString() + "... Taking as much as possible...");
                }
            }
            break;
        case BYDISPLAY:
            int found_items = 0;
            if (displayname == null) {
                dB.echoError(scriptEntry.getResidingQueue(), "Must specify a displayname!");
                return;
            }
            for (ItemStack it : inventory.getContents()) {
                if (found_items < qty.asInt() && it != null && it.hasItemMeta() && it.getItemMeta().hasDisplayName() && it.getItemMeta().getDisplayName().equalsIgnoreCase(displayname.identify())) {
                    int amt = it.getAmount();
                    if (found_items + it.getAmount() <= qty.asInt()) {
                        inventory.getInventory().removeItem(it);
                    } else {
                        it.setAmount(it.getAmount() - (qty.asInt() - found_items));
                        break;
                    }
                    found_items += amt;
                }
            }
            break;
        case SLOT:
            inventory.setSlots(slot.asInt() - 1, new ItemStack(Material.AIR));
            break;
        case BYCOVER:
            inventory.removeBook(titleAuthor.get(0), titleAuthor.size() > 1 ? titleAuthor.get(1) : null, qty.asInt());
            break;
    }
}
Also used : net.aufdemrand.denizen.objects.dItem(net.aufdemrand.denizen.objects.dItem) BukkitScriptEntryData(net.aufdemrand.denizen.BukkitScriptEntryData) Element(net.aufdemrand.denizencore.objects.Element) net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) net.aufdemrand.denizen.objects.dInventory(net.aufdemrand.denizen.objects.dInventory) ItemStack(org.bukkit.inventory.ItemStack)

Example 22 with ItemStack

use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class ItemFlags method flags.

public dList flags() {
    dList output = new dList();
    ItemStack itemStack = item.getItemStack();
    if (itemStack.hasItemMeta()) {
        for (ItemFlag flag : itemStack.getItemMeta().getItemFlags()) {
            output.add(flag.name());
        }
    }
    return output;
}
Also used : net.aufdemrand.denizencore.objects.dList(net.aufdemrand.denizencore.objects.dList) ItemStack(org.bukkit.inventory.ItemStack) ItemFlag(org.bukkit.inventory.ItemFlag)

Example 23 with ItemStack

use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class ItemPatterns method setPatterns.

private void setPatterns(List<Pattern> patterns) {
    ItemStack itemStack = item.getItemStack();
    ItemMeta itemMeta = itemStack.getItemMeta();
    if (itemMeta instanceof BlockStateMeta) {
        Banner banner = (Banner) ((BlockStateMeta) itemMeta).getBlockState();
        banner.setPatterns(patterns);
        banner.update();
        ((BlockStateMeta) itemMeta).setBlockState(banner);
    } else {
        ((BannerMeta) itemMeta).setPatterns(patterns);
    }
    itemStack.setItemMeta(itemMeta);
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) BannerMeta(org.bukkit.inventory.meta.BannerMeta) Banner(org.bukkit.block.Banner) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 24 with ItemStack

use of org.bukkit.inventory.ItemStack in project Denizen-For-Bukkit by DenizenScript.

the class ItemChangeMessage method sendMessage.

public static void sendMessage(Player player, String message) {
    ItemStack item = NMSHandler.getInstance().getEntityHelper().getItemInHand(player);
    // which is probably the least intrusive
    if (item == null || item.getType() == Material.AIR) {
        item = new MaterialData(Material.STAINED_GLASS_PANE, DyeColor.GRAY.getDyeData()).toItemStack();
    } else {
        item = item.clone();
    }
    ItemMeta meta = item.getItemMeta();
    meta.setDisplayName(message);
    item.setItemMeta(meta);
    int slot = player.getInventory().getHeldItemSlot() + 36;
    NMSHandler.getInstance().getPacketHelper().setSlot(player, slot, item, true);
    slotChanged.put(player.getUniqueId(), slot);
}
Also used : MaterialData(org.bukkit.material.MaterialData) ItemStack(org.bukkit.inventory.ItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta)

Example 25 with ItemStack

use of org.bukkit.inventory.ItemStack in project Bukkit by Bukkit.

the class PotionTest method illegalApplyToItemStack.

@Test(expected = IllegalArgumentException.class)
public void illegalApplyToItemStack() {
    Potion potion = new Potion(PotionType.POISON);
    potion.apply(new ItemStack(Material.AIR, 1));
}
Also used : ItemStack(org.bukkit.inventory.ItemStack) Test(org.junit.Test)

Aggregations

ItemStack (org.bukkit.inventory.ItemStack)1282 Player (org.bukkit.entity.Player)262 EventHandler (org.bukkit.event.EventHandler)183 ItemMeta (org.bukkit.inventory.meta.ItemMeta)167 ArrayList (java.util.ArrayList)130 Inventory (org.bukkit.inventory.Inventory)115 Location (org.bukkit.Location)88 Material (org.bukkit.Material)87 PlayerInventory (org.bukkit.inventory.PlayerInventory)84 Entity (org.bukkit.entity.Entity)63 Block (org.bukkit.block.Block)52 TagCompound (de.keyle.knbt.TagCompound)47 Map (java.util.Map)44 LivingEntity (org.bukkit.entity.LivingEntity)44 HashMap (java.util.HashMap)40 Mage (com.elmakers.mine.bukkit.api.magic.Mage)39 Enchantment (org.bukkit.enchantments.Enchantment)38 SkullMeta (org.bukkit.inventory.meta.SkullMeta)38 Vector (org.bukkit.util.Vector)35 Test (org.junit.Test)32