Search in sources :

Example 31 with BlockStateMeta

use of org.bukkit.inventory.meta.BlockStateMeta 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 32 with BlockStateMeta

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

the class ItemLock method adjust.

@Override
public void adjust(Mechanism mechanism) {
    // -->
    if (mechanism.matches("lock")) {
        BlockStateMeta bsm = ((BlockStateMeta) item.getItemMeta());
        Lockable lockable = (Lockable) bsm.getBlockState();
        lockable.setLock(mechanism.hasValue() ? mechanism.getValue().asString() : null);
        bsm.setBlockState((BlockState) lockable);
        item.setItemMeta(bsm);
    }
}
Also used : BlockStateMeta(org.bukkit.inventory.meta.BlockStateMeta) Lockable(org.bukkit.block.Lockable)

Aggregations

BlockStateMeta (org.bukkit.inventory.meta.BlockStateMeta)32 CreatureSpawner (org.bukkit.block.CreatureSpawner)22 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)6 Banner (org.bukkit.block.Banner)5 BannerMeta (org.bukkit.inventory.meta.BannerMeta)5 ItemMeta (org.bukkit.inventory.meta.ItemMeta)5 ListTag (com.denizenscript.denizencore.objects.core.ListTag)4 ItemStack (org.bukkit.inventory.ItemStack)3 EntityTag (com.denizenscript.denizen.objects.EntityTag)2 BlockState (org.bukkit.block.BlockState)2 InventoryHolder (org.bukkit.inventory.InventoryHolder)2 InventoryTag (com.denizenscript.denizen.objects.InventoryTag)1 ItemTag (com.denizenscript.denizen.objects.ItemTag)1 ItemScriptContainer (com.denizenscript.denizen.scripts.containers.core.ItemScriptContainer)1 Argument (com.denizenscript.denizencore.objects.Argument)1 MapTag (com.denizenscript.denizencore.objects.core.MapTag)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1