Skip to content

Commit

Permalink
handle offhand in clicks block event
Browse files Browse the repository at this point in the history
  • Loading branch information
mergu committed Aug 13, 2017
1 parent 9baef1a commit ef872d5
Showing 1 changed file with 41 additions and 5 deletions.
Expand Up @@ -392,18 +392,21 @@ public void run() {
// <--[event]
// @Events
// player clicks block
// player (<click type>) clicks (<material>) (with <item>) (in <area>)
// player (<click type>) clicks block (with <item>)
// player (<click type>) clicks (<material>) (with <item>) (using hand/off_hand) (in <area>)
// player (<click type>) clicks block (with <item>) (using hand/off_hand)
// player stands on <pressure plate>
//
// @Regex ^on player (((([^\s]+ )?clicks [^\s]+( with [^\s]+)?( in [^\s]+)?))|(stands on [^\s]+))( in ((notable (cuboid|ellipsoid))|([^\s]+)))?$
//
// @Warning This event may fire twice in minecraft versions above 1.8.8 unless a hand is specified.
//
// @Triggers when a player clicks on a block or stands on a pressure plate.
// @Context
// <context.item> returns the dItem the player is clicking with.
// <context.location> returns the dLocation the player is clicking on.
// <context.cuboids> DEPRECATED.
// <context.click_type> returns an Element of the click type.
// <context.hand> returns an Element of the used hand.
// <context.relative> returns a dLocation of the air block in front of the clicked block.
//
// @Determine
Expand All @@ -418,14 +421,20 @@ public void playerInteract(PlayerInteractEvent event) {
return;
}

if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && event.getHand() == EquipmentSlot.OFF_HAND) {
return;
}
// Replaced with (using hand/off_hand) to detect previously unhandled interactions with the offhand
// if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && event.getHand() == EquipmentSlot.OFF_HAND) {
// return;
// }

Map<String, dObject> context = new HashMap<String, dObject>();
Action action = event.getAction();
dItem item = null;
dPlayer player = dEntity.getPlayerFrom(event.getPlayer());
Element hand = null;
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_9_R2) && event.getHand() != null) {
hand = new Element(event.getHand().name());
}


List<String> events = new ArrayList<String>();

Expand Down Expand Up @@ -453,6 +462,13 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
events.add(interaction);
}

if (hand != null) {
context.put("hand", hand);
for (String interaction : interactions) {
events.add(interaction + " using " + hand.identify());
}
}

if (event.hasItem()) {
item = new dItem(event.getItem());
context.put("item", item);
Expand All @@ -461,6 +477,11 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
events.add(interaction + " with item");
events.add(interaction + " with " + item.identifySimple());
events.add(interaction + " with " + item.identifyMaterial());
if (hand != null) {
events.add(interaction + " with item using " + hand.identify());
events.add(interaction + " with " + item.identifySimple() + " using " + hand.identify());
events.add(interaction + " with " + item.identifyMaterial() + " using " + hand.identify());
}
}
}

Expand All @@ -472,6 +493,10 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
for (String interaction : interactions) {
events.add(interaction + " block");
events.add(interaction + " " + blockMaterial.identifySimple());
if (hand != null) {
events.add(interaction + " block using " + hand.identify());
events.add(interaction + " " + blockMaterial.identifySimple() + " using " + hand.identify());
}
}

if (event.hasItem()) {
Expand All @@ -485,6 +510,17 @@ else if (action == Action.RIGHT_CLICK_AIR || action == Action.RIGHT_CLICK_BLOCK)
" with " + item.identifySimple());
events.add(interaction + " " + blockMaterial.identifySimple() +
" with " + item.identifyMaterial());
if (hand != null) {
events.add(interaction + " block with item using " + hand.identify());
events.add(interaction + " block with " + item.identifySimple() + " using " + hand.identify());
events.add(interaction + " block with " + item.identifyMaterial() + " using " + hand.identify());
events.add(interaction + " " + blockMaterial.identifySimple() +
" with item using " + hand.identify());
events.add(interaction + " " + blockMaterial.identifySimple() +
" with " + item.identifySimple() + " using " + hand.identify());
events.add(interaction + " " + blockMaterial.identifySimple() +
" with " + item.identifyMaterial() + " using " + hand.identify());
}
}
}

Expand Down

0 comments on commit ef872d5

Please sign in to comment.