From 0c39a7cbfdf360bb87f589188c394080fb0ee6d9 Mon Sep 17 00:00:00 2001 From: LexManos Date: Wed, 14 Mar 2012 16:47:15 -0700 Subject: [PATCH] New EntityInteract hook for handeling player vs entity interaction on the global level vs the item level. --- .../net/minecraft/src/forge/ForgeHooks.java | 13 ++++++++++ .../src/forge/IEntityInteractHandler.java | 24 ++++++++++++++++++ .../minecraft/src/forge/MinecraftForge.java | 9 +++++++ .../net/minecraft/src/EntityPlayer.java.patch | 25 +++++++++++++++---- .../net/minecraft/src/EntityPlayer.java.patch | 23 ++++++++++++++--- 5 files changed, 85 insertions(+), 9 deletions(-) create mode 100644 forge/forge_common/net/minecraft/src/forge/IEntityInteractHandler.java diff --git a/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java b/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java index 3ceaaa489..8492c11f1 100644 --- a/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java +++ b/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java @@ -183,6 +183,19 @@ public class ForgeHooks return true; } static LinkedList chunkLoadHandlers = new LinkedList(); + + public static boolean onEntityInteract(EntityPlayer player, Entity entity, boolean isAttack) + { + for (IEntityInteractHandler handler : entityInteractHandlers) + { + if (!handler.onEntityInteract(player, entity, isAttack)) + { + return false; + } + } + return true; + } + static LinkedList entityInteractHandlers = new LinkedList(); // Plant Management // ------------------------------------------------------------ diff --git a/forge/forge_common/net/minecraft/src/forge/IEntityInteractHandler.java b/forge/forge_common/net/minecraft/src/forge/IEntityInteractHandler.java new file mode 100644 index 000000000..b19e9d359 --- /dev/null +++ b/forge/forge_common/net/minecraft/src/forge/IEntityInteractHandler.java @@ -0,0 +1,24 @@ +/** + * This software is provided under the terms of the Minecraft Forge Public + * License v1.0. + */ + +package net.minecraft.src.forge; + +import net.minecraft.src.EntityPlayer; +import net.minecraft.src.Entity; + +public interface IEntityInteractHandler +{ + /** + * This is called before a player attacks, or interacts {left or right click by default} + * with another entity. Before any damage, or other interaction code is run. + * In multiplayer, this is called by both the client and the server. + * + * @param player The player doing the interacting + * @param entity The entity being interacted with + * @param isAttack True if it is a attack {left click} false if it is a interact {right click} + * @return True to continue processing, false to cancel. + */ + public boolean onEntityInteract(EntityPlayer player, Entity entity, boolean isAttack); +} \ No newline at end of file diff --git a/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java b/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java index 08fa7349e..9213e1b87 100755 --- a/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java +++ b/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java @@ -111,6 +111,15 @@ public class MinecraftForge ForgeHooks.pickupHandlers.add(handler); } + /** + * Register a new entity interact handler. + * @param handler The Handler to be registered + */ + public static void registerEntityInteractHandler(IEntityInteractHandler handler) + { + ForgeHooks.entityInteractHandlers.add(handler); + } + /** * This is not supposed to be called outside of Minecraft internals. */ diff --git a/forge/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch b/forge/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch index 04c36e1d4..4201b9c53 100644 --- a/forge/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch +++ b/forge/patches/minecraft/net/minecraft/src/EntityPlayer.java.patch @@ -73,7 +73,18 @@ par2 = this.applyPotionDamageCalculations(par1DamageSource, par2); this.addExhaustion(par1DamageSource.getHungerDamage()); this.health -= par2; -@@ -1034,7 +1058,9 @@ +@@ -1004,6 +1028,10 @@ + */ + public void useCurrentItemOnEntity(Entity par1Entity) + { ++ if (!ForgeHooks.onEntityInteract(this, par1Entity, false)) ++ { ++ return; ++ } + if (!par1Entity.interact(this)) + { + ItemStack var2 = this.getCurrentEquippedItem(); +@@ -1034,7 +1062,9 @@ */ public void destroyCurrentEquippedItem() { @@ -83,10 +94,14 @@ } /** -@@ -1063,6 +1089,11 @@ +@@ -1063,6 +1093,15 @@ */ public void attackTargetEntityWithCurrentItem(Entity par1Entity) { ++ if (!ForgeHooks.onEntityInteract(this, par1Entity, true)) ++ { ++ return; ++ } + ItemStack stack = getCurrentEquippedItem(); + if (stack != null && stack.getItem().onLeftClickEntity(stack, this, par1Entity)) + { @@ -95,7 +110,7 @@ if (par1Entity.canAttackWithItem()) { int var2 = this.inventory.getDamageVsEntity(par1Entity); -@@ -1205,6 +1236,12 @@ +@@ -1205,6 +1244,12 @@ */ public EnumStatus sleepInBedAt(int par1, int par2, int par3) { @@ -108,7 +123,7 @@ if (!this.worldObj.isRemote) { if (this.isPlayerSleeping() || !this.isEntityAlive()) -@@ -1687,6 +1724,7 @@ +@@ -1687,6 +1732,7 @@ return 101; } } @@ -116,7 +131,7 @@ } return var3; -@@ -1857,4 +1895,30 @@ +@@ -1857,4 +1903,30 @@ { return !this.capabilities.isFlying; } diff --git a/forge/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch b/forge/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch index 0380fd8d9..e27339573 100644 --- a/forge/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch +++ b/forge/patches/minecraft_server/net/minecraft/src/EntityPlayer.java.patch @@ -81,7 +81,18 @@ par2 = this.applyPotionDamageCalculations(par1DamageSource, par2); this.addExhaustion(par1DamageSource.getHungerDamage()); this.health -= par2; -@@ -960,7 +990,9 @@ +@@ -930,6 +960,10 @@ + */ + public void useCurrentItemOnEntity(Entity par1Entity) + { ++ if (!ForgeHooks.onEntityInteract(this, par1Entity, false)) ++ { ++ return; ++ } + if (!par1Entity.interact(this)) + { + ItemStack var2 = this.getCurrentEquippedItem(); +@@ -960,7 +994,9 @@ */ public void destroyCurrentEquippedItem() { @@ -91,10 +102,14 @@ } /** -@@ -989,6 +1021,12 @@ +@@ -989,6 +1025,16 @@ */ public void attackTargetEntityWithCurrentItem(Entity par1Entity) { ++ if (!ForgeHooks.onEntityInteract(this, par1Entity, true)) ++ { ++ return; ++ } + ItemStack stack = getCurrentEquippedItem(); + if (stack != null && stack.getItem().onLeftClickEntity(stack, this, par1Entity)) + { @@ -104,7 +119,7 @@ if (par1Entity.canAttackWithItem()) { int var2 = this.inventory.getDamageVsEntity(par1Entity); -@@ -1127,6 +1165,12 @@ +@@ -1127,6 +1173,12 @@ */ public EnumStatus sleepInBedAt(int par1, int par2, int par3) { @@ -117,7 +132,7 @@ if (!this.worldObj.isRemote) { if (this.isPlayerSleeping() || !this.isEntityAlive()) -@@ -1708,4 +1752,16 @@ +@@ -1708,4 +1760,16 @@ { return !this.capabilities.isFlying; }