Implmented Sengir's IPickupHandler.
http://www.mod-buildcraft.com/forums/topic/hook-intercept-item-pickups-by-player-entities/?view=all
This commit is contained in:
parent
408d2a9d62
commit
14ccf7834b
5 changed files with 111 additions and 0 deletions
|
@ -7,6 +7,7 @@ package net.minecraft.src.forge;
|
|||
|
||||
import net.minecraft.src.Block;
|
||||
import net.minecraft.src.Entity;
|
||||
import net.minecraft.src.EntityItem;
|
||||
import net.minecraft.src.EntityMinecart;
|
||||
import net.minecraft.src.EntityPlayer;
|
||||
import net.minecraft.src.IInventory;
|
||||
|
@ -137,6 +138,21 @@ public class ForgeHooks {
|
|||
}
|
||||
static LinkedList<IConnectionHandler> connectionHandlers = new LinkedList<IConnectionHandler>();
|
||||
|
||||
public static boolean onItemPickup(EntityPlayer player, EntityItem item)
|
||||
{
|
||||
boolean cont = true;
|
||||
for (IPickupHandler handler : pickupHandlers)
|
||||
{
|
||||
cont = cont && handler.onItemPickup(player, item);
|
||||
if (!cont || item.item.stackSize <= 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return cont;
|
||||
}
|
||||
static LinkedList<IPickupHandler> pickupHandlers = new LinkedList<IPickupHandler>();
|
||||
|
||||
// Plant Management
|
||||
// ------------------------------------------------------------
|
||||
static class ProbableItem {
|
||||
|
|
|
@ -0,0 +1,26 @@
|
|||
package net.minecraft.src.forge;
|
||||
|
||||
import net.minecraft.src.EntityItem;
|
||||
import net.minecraft.src.EntityPlayer;
|
||||
import net.minecraft.src.ItemStack;
|
||||
|
||||
public interface IPickupHandler
|
||||
{
|
||||
|
||||
/**
|
||||
* Raised when a player collides with a EntityItem.
|
||||
* The handler may consume all, or part of the stack.
|
||||
* The handler will only be called if the stack size is > 0
|
||||
* The event may be cut part way through if the stack size
|
||||
* falls to 0 or a previous handler returns false;
|
||||
* Will only be called if delay before pickup is 0.
|
||||
*
|
||||
* The Entity will destroyed if the stack size falls to 0.
|
||||
*
|
||||
* @param player Player that picked up the item
|
||||
* @param item Item picked up as entity. May be manipulated
|
||||
* @return True If processing should continue.
|
||||
*/
|
||||
public boolean onItemPickup(EntityPlayer player, EntityItem item);
|
||||
|
||||
}
|
|
@ -92,6 +92,15 @@ public class MinecraftForge {
|
|||
ForgeHooks.connectionHandlers.add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a new Item Pickup event handler
|
||||
* @param handler The Handler to be registered
|
||||
*/
|
||||
public static void registerPickupHandler(IPickupHandler handler)
|
||||
{
|
||||
ForgeHooks.pickupHandlers.add(handler);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is not supposed to be called outside of Minecraft internals.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
--- ../src_base/minecraft/net/minecraft/src/EntityItem.java 0000-00-00 00:00:00.000000000 -0000
|
||||
+++ ../src_work/minecraft/net/minecraft/src/EntityItem.java 0000-00-00 00:00:00.000000000 -0000
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
+import net.minecraft.src.forge.ForgeHooks;
|
||||
+
|
||||
public class EntityItem extends Entity
|
||||
{
|
||||
public ItemStack item;
|
||||
@@ -139,6 +141,18 @@
|
||||
return;
|
||||
}
|
||||
int i = item.stackSize;
|
||||
+ if (delayBeforeCanPickup == 0 && !ForgeHooks.onItemPickup(entityplayer, this))
|
||||
+ {
|
||||
+ ModLoader.OnItemPickup(entityplayer, item);
|
||||
+ worldObj.playSoundAtEntity(this, "random.pop", 0.2F, ((rand.nextFloat() - rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
|
||||
+ entityplayer.onItemPickup(this, i);
|
||||
+ if (item.stackSize <= 0)
|
||||
+ {
|
||||
+ setEntityDead();
|
||||
+ }
|
||||
+ return;
|
||||
+ }
|
||||
+ i = item.stackSize;
|
||||
if (delayBeforeCanPickup == 0 && entityplayer.inventory.addItemStackToInventory(item))
|
||||
{
|
||||
if (item.itemID == Block.wood.blockID)
|
|
@ -0,0 +1,30 @@
|
|||
--- ../src_base/minecraft_server/net/minecraft/src/EntityItem.java 0000-00-00 00:00:00.000000000 -0000
|
||||
+++ ../src_work/minecraft_server/net/minecraft/src/EntityItem.java 0000-00-00 00:00:00.000000000 -0000
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
+import net.minecraft.src.forge.ForgeHooks;
|
||||
+
|
||||
public class EntityItem extends Entity
|
||||
{
|
||||
public ItemStack item;
|
||||
@@ -139,6 +141,18 @@
|
||||
return;
|
||||
}
|
||||
int i = item.stackSize;
|
||||
+ if (delayBeforeCanPickup == 0 && !ForgeHooks.onItemPickup(entityplayer, this))
|
||||
+ {
|
||||
+ ModLoader.OnItemPickup(entityplayer, item);
|
||||
+ worldObj.playSoundAtEntity(this, "random.pop", 0.2F, ((rand.nextFloat() - rand.nextFloat()) * 0.7F + 1.0F) * 2.0F);
|
||||
+ entityplayer.onItemPickup(this, i);
|
||||
+ if (item.stackSize <= 0)
|
||||
+ {
|
||||
+ setEntityDead();
|
||||
+ }
|
||||
+ return;
|
||||
+ }
|
||||
+ i = item.stackSize;
|
||||
if (delayBeforeCanPickup == 0 && entityplayer.inventory.addItemStackToInventory(item))
|
||||
{
|
||||
if (item.itemID == Block.wood.blockID)
|
Loading…
Reference in a new issue