|
| 1 | +/* |
| 2 | + * This file is part of Skript. |
| 3 | + * |
| 4 | + * Skript is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * Skript is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with Skript. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * |
| 18 | + * Copyright 2011-2013 Peter Güttinger |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +package ch.njol.skript.expressions; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | + |
| 26 | +import org.bukkit.Location; |
| 27 | +import org.bukkit.block.Biome; |
| 28 | +import org.bukkit.event.Event; |
| 29 | +import org.bukkit.inventory.ItemStack; |
| 30 | +import org.bukkit.inventory.meta.ItemMeta; |
| 31 | +import org.eclipse.jdt.annotation.Nullable; |
| 32 | + |
| 33 | +import ch.njol.skript.Skript; |
| 34 | +import ch.njol.skript.aliases.ItemType; |
| 35 | +import ch.njol.skript.classes.Converter; |
| 36 | +import ch.njol.skript.doc.Description; |
| 37 | +import ch.njol.skript.doc.Examples; |
| 38 | +import ch.njol.skript.doc.Name; |
| 39 | +import ch.njol.skript.doc.Since; |
| 40 | +import ch.njol.skript.expressions.base.PropertyExpression; |
| 41 | +import ch.njol.skript.lang.Expression; |
| 42 | +import ch.njol.skript.lang.ExpressionType; |
| 43 | +import ch.njol.skript.lang.SkriptParser.ParseResult; |
| 44 | +import ch.njol.skript.util.Direction; |
| 45 | +import ch.njol.util.Kleenean; |
| 46 | + |
| 47 | +/** |
| 48 | + * @author bensku |
| 49 | + */ |
| 50 | +@Name("Unbreakable Items") |
| 51 | +@Description("Creates unbreakable copies of given items.") |
| 52 | +@Examples("unbreakable iron sword #Creates unbreakable iron sword") |
| 53 | +@Since("2.2-dev13b") |
| 54 | +public class ExprUnbreakable extends PropertyExpression<ItemType, ItemType> { |
| 55 | + |
| 56 | + static { |
| 57 | + Skript.registerExpression(ExprUnbreakable.class, ItemType.class, ExpressionType.PROPERTY, "unbreakable %itemtypes%"); |
| 58 | + } |
| 59 | + |
| 60 | + @SuppressWarnings({"unchecked", "null"}) |
| 61 | + @Override |
| 62 | + public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 63 | + setExpr((Expression<? extends ItemType>) exprs[0]); |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected ItemType[] get(final Event e, final ItemType[] source) { |
| 69 | + return get(source, new Converter<ItemType, ItemType>() { |
| 70 | + @Override |
| 71 | + public ItemType convert(final ItemType i) { |
| 72 | + ItemType clone = i.clone(); |
| 73 | + |
| 74 | + Object meta = clone.getItemMeta(); |
| 75 | + if (meta == null) { |
| 76 | + ItemStack random = clone.getRandom(); // Should not happen, but... |
| 77 | + if (random == null) |
| 78 | + return clone; |
| 79 | + meta = random.getItemMeta(); |
| 80 | + } |
| 81 | + if (!(meta instanceof ItemMeta)) { |
| 82 | + Skript.error("Unknown item meta type, can't make item unbreakable!"); |
| 83 | + return clone; |
| 84 | + } |
| 85 | + |
| 86 | + ((ItemMeta) meta).spigot().setUnbreakable(true); |
| 87 | + clone.setItemMeta(meta); |
| 88 | + |
| 89 | + return clone; |
| 90 | + } |
| 91 | + }); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Class<? extends ItemType> getReturnType() { |
| 96 | + return ItemType.class; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public String toString(@Nullable Event e, boolean debug) { |
| 101 | + if (e == null) |
| 102 | + return "unbreakable items"; |
| 103 | + return "unbreakable " + Arrays.toString(getExpr().getAll(e)); |
| 104 | + } |
| 105 | +} |
0 commit comments