New EntityInteract hook for handeling player vs entity interaction on the global level vs the item level.

This commit is contained in:
LexManos 2012-03-14 16:47:15 -07:00
parent 09725ae326
commit 0c39a7cbfd
5 changed files with 85 additions and 9 deletions

View File

@ -183,6 +183,19 @@ public class ForgeHooks
return true;
}
static LinkedList<IChunkLoadHandler> chunkLoadHandlers = new LinkedList<IChunkLoadHandler>();
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<IEntityInteractHandler> entityInteractHandlers = new LinkedList<IEntityInteractHandler>();
// Plant Management
// ------------------------------------------------------------

View File

@ -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);
}

View File

@ -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.
*/

View File

@ -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;
}

View File

@ -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;
}