|
| 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.events; |
| 23 | + |
| 24 | +import org.bukkit.entity.LivingEntity; |
| 25 | +import org.bukkit.event.Event; |
| 26 | +import org.bukkit.event.player.PlayerBucketEvent; |
| 27 | +import org.bukkit.event.player.PlayerItemHeldEvent; |
| 28 | +import org.bukkit.inventory.EntityEquipment; |
| 29 | +import org.bukkit.inventory.ItemStack; |
| 30 | +import org.bukkit.inventory.PlayerInventory; |
| 31 | +import org.eclipse.jdt.annotation.Nullable; |
| 32 | + |
| 33 | +import ch.njol.skript.Skript; |
| 34 | +import ch.njol.skript.effects.Delay; |
| 35 | +import ch.njol.skript.expressions.ExprTool; |
| 36 | +import ch.njol.skript.lang.ExpressionType; |
| 37 | +import ch.njol.skript.util.EquipmentSlot; |
| 38 | +import ch.njol.skript.util.Getter; |
| 39 | +import ch.njol.skript.util.InventorySlot; |
| 40 | +import ch.njol.skript.util.Slot; |
| 41 | + |
| 42 | +/** |
| 43 | + * Offhand tool expression for Minecraft 1.9. |
| 44 | + * @author bensku |
| 45 | + * |
| 46 | + */ |
| 47 | +public class ExprOffTool extends ExprTool { |
| 48 | + static { |
| 49 | + if (Skript.isRunningMinecraft(1, 9)) { |
| 50 | + Skript.registerExpression(ExprOffTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon) [of %livingentities%]", "%livingentities%'[s] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon)", |
| 51 | + "[the] (off[ ]hand tool|off[ ] hand item|shield[ item]) [of %livingentities%]", "%livingentities%'[s] (off[ ]hand tool|off[ ] hand item|shield[ item])"); |
| 52 | + } else { // Don't break scripts if running older Minecraft |
| 53 | + Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon) [of %livingentities%]", "%livingentities%'[s] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon)", |
| 54 | + "[the] (off[ ]hand tool|off[ ] hand item|shield[ item]) [of %livingentities%]", "%livingentities%'[s] (off[ ]hand tool|off[ ] hand item|shield[ item])"); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Slot[] get(final Event e, final LivingEntity[] source) { |
| 60 | + final boolean delayed = Delay.isDelayed(e); |
| 61 | + return get(source, new Getter<Slot, LivingEntity>() { |
| 62 | + @Override |
| 63 | + @Nullable |
| 64 | + public Slot get(final LivingEntity p) { |
| 65 | + if (!delayed) { |
| 66 | + if (e instanceof PlayerItemHeldEvent && ((PlayerItemHeldEvent) e).getPlayer() == p) { |
| 67 | + Skript.info("ItemHeldEvent"); |
| 68 | + final PlayerInventory i = ((PlayerItemHeldEvent) e).getPlayer().getInventory(); |
| 69 | + assert i != null; |
| 70 | + return new InventorySlot(i, getTime() >= 0 ? ((PlayerItemHeldEvent) e).getNewSlot() : ((PlayerItemHeldEvent) e).getPreviousSlot()); |
| 71 | + } else if (e instanceof PlayerBucketEvent && ((PlayerBucketEvent) e).getPlayer() == p) { |
| 72 | + Skript.info("PlayerBucketEvent"); |
| 73 | + final PlayerInventory i = ((PlayerBucketEvent) e).getPlayer().getInventory(); |
| 74 | + assert i != null; |
| 75 | + return new InventorySlot(i, ((PlayerBucketEvent) e).getPlayer().getInventory().getHeldItemSlot()) { |
| 76 | + @Override |
| 77 | + @Nullable |
| 78 | + public ItemStack getItem() { |
| 79 | + return getTime() <= 0 ? super.getItem() : ((PlayerBucketEvent) e).getItemStack(); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public void setItem(final @Nullable ItemStack item) { |
| 84 | + if (getTime() >= 0) { |
| 85 | + ((PlayerBucketEvent) e).setItemStack(item); |
| 86 | + } else { |
| 87 | + super.setItem(item); |
| 88 | + } |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | + } |
| 93 | + final EntityEquipment e = p.getEquipment(); |
| 94 | + if (e == null) |
| 95 | + return null; |
| 96 | + return new EquipmentSlot(e, EquipmentSlot.EquipSlot.OFF_HAND) { |
| 97 | + @Override |
| 98 | + public String toString_i() { |
| 99 | + return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + super.toString_i(); |
| 100 | + } |
| 101 | + }; |
| 102 | + } |
| 103 | + }); |
| 104 | + } |
| 105 | +} |
0 commit comments