Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

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?

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

    ...

}

  • Author

Oh.. Silly me. Thank you!

 

But TileEntity doesn't seem to have a good place to init stuff.

For tile entities it is updateEntity(){

}.

 

And that boolean is not required because it is standartly true.

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

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

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.