From aeebb6e9f990ebfd8d5ee28ff27a6c0ab0763ce8 Mon Sep 17 00:00:00 2001 From: LexManos Date: Tue, 11 Sep 2012 17:35:53 -0700 Subject: [PATCH] Add ability for Items to create custom Entities for themselves when added to the world, PR: #151 --- .../common/ForgeInternalHandler.java | 28 +++++++++++++++++ .../minecraftforge/common/MinecraftForge.java | 8 +++++ .../common/net/minecraft/src/Item.java.patch | 30 ++++++++++++++++++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 common/net/minecraftforge/common/ForgeInternalHandler.java diff --git a/common/net/minecraftforge/common/ForgeInternalHandler.java b/common/net/minecraftforge/common/ForgeInternalHandler.java new file mode 100644 index 000000000..16c516949 --- /dev/null +++ b/common/net/minecraftforge/common/ForgeInternalHandler.java @@ -0,0 +1,28 @@ +package net.minecraftforge.common; + +import net.minecraft.src.*; +import net.minecraftforge.event.*; +import net.minecraftforge.event.entity.*; + +public class ForgeInternalHandler +{ + @ForgeSubscribe(priority = EventPriority.HIGHEST) + public void onEntityJoinWorld(EntityJoinWorldEvent event) + { + Entity entity = event.entity; + if (entity instanceof EntityItem) + { + ItemStack item = ((EntityItem)entity).item; + if (item.getItem().hasCustomEntity(item)) + { + Entity newEntity = item.getItem().createEntity(event.world, entity, item); + if (newEntity != null) + { + entity.setDead(); + event.setCanceled(true); + event.world.spawnEntityInWorld(entity); + } + } + } + } +} diff --git a/common/net/minecraftforge/common/MinecraftForge.java b/common/net/minecraftforge/common/MinecraftForge.java index fb6b7698a..fe1dc8323 100644 --- a/common/net/minecraftforge/common/MinecraftForge.java +++ b/common/net/minecraftforge/common/MinecraftForge.java @@ -3,6 +3,8 @@ package net.minecraftforge.common; import java.lang.reflect.Constructor; import java.util.*; +import cpw.mods.fml.common.FMLLog; + import net.minecraft.src.*; import net.minecraftforge.common.ForgeHooks.GrassEntry; import net.minecraftforge.common.ForgeHooks.SeedEntry; @@ -19,6 +21,7 @@ public class MinecraftForge */ public static final EventBus EVENT_BUS = new EventBus(); public static boolean SPAWNER_ALLOW_ON_INVERTED = false; + private static final ForgeInternalHandler INTERNAL_HANDLER = new ForgeInternalHandler(); /** Register a new plant to be planted when bonemeal is used on grass. @@ -166,6 +169,9 @@ public class MinecraftForge */ public static void initialize() { + System.out.printf("MinecraftForge v%s Initialized\n", ForgeVersion.getVersion()); + FMLLog.info("MinecraftForge v%s Initialized", ForgeVersion.getVersion()); + Block filler = new Block(0, Material.air); Block.blocksList[0] = null; Block.opaqueCubeLookup[0] = false; @@ -185,6 +191,8 @@ public class MinecraftForge temp[x] = EntityEnderman.carriableBlocks[x]; } EntityEnderman.carriableBlocks = temp; + + EVENT_BUS.register(INTERNAL_HANDLER); } public static String getBrandingVersion() diff --git a/patches/common/net/minecraft/src/Item.java.patch b/patches/common/net/minecraft/src/Item.java.patch index b6f028fe9..1540617c7 100644 --- a/patches/common/net/minecraft/src/Item.java.patch +++ b/patches/common/net/minecraft/src/Item.java.patch @@ -37,7 +37,7 @@ Vec3 var23 = var13.addVector((double)var18 * var21, (double)var17 * var21, (double)var20 * var21); return par1World.rayTraceBlocks_do_do(var13, var23, par3, !par3); } -@@ -650,4 +662,212 @@ +@@ -650,4 +662,240 @@ { StatList.initStats(); } @@ -248,5 +248,33 @@ + public int getEntityLifespan(ItemStack itemStack, World world) + { + return 6000; ++ } ++ ++ /** ++ * Determines if this Item has a special entity for when they are in the world. ++ * Is called when a EntityItem is spawned in the world, if true and Item#createCustomEntity ++ * returns non null, the EntityItem will be destroyed and the new Entity will be added to the world. ++ * ++ * @param stack The current item stack ++ * @return True of the item has a custom entity, If true, Item#createCustomEntity will be called ++ */ ++ public boolean hasCustomEntity(ItemStack stack) ++ { ++ return false; ++ } ++ ++ /** ++ * This function should return a new entity to replace the dropped item. ++ * Returning null here will not kill the EntityItem and will leave it to function normally. ++ * Called when the item it placed in a world. ++ * ++ * @param world The world object ++ * @param location The EntityItem object, useful for getting the position of the entity ++ * @param itemstack The current item stack ++ * @return A new Entity object to spawn or null ++ */ ++ public Entity createEntity(World world, Entity location, ItemStack itemstack) ++ { ++ return null; + } }