Search in sources :

Example 56 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class PlayerBreaksItemScriptEvent method onPlayerItemBreak.

@EventHandler
public void onPlayerItemBreak(PlayerItemBreakEvent event) {
    if (EntityTag.isNPC(event.getPlayer())) {
        return;
    }
    item = new ItemTag(event.getBrokenItem());
    this.event = event;
    cancelled = false;
    fire(event);
}
Also used : ItemTag(com.denizenscript.denizen.objects.ItemTag) EventHandler(org.bukkit.event.EventHandler)

Example 57 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class PlayerConsumesScriptEvent method onPlayerConsumes.

@EventHandler
public void onPlayerConsumes(PlayerItemConsumeEvent event) {
    if (EntityTag.isNPC(event.getPlayer())) {
        return;
    }
    item = new ItemTag(event.getItem());
    this.event = event;
    fire(event);
}
Also used : ItemTag(com.denizenscript.denizen.objects.ItemTag) EventHandler(org.bukkit.event.EventHandler)

Example 58 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemInventory method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("inventory_contents") && mechanism.hasValue()) {
        List<ItemStack> items = new ArrayList<>();
        for (ItemTag item : mechanism.valueAsType(ListTag.class).filter(ItemTag.class, mechanism.context)) {
            items.add(item.getItemStack());
        }
        if (item.getItemMeta() instanceof BlockStateMeta) {
            BlockStateMeta bsm = ((BlockStateMeta) item.getItemMeta());
            InventoryHolder invHolder = (InventoryHolder) bsm.getBlockState();
            if (items.size() > getInventoryFor(invHolder).getSize()) {
                mechanism.echoError("Invalid inventory_contents input size; expected " + getInventoryFor(invHolder).getSize() + " or less.");
                return;
            }
            getInventoryFor(invHolder).setContents(items.toArray(new ItemStack[0]));
            bsm.setBlockState((BlockState) invHolder);
            item.setItemMeta(bsm);
        } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
            BundleMeta bundle = (BundleMeta) item.getItemMeta();
            bundle.setItems(items);
            item.setItemMeta(bundle);
        }
    }
    if (mechanism.matches("inventory") && mechanism.hasValue()) {
        Deprecations.itemInventoryTag.warn(mechanism.context);
        Argument argument = new Argument("");
        argument.unsetValue();
        argument.object = mechanism.getValue();
        Map.Entry<Integer, InventoryTag> inventoryPair = Conversion.getInventory(argument, mechanism.context);
        if (inventoryPair == null || inventoryPair.getValue().getInventory() == null) {
            return;
        }
        ListTag items = InventoryContents.getFrom(inventoryPair.getValue()).getContents(false);
        ItemStack[] itemArray = new ItemStack[items.size()];
        for (int i = 0; i < itemArray.length; i++) {
            itemArray[i] = ((ItemTag) items.objectForms.get(i)).getItemStack().clone();
        }
        if (item.getItemMeta() instanceof BlockStateMeta) {
            BlockStateMeta bsm = ((BlockStateMeta) item.getItemMeta());
            InventoryHolder invHolder = (InventoryHolder) bsm.getBlockState();
            if (items.size() > getInventoryFor(invHolder).getSize()) {
                mechanism.echoError("Invalid inventory mechanism input size; expected " + getInventoryFor(invHolder).getSize() + " or less.");
                return;
            }
            getInventoryFor(invHolder).setContents(itemArray);
            bsm.setBlockState((BlockState) invHolder);
            item.setItemMeta(bsm);
        } else if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_17)) {
            BundleMeta bundle = (BundleMeta) item.getItemMeta();
            bundle.setItems(Arrays.asList(itemArray));
            item.setItemMeta(bundle);
        }
    }
}
Also used : BundleMeta(org.bukkit.inventory.meta.BundleMeta) Argument(com.denizenscript.denizencore.objects.Argument) ArrayList(java.util.ArrayList) ListTag(com.denizenscript.denizencore.objects.core.ListTag) BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) ItemStack(org.bukkit.inventory.ItemStack) ItemTag(com.denizenscript.denizen.objects.ItemTag) InventoryHolder(org.bukkit.inventory.InventoryHolder) Map(java.util.Map) InventoryTag(com.denizenscript.denizen.objects.InventoryTag)

Example 59 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemRawNBT method setFullNBT.

public void setFullNBT(ItemTag item, MapTag input, TagContext context, boolean retainOld) {
    CompoundTag compoundTag = retainOld ? NMSHandler.getItemHelper().getNbtData(item.getItemStack()) : null;
    Map<String, Tag> result = compoundTag == null ? new LinkedHashMap<>() : new LinkedHashMap<>(compoundTag.getValue());
    for (Map.Entry<StringHolder, ObjectTag> entry : input.map.entrySet()) {
        try {
            Tag tag = convertObjectToNbt(entry.getValue().toString(), context, "(item).");
            if (tag != null) {
                result.put(entry.getKey().str, tag);
            }
        } catch (Exception ex) {
            Debug.echoError("Raw_Nbt input failed for root key '" + entry.getKey().str + "'.");
            Debug.echoError(ex);
            return;
        }
    }
    compoundTag = NMSHandler.getInstance().createCompoundTag(result);
    item.setItemStack(NMSHandler.getItemHelper().setNbtData(item.getItemStack(), compoundTag));
}
Also used : StringHolder(com.denizenscript.denizencore.utilities.text.StringHolder) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ListTag(com.denizenscript.denizencore.objects.core.ListTag) ObjectTag(com.denizenscript.denizencore.objects.ObjectTag) ItemTag(com.denizenscript.denizen.objects.ItemTag) MapTag(com.denizenscript.denizencore.objects.core.MapTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag)

Example 60 with ItemTag

use of com.denizenscript.denizen.objects.ItemTag in project Denizen-For-Bukkit by DenizenScript.

the class ItemFlags method getObjectAttribute.

@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
    if (attribute == null) {
        return null;
    }
    // -->
    if (attribute.startsWith("with_flag")) {
        ItemTag item = new ItemTag(this.item.getItemStack().clone());
        FlagCommand.FlagActionProvider provider = new FlagCommand.FlagActionProvider();
        provider.tracker = item.getFlagTracker();
        DataAction action = DataActionHelper.parse(provider, attribute.getParam(), attribute.context);
        // -->
        if (attribute.startsWith("duration", 2)) {
            provider.expiration = new TimeTag(TimeTag.now().millis() + attribute.getContextObject(2).asType(DurationTag.class, attribute.context).getMillis());
            attribute.fulfill(1);
        }
        action.execute(attribute.context);
        item.reapplyTracker(provider.tracker);
        return item.getObjectAttribute(attribute.fulfill(1));
    }
    return null;
}
Also used : FlagCommand(com.denizenscript.denizencore.scripts.commands.core.FlagCommand) DataAction(com.denizenscript.denizencore.utilities.data.DataAction) TimeTag(com.denizenscript.denizencore.objects.core.TimeTag) ItemTag(com.denizenscript.denizen.objects.ItemTag)

Aggregations

ItemTag (com.denizenscript.denizen.objects.ItemTag)77 EventHandler (org.bukkit.event.EventHandler)35 ItemStack (org.bukkit.inventory.ItemStack)20 LocationTag (com.denizenscript.denizen.objects.LocationTag)19 EntityTag (com.denizenscript.denizen.objects.EntityTag)14 ListTag (com.denizenscript.denizencore.objects.core.ListTag)14 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)13 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)9 Item (org.bukkit.entity.Item)7 Argument (com.denizenscript.denizencore.objects.Argument)6 List (java.util.List)6 InvalidArgumentsException (com.denizenscript.denizencore.exceptions.InvalidArgumentsException)5 ScriptTag (com.denizenscript.denizencore.objects.core.ScriptTag)5 HumanEntity (org.bukkit.entity.HumanEntity)5 Player (org.bukkit.entity.Player)5 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)4 BukkitTagContext (com.denizenscript.denizen.tags.BukkitTagContext)4 DurationTag (com.denizenscript.denizencore.objects.core.DurationTag)4 ArrayList (java.util.ArrayList)4 MaterialTag (com.denizenscript.denizen.objects.MaterialTag)3