diff --git a/src/main/java/ch/njol/skript/bukkitutil/block/BlockValues.java b/src/main/java/ch/njol/skript/bukkitutil/block/BlockValues.java index ff69fd60b2b..23c52442d82 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/block/BlockValues.java +++ b/src/main/java/ch/njol/skript/bukkitutil/block/BlockValues.java @@ -40,4 +40,6 @@ public abstract class BlockValues { @Override public abstract int hashCode(); -} + public abstract Object getData(); + +} diff --git a/src/main/java/ch/njol/skript/bukkitutil/block/MagicBlockCompat.java b/src/main/java/ch/njol/skript/bukkitutil/block/MagicBlockCompat.java index aefe9cf4f66..50261455b66 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/block/MagicBlockCompat.java +++ b/src/main/java/ch/njol/skript/bukkitutil/block/MagicBlockCompat.java @@ -72,7 +72,7 @@ public class MagicBlockCompat implements BlockCompat { private class MagicBlockValues extends BlockValues { private Material id; - short data; + private short data; private int itemFlags; @SuppressWarnings("null") @@ -133,6 +133,11 @@ public MatchQuality match(BlockValues other) { return MatchQuality.DIFFERENT; } } + + @Override + public Object getData() { + return data; + } } private static class MagicBlockSetter implements BlockSetter { diff --git a/src/main/java/ch/njol/skript/bukkitutil/block/NewBlockCompat.java b/src/main/java/ch/njol/skript/bukkitutil/block/NewBlockCompat.java index b75efb0d1b4..4671bfadddd 100644 --- a/src/main/java/ch/njol/skript/bukkitutil/block/NewBlockCompat.java +++ b/src/main/java/ch/njol/skript/bukkitutil/block/NewBlockCompat.java @@ -108,6 +108,11 @@ public MatchQuality match(BlockValues other) { } } + @Override + public Object getData() { + return data; + } + } private static class NewBlockSetter implements BlockSetter { diff --git a/src/main/java/ch/njol/skript/expressions/ExprDurability.java b/src/main/java/ch/njol/skript/expressions/ExprDurability.java index 45e35b6d124..831ea7e86ff 100644 --- a/src/main/java/ch/njol/skript/expressions/ExprDurability.java +++ b/src/main/java/ch/njol/skript/expressions/ExprDurability.java @@ -19,19 +19,20 @@ */ package ch.njol.skript.expressions; +import org.bukkit.block.data.BlockData; import org.bukkit.event.Event; import org.bukkit.inventory.ItemStack; import org.eclipse.jdt.annotation.Nullable; +import ch.njol.skript.Skript; import ch.njol.skript.aliases.ItemType; +import ch.njol.skript.bukkitutil.block.BlockValues; import ch.njol.skript.classes.Changer.ChangeMode; -import ch.njol.skript.classes.Changer.ChangerUtils; import ch.njol.skript.doc.Description; import ch.njol.skript.doc.Examples; import ch.njol.skript.doc.Name; import ch.njol.skript.doc.Since; import ch.njol.skript.expressions.base.SimplePropertyExpression; -import ch.njol.skript.util.slot.Slot; import ch.njol.util.coll.CollectionUtils; /** @@ -43,21 +44,25 @@ "but this expression can e.g. be used to \"add 1 to data of <item>\", e.g. for cycling through all wool colours."}) @Examples({"add 1 to the data value of the clicked block"}) @Since("1.2") -public class ExprDurability extends SimplePropertyExpression { +public class ExprDurability extends SimplePropertyExpression { static { - register(ExprDurability.class, Short.class, "((data|damage)[s] [value[s]]|durabilit(y|ies))", "itemstacks/slots"); + register(ExprDurability.class, Object.class, "((data|damage)[s] [value[s]]|durabilit(y|ies))", "itemtypes"); } + private final static boolean USING_NEW_BLOCK_COMPAT = Skript.isRunningMinecraft(1, 13); + + @SuppressWarnings("deprecation") @Override @Nullable - public Short convert(final Object o) { - if (o instanceof Slot) { - final ItemStack i = ((Slot) o).getItem(); - return i == null ? null : i.getDurability(); - } else { - return ((ItemStack) o).getDurability(); + public Object convert(final ItemType i) { + if (i.hasBlock()) { + BlockValues bv = i.getTypes().get(0).getBlockValues(); + if (bv == null) return null; + return USING_NEW_BLOCK_COMPAT ? ((BlockData) bv.getData()).getAsString() : bv.getData(); } + ItemStack stack = i.getRandom(); + return stack == null ? null : stack.getDurability(); } @Override @@ -66,8 +71,8 @@ public String getPropertyName() { } @Override - public Class getReturnType() { - return Short.class; + public Class getReturnType() { + return Object.class; } @Override @@ -75,44 +80,66 @@ public Class getReturnType() { public Class[] acceptChange(final ChangeMode mode) { if (mode == ChangeMode.REMOVE_ALL) return null; - if (Slot.class.isAssignableFrom(getExpr().getReturnType()) || getExpr().isSingle() && ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, ItemStack.class, ItemType.class)) - return CollectionUtils.array(Number.class); - return null; + return CollectionUtils.array(Object.class); } + @SuppressWarnings("deprecation") @Override public void change(final Event e, final @Nullable 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) { - final ItemStack i = o instanceof Slot ? ((Slot) o).getItem() : (ItemStack) o; - if (i == null) - continue; - switch (mode) { - case REMOVE: - a = -a; - //$FALL-THROUGH$ - case ADD: - i.setDurability((short) (i.getDurability() + a)); - break; - case SET: - i.setDurability((short) a); - break; - case DELETE: - case RESET: - a = 0; - i.setDurability((short) 0); - break; - case REMOVE_ALL: - assert false; + if (delta == null) return; + Object[] arr = getExpr().getArray(e); + ItemType[] newarr = new ItemType[arr.length]; + if (delta[0] instanceof String && USING_NEW_BLOCK_COMPAT) { + String a = (String) delta[0]; + assert a != null; + for (int i = 0; i < arr.length; i++) { + ItemType item = (ItemType) arr[i]; + ItemType newitem; + switch (mode) { + case SET: + newitem = new ItemType(item.getMaterial(), a); + newitem.setAmount(item.getAmount()); + break; + case DELETE: + case RESET: + newitem = new ItemType(item.getMaterial()); + newitem.setAmount(item.getAmount()); + break; + case REMOVE_ALL: + assert false; + default: + newitem = item; + } + newarr[i] = newitem; } - if (o instanceof Slot) - ((Slot) o).setItem(i); - else if (ChangerUtils.acceptsChange(getExpr(), ChangeMode.SET, ItemStack.class)) - getExpr().change(e, new ItemStack[] {i}, ChangeMode.SET); - else - getExpr().change(e, new ItemType[] {new ItemType(i)}, ChangeMode.SET); + } else if (delta[0] instanceof Number) { + int a = ((Number) delta[0]).intValue(); + for (int i = 0; i < arr.length; i++) { + ItemStack stack = ((ItemType) arr[i]).getRandom(); + assert stack != null; + switch (mode) { + case REMOVE: + stack.setDurability((short) (stack.getDurability() - a)); + break; + case ADD: + stack.setDurability((short) (stack.getDurability() + a)); + break; + case SET: + stack.setDurability((short) a); + break; + case DELETE: + case RESET: + stack.setDurability((short) 0); + break; + case REMOVE_ALL: + assert false; + } + newarr[i] = new ItemType(stack); + } + } else { + return; } + change(e, newarr, ChangeMode.SET); } }