Skip to content

Commit

Permalink
Fix off hand items, block break events and some more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Mar 10, 2016
1 parent b5a4616 commit 703b81f
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 71 deletions.
13 changes: 10 additions & 3 deletions src/main/java/ch/njol/skript/events/EvtBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

package ch.njol.skript.events;

import java.lang.invoke.MethodHandle;

import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ItemFrame;
Expand Down Expand Up @@ -103,8 +105,8 @@ public boolean check(final Event e) {
}
if (types == null)
return true;
final int id;
final short durability;
int id = 0;
short durability = 0;
if (e instanceof BlockEvent) {
id = ((BlockEvent) e).getBlock().getTypeId();
durability = ((BlockEvent) e).getBlock().getData();
Expand Down Expand Up @@ -138,10 +140,15 @@ public boolean check(final @Nullable ItemType t) {
assert false;
return false;
}

//Hacky code to fix Java 8 compilation problems... TODO maybe use lambdas instead and break Java 7?
final int idFinal = id;
final short durFinal = durability;

return types.check(e, new Checker<ItemType>() {
@Override
public boolean check(final @Nullable ItemType t) {
return t != null && t.isOfType(id, durability);
return t != null && t.isOfType(idFinal, durFinal);
}
});
}
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/ch/njol/skript/events/ExprOffTool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
*
* Copyright 2011-2013 Peter Güttinger
*
*/

package ch.njol.skript.events;

import org.bukkit.entity.LivingEntity;
import org.bukkit.event.Event;
import org.bukkit.event.player.PlayerBucketEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.effects.Delay;
import ch.njol.skript.expressions.ExprTool;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.util.EquipmentSlot;
import ch.njol.skript.util.Getter;
import ch.njol.skript.util.InventorySlot;
import ch.njol.skript.util.Slot;

/**
* Offhand tool expression for Minecraft 1.9.
* @author bensku
*
*/
public class ExprOffTool extends ExprTool {
static {
if (Skript.isRunningMinecraft(1, 9)) {
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)",
"[the] (off[ ]hand tool|off[ ] hand item|shield[ item]) [of %livingentities%]", "%livingentities%'[s] (off[ ]hand tool|off[ ] hand item|shield[ item])");
} else { // Don't break scripts if running older Minecraft
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)",
"[the] (off[ ]hand tool|off[ ] hand item|shield[ item]) [of %livingentities%]", "%livingentities%'[s] (off[ ]hand tool|off[ ] hand item|shield[ item])");
}
}

@Override
protected Slot[] get(final Event e, final LivingEntity[] source) {
final boolean delayed = Delay.isDelayed(e);
return get(source, new Getter<Slot, LivingEntity>() {
@Override
@Nullable
public Slot get(final LivingEntity p) {
if (!delayed) {
if (e instanceof PlayerItemHeldEvent && ((PlayerItemHeldEvent) e).getPlayer() == p) {
Skript.info("ItemHeldEvent");
final PlayerInventory i = ((PlayerItemHeldEvent) e).getPlayer().getInventory();
assert i != null;
return new InventorySlot(i, getTime() >= 0 ? ((PlayerItemHeldEvent) e).getNewSlot() : ((PlayerItemHeldEvent) e).getPreviousSlot());
} else if (e instanceof PlayerBucketEvent && ((PlayerBucketEvent) e).getPlayer() == p) {
Skript.info("PlayerBucketEvent");
final PlayerInventory i = ((PlayerBucketEvent) e).getPlayer().getInventory();
assert i != null;
return new InventorySlot(i, ((PlayerBucketEvent) e).getPlayer().getInventory().getHeldItemSlot()) {
@Override
@Nullable
public ItemStack getItem() {
return getTime() <= 0 ? super.getItem() : ((PlayerBucketEvent) e).getItemStack();
}

@Override
public void setItem(final @Nullable ItemStack item) {
if (getTime() >= 0) {
((PlayerBucketEvent) e).setItemStack(item);
} else {
super.setItem(item);
}
}
};
}
}
final EntityEquipment e = p.getEquipment();
if (e == null)
return null;
return new EquipmentSlot(e, EquipmentSlot.EquipSlot.OFF_HAND) {
@Override
public String toString_i() {
return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + super.toString_i();
}
};
}
});
}
}
68 changes: 3 additions & 65 deletions src/main/java/ch/njol/skript/expressions/ExprTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,17 @@
@Examples({"player is holding a pickaxe",
"# is the same as",
"player's tool is a pickaxe",
"player's offhand tool is shield #Only for Minecraft 1.9"})
"player's off hand tool is shield #Only for Minecraft 1.9"})
@Since("1.0")
public class ExprTool extends PropertyExpression<LivingEntity, Slot> {
static {
Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item|weapon) [of %livingentities%]", "%livingentities%'[s] (tool|held item|weapon)",
"[the] (off[-]tool|off[-][held ]item|off[-]weapon) [of %livingentities%]", "%livingentities%'[s] (off[-]tool|off[-][held ]item|off[-]weapon)");
if (Skript.isRunningMinecraft(1, 9)) {
Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item|weapon) [of %livingentities%]", "%livingentities%'[s] (tool|held item|weapon)",
"[the] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon) [of %livingentities%]", "%livingentities%'[s] (off[(-| )]tool|off[(-| )][held ]item|off[(-| )]weapon)");
} else {
Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item|weapon) [of %livingentities%]", "%livingentities%'[s] (tool|held item|weapon)");
}
Skript.registerExpression(ExprTool.class, Slot.class, ExpressionType.PROPERTY, "[the] (tool|held item|weapon) [of %livingentities%]", "%livingentities%'[s] (tool|held item|weapon)");
}

boolean offTool; //Is this item offhand tools

@SuppressWarnings({"unchecked", "null"})
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
setExpr((Expression<Player>) exprs[0]);
offTool = matchedPattern >= 2;
return true;
}

Expand Down Expand Up @@ -118,7 +108,7 @@ public void setItem(final @Nullable ItemStack item) {
final EntityEquipment e = p.getEquipment();
if (e == null)
return null;
return new EquipmentSlot(e, offTool ? EquipmentSlot.EquipSlot.OFF_TOOL : EquipmentSlot.EquipSlot.TOOL) {
return new EquipmentSlot(e, EquipmentSlot.EquipSlot.TOOL) {
@Override
public String toString_i() {
return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + super.toString_i();
Expand Down Expand Up @@ -146,56 +136,4 @@ public boolean setTime(final int time) {
return super.setTime(time, getExpr(), PlayerItemHeldEvent.class, PlayerBucketFillEvent.class, PlayerBucketEmptyEvent.class);
}

/**
* Offhand tool expression for Minecraft 1.9.
* @author bensku
*
*/
public class OffTool extends ExprTool {
@Override
protected Slot[] get(final Event e, final LivingEntity[] source) {
final boolean delayed = Delay.isDelayed(e);
return get(source, new Getter<Slot, LivingEntity>() {
@Override
@Nullable
public Slot get(final LivingEntity p) {
if (!delayed) {
if (e instanceof PlayerItemHeldEvent && ((PlayerItemHeldEvent) e).getPlayer() == p) {
final PlayerInventory i = ((PlayerItemHeldEvent) e).getPlayer().getInventory();
assert i != null;
return new InventorySlot(i, getTime() >= 0 ? ((PlayerItemHeldEvent) e).getNewSlot() : ((PlayerItemHeldEvent) e).getPreviousSlot());
} else if (e instanceof PlayerBucketEvent && ((PlayerBucketEvent) e).getPlayer() == p) {
final PlayerInventory i = ((PlayerBucketEvent) e).getPlayer().getInventory();
assert i != null;
return new InventorySlot(i, ((PlayerBucketEvent) e).getPlayer().getInventory().getHeldItemSlot()) {
@Override
@Nullable
public ItemStack getItem() {
return getTime() <= 0 ? super.getItem() : ((PlayerBucketEvent) e).getItemStack();
}

@Override
public void setItem(final @Nullable ItemStack item) {
if (getTime() >= 0) {
((PlayerBucketEvent) e).setItemStack(item);
} else {
super.setItem(item);
}
}
};
}
}
final EntityEquipment e = p.getEquipment();
if (e == null)
return null;
return new EquipmentSlot(e, EquipmentSlot.EquipSlot.OFF_HAND) {
@Override
public String toString_i() {
return "the " + (getTime() == 1 ? "future " : getTime() == -1 ? "former " : "") + super.toString_i();
}
};
}
});
}
}
}
6 changes: 3 additions & 3 deletions src/main/java/ch/njol/skript/util/EquipmentSlot.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public void set(final EntityEquipment e, final @Nullable ItemStack item) {
}
}
},
OFF_HAND { // Since Minecraft 1.9 (defaults to main hand if earlier version)
OFF_HAND { // Since Minecraft 1.9 (defaults to empty if earlier version)

@Override
@Nullable
public ItemStack get(EntityEquipment e) {
if (Skript.isRunningMinecraft(1, 9)) {
return e.getItemInOffHand();
}
Skript.warning("No off hand support, but skript would need that!");
Skript.warning("No off hand support, but a skript would need that!");
return new ItemStack(Material.AIR);
}

Expand All @@ -81,7 +81,7 @@ public void set(EntityEquipment e, @Nullable ItemStack item) {
if (Skript.isRunningMinecraft(1, 9)) {
e.setItemInOffHand(item);
} else {
Skript.warning("No off hand support, but skript would need that!");
Skript.warning("No off hand support, but a skript would need that!");
}
}

Expand Down

0 comments on commit 703b81f

Please sign in to comment.