Skip to content

Commit

Permalink
Fix click event with holding not checking both hands (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Feb 18, 2017
1 parent 36572f4 commit f127328
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions src/main/java/ch/njol/skript/events/EvtClick.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
Expand All @@ -59,25 +60,30 @@
@SuppressWarnings("unchecked")
public class EvtClick extends SkriptEvent {

// Important: a click on an entity fires both an PlayerInteractEntityEvent and a PlayerInteractEvent
// TheBukor: It only fires a PlayerInteractEntityEvent in 1.9, I guess.

private final static boolean twoHanded = Skript.isRunningMinecraft(1, 9);
final static boolean twoHanded = Skript.isRunningMinecraft(1, 9);

/**
* Click types.
*/
private final static int RIGHT = 1, LEFT = 2, ANY = RIGHT | LEFT;

/**
* If we used "holding" somewhere there, we must check if either hand
* contains the tool.
*/
private final static int HOLDING = 4;

static {
Class<? extends PlayerEvent> clickEvent;
if (twoHanded) // Armor stand support!
clickEvent = PlayerInteractAtEntityEvent.class;
else
clickEvent = PlayerInteractEntityEvent.class;

@SuppressWarnings("unchecked")
Class<? extends PlayerEvent>[] eventTypes = CollectionUtils.array(PlayerInteractEvent.class, clickEvent);
Skript.registerEvent("Click", EvtClick.class, eventTypes,
"[(" + RIGHT + "¦right|" + LEFT + "¦left)(| |-)][mouse(| |-)]click[ing] [on %-entitydata/itemtype%] [(with|using|holding) %itemtype%]",
"[(" + RIGHT + "¦right|" + LEFT + "¦left)(| |-)][mouse(| |-)]click[ing] (with|using|holding) %itemtype% on %entitydata/itemtype%")
"[(" + RIGHT + "¦right|" + LEFT + "¦left)(| |-)][mouse(| |-)]click[ing] [on %-entitydata/itemtype%] [(with|using|" + HOLDING + holding) %itemtype%]",
"[(" + RIGHT + "¦right|" + LEFT + "¦left)(| |-)][mouse(| |-)]click[ing] (with|using|" + HOLDING + holding) %itemtype% on %entitydata/itemtype%")
.description("Called when a user clicks on a block, an entity or air with or without an item in their hand.",
"Please note that rightclick events with an empty hand while not looking at a block are not sent to the server, so there's no way to detect them.")
.examples("on click",
Expand All @@ -94,10 +100,11 @@ public class EvtClick extends SkriptEvent {
private Literal<ItemType> tools;

private int click = ANY;
boolean isHolding = false;

@Override
public boolean init(final Literal<?>[] args, final int matchedPattern, final ParseResult parser) {
//Skript.info("matchedPattern is " + matchedPattern); // TODO there is something wrong here...
//Skript.info("matchedPattern is " + matchedPattern);
//Skript.info("args is " + Arrays.toString(args));
click = parser.mark == 0 ? ANY : parser.mark;
types = args[matchedPattern];
Expand All @@ -110,6 +117,7 @@ public boolean init(final Literal<?>[] args, final int matchedPattern, final Par
}
}
tools = (Literal<ItemType>) args[1 - matchedPattern];
isHolding = (parser.mark & HOLDING) != 0; // Check if third-least significant byte is 1
return true;
}

Expand Down Expand Up @@ -181,7 +189,12 @@ public boolean check(final Event e) {
if (e instanceof PlayerInteractEvent && tools != null && !tools.check(e, new Checker<ItemType>() {
@Override
public boolean check(final ItemType t) {
return t.isOfType(((PlayerInteractEvent) e).getItem());
if (isHolding && twoHanded) {
PlayerInventory invi = ((PlayerInteractEvent) e).getPlayer().getInventory();
return t.isOfType(invi.getItemInMainHand()) || t.isOfType(invi.getItemInOffHand());
} else {
return t.isOfType(((PlayerInteractEvent) e).getItem());
}
}
})) {
return false;
Expand Down

0 comments on commit f127328

Please sign in to comment.