Add ability for Items to create custom Entities for themselves when added to the world, PR: #151

This commit is contained in:
LexManos 2012-09-11 17:35:53 -07:00
parent 96be8c4f19
commit aeebb6e9f9
3 changed files with 65 additions and 1 deletions

View file

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

View file

@ -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()

View file

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