From 14ccf7834b30e047cda13f6656fea47c84553e78 Mon Sep 17 00:00:00 2001 From: LexManos Date: Fri, 10 Feb 2012 06:56:28 -0800 Subject: [PATCH] Implmented Sengir's IPickupHandler. http://www.mod-buildcraft.com/forums/topic/hook-intercept-item-pickups-by-player-entities/?view=all --- .../net/minecraft/src/forge/ForgeHooks.java | 16 ++++++++++ .../minecraft/src/forge/IPickupHandler.java | 26 ++++++++++++++++ .../minecraft/src/forge/MinecraftForge.java | 9 ++++++ .../net/minecraft/src/EntityItem.java.patch | 30 +++++++++++++++++++ .../net/minecraft/src/EntityItem.java.patch | 30 +++++++++++++++++++ 5 files changed, 111 insertions(+) create mode 100644 forge/forge_common/net/minecraft/src/forge/IPickupHandler.java create mode 100644 forge/patches/minecraft/net/minecraft/src/EntityItem.java.patch create mode 100644 forge/patches/minecraft_server/net/minecraft/src/EntityItem.java.patch diff --git a/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java b/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java index eb65a7376..6b02e678a 100644 --- a/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java +++ b/forge/forge_common/net/minecraft/src/forge/ForgeHooks.java @@ -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; @@ -136,6 +137,21 @@ public class ForgeHooks { } } static LinkedList connectionHandlers = new LinkedList(); + + 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 pickupHandlers = new LinkedList(); // Plant Management // ------------------------------------------------------------ diff --git a/forge/forge_common/net/minecraft/src/forge/IPickupHandler.java b/forge/forge_common/net/minecraft/src/forge/IPickupHandler.java new file mode 100644 index 000000000..3f703c3e2 --- /dev/null +++ b/forge/forge_common/net/minecraft/src/forge/IPickupHandler.java @@ -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); + +} \ 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 9dee1ae19..7c7d28ae2 100755 --- a/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java +++ b/forge/forge_common/net/minecraft/src/forge/MinecraftForge.java @@ -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. */ diff --git a/forge/patches/minecraft/net/minecraft/src/EntityItem.java.patch b/forge/patches/minecraft/net/minecraft/src/EntityItem.java.patch new file mode 100644 index 000000000..ef50c3867 --- /dev/null +++ b/forge/patches/minecraft/net/minecraft/src/EntityItem.java.patch @@ -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) diff --git a/forge/patches/minecraft_server/net/minecraft/src/EntityItem.java.patch b/forge/patches/minecraft_server/net/minecraft/src/EntityItem.java.patch new file mode 100644 index 000000000..ee9e7baa9 --- /dev/null +++ b/forge/patches/minecraft_server/net/minecraft/src/EntityItem.java.patch @@ -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)