Posted September 2, 201411 yr I have written a simple custom entity extending the Entity base class, and am trying to spawn instances of this entity from one of my custom TileEntities. I have registered my entity in the preInit method of the main mod class like this: int uniqueId = EntityRegistry.findGlobalUniqueEntityId(); EntityRegistry.registerGlobalEntityID(MyEntity.class, "myEntity", uniqueId); EntityRegistry.registerModEntity(MyEntity.class, "myEntity", uniqueId, this, 80, 3, false); But when try to instantiate it using Entity.createEntityByName it always returns null, and if I create an instance myself using "new MyEntity..." and then pass it to World.spawnEntityInWorld, I still get a NullPointerException. How should I go about this? Is there something I'm missing?
September 2, 201411 yr Author Ah, of course... This is my entity class. It hardly does anything but I guess that might be the problem. package com.vinther.mymod.entity; import net.minecraft.entity.Entity; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.AxisAlignedBB; import net.minecraft.world.World; public class MyEntity extends Entity { public MyEntity(World world) { super(world); this.setSize(1f, 1f); } @Override protected void entityInit() {} public void onEntityUpdate() { super.onEntityUpdate(); System.out.println(this.posX +", "+ this.posY +", "+ this.posZ); } public boolean canBeCollidedWith() { return true; } @Override protected void readEntityFromNBT(NBTTagCompound p_70037_1_) {} @Override protected void writeEntityToNBT(NBTTagCompound p_70014_1_) {} public AxisAlignedBB getCollisionBox(Entity entity) { return AxisAlignedBB.getBoundingBox(this.posX - this.width / 2f, this.posY - this.height / 2f, this.posZ - this.width / 2f, this.posX + this.width / 2f, this.posY + this.height / 2f, this.posZ + this.width / 2f); } } And this is the TileEntity where I try to instantiate it. package com.vinther.mymod.tileentity; import com.vinther.mymod.MyMod; import com.vinther.mymod.entity.MyEntity; import cpw.mods.fml.relauncher.Side; import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.entity.Entity; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.tileentity.TileEntity; import net.minecraft.util.AxisAlignedBB; import net.minecraft.world.World; public class MyTileEntity extends TileEntity { public MyTileEntity() { MyEntity entity = new MyEntity(worldObj); worldObj.spawnEntityInWorld(entity); // This line throws NullPointerException } ... }
September 2, 201411 yr Author Oh.. Silly me. Thank you! But TileEntity doesn't seem to have a good place to init stuff.
September 3, 201411 yr For tile entities it is updateEntity(){ }. And that boolean is not required because it is standartly true.
September 3, 201411 yr This tile entity doesn't have the boolean for updating every tick and updates every tick anyway: public class TileEntityEnderLift extends TileEntity { public void updateEntity(){ List mobs = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, AxisAlignedBB.getBoundingBox(xCoord - 0.5, yCoord + 1, zCoord - 0.5, xCoord + 0.5, yCoord + 100, zCoord + 0.5)); for (int j = 0; j < mobs.size(); ++j){ EntityLivingBase mob = (EntityLivingBase) mobs.get(j); mob.motionY = 0.1; mob.fallDistance = 0; } } }
September 3, 201411 yr This tile entity doesn't have the boolean for updating every tick and updates every tick anyway: He meant he should make a boolean field (may be called isInitialized) that's by default false, and on update, if it is false set to true and initialize...
September 4, 201411 yr Author Yep, a boolean flag and initializing in update works fine. It's just not as clean as I would like it, but it's fine. Thanks again!
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.