Search in sources :

Example 1 with BundleMeta

use of org.bukkit.inventory.meta.BundleMeta 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)

Aggregations

InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 Argument (com.denizenscript.denizencore.objects.Argument)1 ListTag (com.denizenscript.denizencore.objects.core.ListTag)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 InventoryHolder (org.bukkit.inventory.InventoryHolder)1 ItemStack (org.bukkit.inventory.ItemStack)1 BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)1 BundleMeta (org.bukkit.inventory.meta.BundleMeta)1