Skip to content

Commit

Permalink
Several bugfixes for PlayerInteractEvent (#2943)
Browse files Browse the repository at this point in the history
* Don't change Result if setCanceled(false) called

* Fix RightClickEmpty firing even when targeting a block or entity

* Add a left click empty event

* Move hook point for EntityInteractSpecific to the right place

* Fix LeftClickBlock not firing clientside in creative, and clarify docs about creative
  • Loading branch information
williewillus authored and LexManos committed Jun 4, 2016
1 parent 92914f8 commit 35cd9fe
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
13 changes: 10 additions & 3 deletions patches/minecraft/net/minecraft/client/Minecraft.java.patch
Expand Up @@ -184,12 +184,19 @@
{
this.field_71442_b.func_180511_b(blockpos, this.field_71476_x.field_178784_b);
break;
@@ -1476,7 +1494,7 @@
@@ -1447,6 +1465,7 @@
}

this.field_71439_g.func_184821_cY();
+ net.minecraftforge.common.ForgeHooks.onEmptyLeftClick(this.field_71439_g, this.field_71439_g.func_184614_ca());
}

this.field_71439_g.func_184609_a(EnumHand.MAIN_HAND);
@@ -1476,7 +1495,6 @@
switch (this.field_71476_x.field_72313_a)
{
case ENTITY:
-
+ if(!net.minecraftforge.common.ForgeHooks.onInteractEntityAt(field_71439_g, field_71476_x.field_72308_g, field_71476_x, field_71439_g.func_184586_b(enumhand), enumhand))
if (this.field_71442_b.func_187102_a(this.field_71439_g, this.field_71476_x.field_72308_g, this.field_71476_x, this.field_71439_g.func_184586_b(enumhand), enumhand) == EnumActionResult.SUCCESS)
{
return;
Expand All @@ -198,7 +205,7 @@

ItemStack itemstack1 = this.field_71439_g.func_184586_b(enumhand);
-
+ if (itemstack1 == null) net.minecraftforge.common.ForgeHooks.onEmptyClick(this.field_71439_g, enumhand);
+ if (itemstack1 == null && (this.field_71476_x == null || this.field_71476_x.field_72313_a == RayTraceResult.Type.MISS)) net.minecraftforge.common.ForgeHooks.onEmptyClick(this.field_71439_g, enumhand);
if (itemstack1 != null && this.field_71442_b.func_187101_a(this.field_71439_g, this.field_71441_e, itemstack1, enumhand) == EnumActionResult.SUCCESS)
{
this.field_71460_t.field_78516_c.func_187460_a(enumhand);
Expand Down
Expand Up @@ -50,7 +50,15 @@
return flag;
}
}
@@ -218,14 +224,17 @@
@@ -207,6 +213,7 @@
if (this.field_78779_k.func_77145_d())
{
this.field_78774_b.func_147297_a(new CPacketPlayerDigging(CPacketPlayerDigging.Action.START_DESTROY_BLOCK, p_180511_1_, p_180511_2_));
+ if (!net.minecraftforge.common.ForgeHooks.onLeftClickBlock(this.field_78776_a.field_71439_g, p_180511_1_, p_180511_2_, net.minecraftforge.common.ForgeHooks.rayTraceEyeHitVec(this.field_78776_a.field_71439_g, func_78757_d() + 1)).isCanceled())
func_178891_a(this.field_78776_a, this, p_180511_1_, p_180511_2_);
this.field_78781_i = 5;
}
@@ -218,14 +225,17 @@
}

this.field_78774_b.func_147297_a(new CPacketPlayerDigging(CPacketPlayerDigging.Action.START_DESTROY_BLOCK, p_180511_1_, p_180511_2_));
Expand All @@ -69,7 +77,7 @@
if (flag && iblockstate.func_185903_a(this.field_78776_a.field_71439_g, this.field_78776_a.field_71439_g.field_70170_p, p_180511_1_) >= 1.0F)
{
this.func_187103_a(p_180511_1_);
@@ -372,13 +381,32 @@
@@ -372,13 +382,32 @@
}
else
{
Expand Down Expand Up @@ -104,7 +112,7 @@
}

if (!flag && p_187099_3_ != null && p_187099_3_.func_77973_b() instanceof ItemBlock)
@@ -394,7 +422,7 @@
@@ -394,7 +423,7 @@

this.field_78774_b.func_147297_a(new CPacketPlayerTryUseItemOnBlock(p_187099_4_, p_187099_5_, p_187099_7_, f, f1, f2));

Expand All @@ -113,7 +121,7 @@
{
if (p_187099_3_ == null)
{
@@ -412,14 +440,19 @@
@@ -412,14 +441,19 @@
{
int i = p_187099_3_.func_77960_j();
int j = p_187099_3_.field_77994_a;
Expand All @@ -134,15 +142,15 @@
}
}
else
@@ -446,6 +479,7 @@
@@ -446,6 +480,7 @@
}
else
{
+ if (net.minecraftforge.common.ForgeHooks.onItemRightClick(p_187101_1_, p_187101_4_, p_187101_3_)) return net.minecraft.util.EnumActionResult.PASS;
int i = p_187101_3_.field_77994_a;
ActionResult<ItemStack> actionresult = p_187101_3_.func_77957_a(p_187101_2_, p_187101_1_, p_187101_4_);
ItemStack itemstack = (ItemStack)actionresult.func_188398_b();
@@ -454,9 +488,10 @@
@@ -454,9 +489,10 @@
{
p_187101_1_.func_184611_a(p_187101_4_, itemstack);

Expand All @@ -154,3 +162,11 @@
}
}

@@ -494,6 +530,7 @@
this.func_78750_j();
Vec3d vec3d = new Vec3d(p_187102_3_.field_72307_f.field_72450_a - p_187102_2_.field_70165_t, p_187102_3_.field_72307_f.field_72448_b - p_187102_2_.field_70163_u, p_187102_3_.field_72307_f.field_72449_c - p_187102_2_.field_70161_v);
this.field_78774_b.func_147297_a(new CPacketUseEntity(p_187102_2_, p_187102_5_, vec3d));
+ if(net.minecraftforge.common.ForgeHooks.onInteractEntityAt(p_187102_1_, p_187102_2_, p_187102_3_, p_187102_1_.func_184586_b(p_187102_5_), p_187102_5_)) return EnumActionResult.PASS;
return this.field_78779_k == WorldSettings.GameType.SPECTATOR ? EnumActionResult.PASS : p_187102_2_.func_184199_a(p_187102_1_, vec3d, p_187102_4_, p_187102_5_);
}

5 changes: 5 additions & 0 deletions src/main/java/net/minecraftforge/common/ForgeHooks.java
Expand Up @@ -987,6 +987,11 @@ public static void onEmptyClick(EntityPlayer player, EnumHand hand)
MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent.RightClickEmpty(player, hand));
}

public static void onEmptyLeftClick(EntityPlayer player, ItemStack stack)
{
MinecraftForge.EVENT_BUS.post(new PlayerInteractEvent.LeftClickEmpty(player, stack));
}

private static ThreadLocal<Deque<LootTableContext>> lootContext = new ThreadLocal<Deque<LootTableContext>>();
private static LootTableContext getLootTableContext()
{
Expand Down
Expand Up @@ -182,8 +182,11 @@ public void setUseItem(Result triggerItem)
public void setCanceled(boolean canceled)
{
super.setCanceled(canceled);
useBlock = DENY;
useItem = DENY;
if (canceled)
{
useBlock = DENY;
useItem = DENY;
}
}
}

Expand Down Expand Up @@ -220,8 +223,12 @@ public RightClickEmpty(EntityPlayer player, EnumHand hand)
* This event controls which of {@link net.minecraft.block.Block#onBlockClicked} and/or the item harvesting methods will be called
* Canceling the event will cause none of the above noted methods to be called.
* There are various results to this event, see the getters below.
*
* Note that if the event is canceled and the player holds down left mouse, the event will continue to fire.
* This is due to how vanilla calls the left click handler methods.
*
* Also note that creative mode directly breaks the block without running any other logic.
* Therefore, in creative mode, {@link #setUseBlock} and {@link #setUseItem} have no effect.
*/
@Cancelable
public static class LeftClickBlock extends PlayerInteractEvent
Expand All @@ -245,15 +252,15 @@ public Vec3d getHitVec()
}

/**
* @return If {@link net.minecraft.block.Block#onBlockClicked} should be called
* @return If {@link net.minecraft.block.Block#onBlockClicked} should be called. Changing this has no effect in creative mode
*/
public Result getUseBlock()
{
return useBlock;
}

/**
* @return If the block should be attempted to be mined with the current item
* @return If the block should be attempted to be mined with the current item. Changing this has no effect in creative mode
*/
public Result getUseItem()
{
Expand All @@ -274,8 +281,24 @@ public void setUseItem(Result triggerItem)
public void setCanceled(boolean canceled)
{
super.setCanceled(canceled);
useBlock = DENY;
useItem = DENY;
if (canceled)
{
useBlock = DENY;
useItem = DENY;
}
}
}

/**
* This event is fired on the client side when the player left clicks empty space with any ItemStack.
* The server is not aware of when the client left clicks empty space, you will need to tell the server yourself.
* This event cannot be canceled.
*/
public static class LeftClickEmpty extends PlayerInteractEvent
{
public LeftClickEmpty(EntityPlayer player, ItemStack stack)
{
super(player, EnumHand.MAIN_HAND, stack, new BlockPos(player), null);
}
}

Expand Down

0 comments on commit 35cd9fe

Please sign in to comment.