Jump to content

Spawning an Entity


TLHPoE

Recommended Posts

In my mod, there's a block that when right clicked spawns the Monkeysaur entity:

package tlhpoe.fs.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import tlhpoe.fs.entity.boss.EntityMonkeysaur;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockMonkeysaurAltar extends Block {
public IIcon iSide, iTop;

public BlockMonkeysaurAltar() {
	super(Material.rock);
}

@Override
@SideOnly(Side.CLIENT)
public IIcon getIcon(int side, int p_149691_2_) {
	if(side == 1)
		return iTop;
	return iSide;
}

@Override
@SideOnly(Side.CLIENT)
public void registerBlockIcons(IIconRegister p_149651_1_) {
	this.iSide = Blocks.gold_block.getBlockTextureFromSide(0);
	this.iTop = p_149651_1_.registerIcon("fs:monkeysaurAltar");
	this.blockIcon = this.iTop;
}

@Override
public boolean onBlockActivated(World w, int x, int y, int z, EntityPlayer p_149727_5_, int p_149727_6_, float p_149727_7_, float p_149727_8_, float p_149727_9_) {
	if(!w.isRemote) {
		EntityMonkeysaur e = new EntityMonkeysaur(w);
		e.posX = x;
		e.posY = y;
		e.posZ = z;
		w.spawnEntityInWorld(e);
		w.setBlock(x, y, z, Blocks.air);
	}
	return true;
}
}

 

The problem is, the entity appears and I can't interact with it and it doesn't do anything. A few seconds later it disappears.

 

Here's the entity's code:

package tlhpoe.fs.entity.boss;

import net.minecraft.command.IEntitySelector;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.boss.IBossDisplayData;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.IMob;
import net.minecraft.world.World;

public class EntityMonkeysaur extends EntityMob implements IBossDisplayData, IMob {
private static final IEntitySelector attackEntitySelector = new IEntitySelector() {
	public boolean isEntityApplicable(Entity par1Entity) {
		return par1Entity instanceof EntityLivingBase && !(par1Entity instanceof EntityMonkeysaur);
	}
};

public EntityMonkeysaur(World par1World) {
	super(par1World);
	this.setSize(0.6F * 4, 1.8F * 4);
	this.tasks.addTask(0, new EntityAISwimming(this));
	this.tasks.addTask(1, new EntityAIAttackOnCollide(this, EntityLivingBase.class, 1.0D, false));
	this.tasks.addTask(7, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(8, new EntityAIWatchClosest(this, EntityLivingBase.class, 8.0F));
	this.tasks.addTask(8, new EntityAILookIdle(this));
	this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, true));
	this.targetTasks.addTask(2, new EntityAINearestAttackableTarget(this, EntityLivingBase.class, 0, false, false, attackEntitySelector));
	this.experienceValue = 350;
}

@Override
protected void applyEntityAttributes() {
	super.applyEntityAttributes();
	this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(64.0D);
	this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(200.0D);
	this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(10);
	this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.23000000417232513D);
}

@Override
public void setInWeb() {
}

@Override
protected void despawnEntity() {
	this.entityAge = 0;
}

@Override
protected void fall(float par1) {
}

@Override
protected boolean isAIEnabled() {
	return true;
}
}

Kain

Link to comment
Share on other sites

I'm using 1.7.2, Minecraft stores its blocks in the Blocks class.

 

Here's where I do my entity registering:

package tlhpoe.fs.helper;

import net.minecraft.client.renderer.entity.Render;
import net.minecraft.entity.Entity;
import tlhpoe.fs.entity.EntityIceCreeper;
import tlhpoe.fs.entity.EntityWaterCreeper;
import tlhpoe.fs.entity.boss.EntityMonkeysaur;
import tlhpoe.fs.entity.render.RenderFSCreeper;
import tlhpoe.fs.entity.render.RenderMonkeysaur;
import cpw.mods.fml.client.registry.RenderingRegistry;
import cpw.mods.fml.common.registry.EntityRegistry;

public class EntityHelper {
public static void doServer() {
	doEntity(EntityIceCreeper.class, "iceCreeper", 0xFFFFFF, 0x00FFFF);
	doEntity(EntityWaterCreeper.class, "waterCreeper", 0x0F8FFF, 0x00B2FF);
	doEntity(EntityMonkeysaur.class, "monkeysaur", 0x663300, 0x472400);
}

public static void doClient() {
	doRender(EntityIceCreeper.class, new RenderFSCreeper("ice"));
	doRender(EntityWaterCreeper.class, new RenderFSCreeper("water"));
	doRender(EntityMonkeysaur.class, new RenderMonkeysaur());
}

private static void doEntity(Class<? extends Entity> clazz, String name, int bgColor, int spotColor) {
	int id = EntityRegistry.findGlobalUniqueEntityId();
	EntityRegistry.registerGlobalEntityID(clazz, name, id, bgColor, spotColor);
}

private static void doRender(Class<? extends Entity> clazz, Render render) {
	RenderingRegistry.registerEntityRenderingHandler(clazz, render);
}
}

 

I can spawn it just fine with the spawn egg.

Kain

Link to comment
Share on other sites

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.