Jump to content

[1.7.10] How to spawn custom entity directly? (Not through world gen or egg)


Recommended Posts

Posted

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?

Posted

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
    }

    ...

}

Posted

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

Posted

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...

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.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.