Skip to content

Commit

Permalink
When clicking usable block, main hand is used in the event
Browse files Browse the repository at this point in the history
  • Loading branch information
bensku committed Mar 17, 2016
1 parent 49c7a9b commit 69e0d75
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/main/java/ch/njol/skript/events/EvtClick.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ public boolean check(final Event e) {
ItemStack offHand = clickEvent.getPlayer().getInventory().getItemInOffHand();

Player player = clickEvent.getPlayer();
@SuppressWarnings("null")
boolean useOffHand = checkOffHandUse(mainHand, offHand, click, player);
assert player != null;
boolean useOffHand = checkOffHandUse(mainHand, offHand, click, player, null);
if ((useOffHand && clickEvent.getHand() == EquipmentSlot.HAND) || (!useOffHand && clickEvent.getHand() == EquipmentSlot.OFF_HAND)) {
return false;
}
Expand All @@ -126,8 +126,8 @@ public boolean check(final Event e) {
ItemStack offHand = clickEvent.getPlayer().getInventory().getItemInOffHand();

Player player = clickEvent.getPlayer();
@SuppressWarnings("null")
boolean useOffHand = checkOffHandUse(mainHand, offHand, click, player);
assert player != null;
boolean useOffHand = checkOffHandUse(mainHand, offHand, click, player, clickEvent.getClickedBlock());
if ((useOffHand && clickEvent.getHand() == EquipmentSlot.HAND) || (!useOffHand && clickEvent.getHand() == EquipmentSlot.OFF_HAND)) {
return false;
}
Expand Down Expand Up @@ -186,10 +186,12 @@ public String toString(final @Nullable Event e, final boolean debug) {
return (click == LEFT ? "left" : click == RIGHT ? "right" : "") + "click" + (types != null ? " on " + types.toString(e, debug) : "") + (tools != null ? " holding " + tools.toString(e, debug) : "");
}

private boolean checkOffHandUse(ItemStack mainHand, ItemStack offHand, int clickType, Player player) {
private boolean checkOffHandUse(@Nullable ItemStack mainHand, @Nullable ItemStack offHand, int clickType, Player player, @Nullable Block target) {
boolean mainUsable = false;
boolean offUsable = false;

if (mainHand == null) mainHand = new ItemStack(Material.AIR);

if (clickType == RIGHT) {
if (offHand == null || offHand.getType() == Material.AIR) return false;
switch (offHand.getType()) {
Expand Down Expand Up @@ -225,7 +227,6 @@ private boolean checkOffHandUse(ItemStack mainHand, ItemStack offHand, int click
offUsable = true;
}

if (mainHand == null || mainHand.getType() == Material.AIR) return true;
switch (mainHand.getType()) {
case BOW:
case EGG:
Expand Down Expand Up @@ -256,6 +257,44 @@ private boolean checkOffHandUse(ItemStack mainHand, ItemStack offHand, int click

if (mainHand.getType().isBlock() || mainHand.getType().isEdible()) {
mainUsable = true;
} else if (target != null) {
switch (target.getType()) {
case ANVIL:
case BEACON:
case BED:
case BREWING_STAND:
case CAKE:
case CAULDRON:
case CHEST:
case TRAPPED_CHEST:
case ENDER_CHEST:
case WORKBENCH:
case ENCHANTMENT_TABLE:
case FURNACE:
case WOOD_DOOR:
case ACACIA_DOOR:
case JUNGLE_DOOR:
case DARK_OAK_DOOR:
case SPRUCE_DOOR:
case BIRCH_DOOR:
case IRON_DOOR:
case TRAP_DOOR:
case IRON_TRAPDOOR:
case FENCE_GATE:
case ACACIA_FENCE_GATE:
case JUNGLE_FENCE_GATE:
case DARK_OAK_FENCE_GATE:
case SPRUCE_FENCE_GATE:
case BIRCH_FENCE_GATE:
case HOPPER:
case DISPENSER:
case DROPPER:
case LEVER:
case WOOD_BUTTON:
case STONE_BUTTON:
case COMMAND:
if (!player.isSneaking()) mainUsable = true;
}
}
}

Expand Down

0 comments on commit 69e0d75

Please sign in to comment.