Search in sources :

Example 1 with ItemType

use of ch.njol.skript.aliases.ItemType in project Skript by SkriptLang.

the class ExprItems method iterator.

@Override
@Nullable
public Iterator<ItemStack> iterator(final Event e) {
    Iterator<ItemStack> iter;
    if (types == null) {
        iter = new Iterator<ItemStack>() {

            private final Iterator<Material> iter = new ArrayIterator<>(Material.values());

            @Override
            public boolean hasNext() {
                return iter.hasNext();
            }

            @Override
            public ItemStack next() {
                return new ItemStack(iter.next());
            }

            @Override
            public void remove() {
            }
        };
    } else {
        @SuppressWarnings("null") final Iterator<ItemType> it = new ArrayIterator<>(types.getArray(e));
        if (!it.hasNext())
            return null;
        iter = new Iterator<ItemStack>() {

            @SuppressWarnings("null")
            Iterator<ItemStack> current = it.next().getAll().iterator();

            @SuppressWarnings("null")
            @Override
            public boolean hasNext() {
                while (!current.hasNext() && it.hasNext()) {
                    current = it.next().getAll().iterator();
                }
                return current.hasNext();
            }

            @SuppressWarnings("null")
            @Override
            public ItemStack next() {
                if (!hasNext())
                    throw new NoSuchElementException();
                return current.next();
            }

            @Override
            public void remove() {
            }
        };
    }
    if (!blocks)
        return iter;
    return new CheckedIterator<>(iter, new NullableChecker<ItemStack>() {

        @Override
        public boolean check(@Nullable final ItemStack is) {
            return is != null && is.getType().isBlock();
        }
    });
}
Also used : ItemType(ch.njol.skript.aliases.ItemType) CheckedIterator(ch.njol.util.coll.iterator.CheckedIterator) Material(org.bukkit.Material) ItemStack(org.bukkit.inventory.ItemStack) ArrayIterator(ch.njol.util.coll.iterator.ArrayIterator) NoSuchElementException(java.util.NoSuchElementException) Nullable(org.eclipse.jdt.annotation.Nullable)

Example 2 with ItemType

use of ch.njol.skript.aliases.ItemType in project Skript by SkriptLang.

the class ExprDurability method change.

@SuppressWarnings("null")
@Override
public void change(final Event e, @Nullable final Object[] delta, final ChangeMode mode) {
    int a = delta == null ? 0 : ((Number) delta[0]).intValue();
    final Object[] os = getExpr().getArray(e);
    for (final Object o : os) {
        ItemStack itemStack = null;
        Block block = null;
        if (o instanceof ItemType)
            itemStack = ((ItemType) o).getRandom();
        else if (o instanceof Slot)
            itemStack = ((Slot) o).getItem();
        else if (LEGACY_BLOCK)
            block = (Block) o;
        else
            return;
        int changeValue = itemStack != null ? ItemUtils.getDamage(itemStack) : block != null ? block.getData() : 0;
        switch(mode) {
            case REMOVE:
                a = -a;
            // $FALL-THROUGH$
            case ADD:
                changeValue += a;
                break;
            case SET:
                changeValue = a;
                break;
            case DELETE:
            case RESET:
                changeValue = 0;
                break;
            case REMOVE_ALL:
                assert false;
        }
        if (o instanceof ItemType && itemStack != null) {
            ItemUtils.setDamage(itemStack, changeValue);
            ((ItemType) o).setTo(new ItemType(itemStack));
        } else if (o instanceof Slot) {
            ItemUtils.setDamage(itemStack, changeValue);
            ((Slot) o).setItem(itemStack);
        } else {
            BlockState blockState = ((Block) o).getState();
            try {
                blockState.setRawData((byte) Math.max(0, changeValue));
                blockState.update();
            }// Catch when a user sets the amount too high
             catch (IllegalArgumentException | NullPointerException ignore) {
            }
        }
    }
}
Also used : BlockState(org.bukkit.block.BlockState) ItemType(ch.njol.skript.aliases.ItemType) Block(org.bukkit.block.Block) Slot(ch.njol.skript.util.slot.Slot) ItemStack(org.bukkit.inventory.ItemStack)

Example 3 with ItemType

use of ch.njol.skript.aliases.ItemType in project Skript by SkriptLang.

the class ExprEnchantItem method change.

@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
    if (delta == null)
        return;
    ItemType item = ((ItemType) delta[0]);
    switch(mode) {
        case SET:
            if (event instanceof PrepareItemEnchantEvent) {
                PrepareItemEnchantEvent e = (PrepareItemEnchantEvent) event;
                e.getItem().setType(item.getMaterial());
                e.getItem().setItemMeta(item.getItemMeta());
                e.getItem().setAmount(item.getAmount());
            } else {
                EnchantItemEvent e = (EnchantItemEvent) event;
                e.getItem().setType(item.getMaterial());
                e.getItem().setItemMeta(item.getItemMeta());
                e.getItem().setAmount(item.getAmount());
            }
            break;
        case ADD:
        case REMOVE:
        case RESET:
        case DELETE:
        case REMOVE_ALL:
            assert false;
    }
}
Also used : EnchantItemEvent(org.bukkit.event.enchantment.EnchantItemEvent) ItemType(ch.njol.skript.aliases.ItemType) PrepareItemEnchantEvent(org.bukkit.event.enchantment.PrepareItemEnchantEvent)

Example 4 with ItemType

use of ch.njol.skript.aliases.ItemType in project Skript by SkriptLang.

the class ExprEnchantmentLevel method change.

@Override
public void change(Event e, @Nullable Object[] delta, ChangeMode mode) {
    ItemType[] itemTypes = items.getArray(e);
    Enchantment[] enchantments = enchants.getArray(e);
    int changeValue = ((Number) delta[0]).intValue();
    for (ItemType itemType : itemTypes) {
        for (Enchantment enchantment : enchantments) {
            EnchantmentType enchantmentType = itemType.getEnchantmentType(enchantment);
            int oldLevel = enchantmentType == null ? 0 : enchantmentType.getLevel();
            int newItemLevel;
            switch(mode) {
                case ADD:
                    newItemLevel = oldLevel + changeValue;
                    break;
                case REMOVE:
                    newItemLevel = oldLevel - changeValue;
                    break;
                case SET:
                    newItemLevel = changeValue;
                    break;
                default:
                    assert false;
                    return;
            }
            if (newItemLevel <= 0) {
                itemType.removeEnchantments(new EnchantmentType(enchantment));
            } else {
                itemType.addEnchantments(new EnchantmentType(enchantment, newItemLevel));
            }
        }
    }
}
Also used : EnchantmentType(ch.njol.skript.util.EnchantmentType) ItemType(ch.njol.skript.aliases.ItemType) Enchantment(org.bukkit.enchantments.Enchantment)

Example 5 with ItemType

use of ch.njol.skript.aliases.ItemType in project Skript by SkriptLang.

the class ExprEnchantments method get.

@Override
@Nullable
protected EnchantmentType[] get(Event e) {
    List<EnchantmentType> enchantments = new ArrayList<>();
    for (ItemType item : items.getArray(e)) {
        EnchantmentType[] enchants = item.getEnchantmentTypes();
        if (enchants == null)
            continue;
        Collections.addAll(enchantments, enchants);
    }
    return enchantments.toArray(new EnchantmentType[0]);
}
Also used : EnchantmentType(ch.njol.skript.util.EnchantmentType) ItemType(ch.njol.skript.aliases.ItemType) ArrayList(java.util.ArrayList) Nullable(org.eclipse.jdt.annotation.Nullable)

Aggregations

ItemType (ch.njol.skript.aliases.ItemType)97 ItemStack (org.bukkit.inventory.ItemStack)52 Nullable (org.eclipse.jdt.annotation.Nullable)17 ItemMeta (org.bukkit.inventory.meta.ItemMeta)16 Block (org.bukkit.block.Block)12 Material (org.bukkit.Material)10 Inventory (org.bukkit.inventory.Inventory)10 NamespacedKey (org.bukkit.NamespacedKey)8 BlockState (org.bukkit.block.BlockState)7 ItemData (ch.njol.skript.aliases.ItemData)6 Slot (ch.njol.skript.util.slot.Slot)6 Item (org.bukkit.entity.Item)6 MaterialData (org.bukkit.material.MaterialData)6 Literal (ch.njol.skript.lang.Literal)5 EnchantmentType (ch.njol.skript.util.EnchantmentType)5 ArrayList (java.util.ArrayList)5 TileState (org.bukkit.block.TileState)5 EntityData (ch.njol.skript.entity.EntityData)4 BlockData (org.bukkit.block.data.BlockData)4 Enchantment (org.bukkit.enchantments.Enchantment)4