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();
}
});
}
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) {
}
}
}
}
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;
}
}
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));
}
}
}
}
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]);
}
Aggregations