Jump to content

Spawn Entity at Block


somarani

Recommended Posts

Hi, I created a boss and want a way to spawn him. I have a block in my mod that allows players (and items) to walk through. So to spawn the boss, you throw 4 items into the block, the items and the block disappear and the boss spawns. For some reason this wont work. The lightning strikes but I get no sound or fire and I get the boss bar but I dont see him. To further test this, I made it place a diamond block when you throw the items. The block does appear but when you right click it, it goes back to the old block. Does anyone know why this is happening?

 

code - http://pastebin.com/19aBpwCi

 

OR

 

 
package somarani.soulcraft.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.Facing;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import somarani.soulcraft.common.SoulCraft;
import somarani.soulcraft.mobboss.EntitySoulBossMob;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class BlockSoulGlass extends Block {

public static boolean skull = false;
public static boolean spider = false;
public static boolean creeper = false;
public static boolean zombie = false;
public static boolean shouldSpawn = false;

public BlockSoulGlass(Material p_i45394_1_) {
	super(p_i45394_1_);

	setHardness(2f);
	setResistance(5f);
	setBlockName("soulglass");
	setCreativeTab(SoulCraft.tabSoul);
	setLightLevel(0f);
	setBlockTextureName("soulcraft:soulglass");

	GameRegistry.registerBlock(this, "soulglass");

}

@SideOnly(Side.CLIENT)
public void addInformation(ItemStack par1ItemStack,
		EntityPlayer par2EntityPlayer, java.util.List par3List, boolean par4) {

	par3List.add("Allows ONLY players to walk right through");

}

@SideOnly(Side.CLIENT)
public boolean shouldSideBeRendered(IBlockAccess p_149646_1_,
		int p_149646_2_, int p_149646_3_, int p_149646_4_, int p_149646_5_) {
	Block block = p_149646_1_.getBlock(p_149646_2_, p_149646_3_,
			p_149646_4_);

	if (this == SoulCraft.soulGlass) {
		if (p_149646_1_.getBlockMetadata(p_149646_2_, p_149646_3_,
				p_149646_4_) != p_149646_1_.getBlockMetadata(p_149646_2_
				- Facing.offsetsXForSide[p_149646_5_], p_149646_3_
				- Facing.offsetsYForSide[p_149646_5_], p_149646_4_
				- Facing.offsetsZForSide[p_149646_5_])) {
			return true;
		}

		if (block == this) {
			return false;
		}
	}

	return true;
}

@SideOnly(Side.CLIENT)
public int getRenderBlockPass() {
	return 0;
}

@SideOnly(Side.CLIENT)
public boolean isOpaqueCube() {
	return false;
}

public void onEntityCollidedWithBlock(World p_149670_1_, int x,
		int y, int z, Entity entity) {
	if (!(entity instanceof EntityPlayer)) {
		entity.setDead();
	}

	System.out.println(entity);
	if (entity instanceof EntityItem) {
		if (entity.toString().contains("spideressence")) {
			spider = true;
		}

		if (entity.toString().contains("bonehelmet")) {
			skull = true;
		}
		if (entity.toString().contains("creeperessence")) {
			creeper = true;
		}

		if (entity.toString().contains("zombieeessence")) {
			zombie = true;
		}

		if(spider && skull && creeper && zombie){

			shouldSpawn = true;

			EntitySoulBossMob mob = new EntitySoulBossMob(Minecraft.getMinecraft().theWorld);

			mob.posX = x + 2;
			mob.posY = y;
			mob.posZ = z + 2;

			Minecraft.getMinecraft().theWorld.spawnEntityInWorld(new EntityLightningBolt(Minecraft.getMinecraft().theWorld, x, y, z));
			Minecraft.getMinecraft().theWorld.spawnEntityInWorld(mob);
			Minecraft.getMinecraft().theWorld.setBlockToAir(x, y, z);
			Minecraft.getMinecraft().theWorld.setBlock(x, y, z, Blocks.diamond_block); // for testing purposes. 

		}


	}

}

public AxisAlignedBB getCollisionBoundingBoxFromPool(World par1World,
		int par2, int par3, int par4) {
	return null;
}

public boolean renderAsNormalBlock() {
	return false;
}

}

Link to comment
Share on other sites

I think your sided stuff is slightly messed up.

 

First of all, you're spawning the entity on the client side, not on the server side.  The way you're getting the world is a client-side method and will crash when used on server.  If it does spawn on client, it will disappear almost immediately or at least cause other trouble.

 

So you need to grab the world from the entity parameter, and check !entity.worldObj.isRemote to ensure you are on server side before spawning.  And you need to use that worldObj as well for the spawn to avoid crash.

 

I'm actually not sure why it isn't already crashing for you (the Minecraft call should crash on server) so maybe that method is only being called on the client side.  If you are detecting the spawn condition on the client side you then need to pass the information to the server with a custom packet, and the server should spawn the boss based on receiving that packet.

 

You should also use @Override annotation as it is good practice to find any typographical or logical mistakes as it will warn you if the method declaration doesn't match the format of the method you think you're overriding.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

So you need to grab the world from the entity parameter...

 

Why not grab the world from the world parameter?

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Ok this is the new code. Now it happens like you said it would. The boss spawns for a few seconds then disappears, although the diamond block and lightning work just fine.

 

 
public void onEntityCollidedWithBlock(World world1, int x,
		int y, int z, Entity entity) {


	if (!(entity instanceof EntityPlayer)) {
		entity.setDead();
	}

	System.out.println(entity);
	if (entity instanceof EntityItem) {
		if (entity.toString().contains("spideressence")) {
			spider = true;
		}

		if (entity.toString().contains("bonehelmet")) {
			skull = true;
		}
		if (entity.toString().contains("creeperessence")) {
			creeper = true;
		}

		if (entity.toString().contains("zombieeessence")) {
			zombie = true;
		}

		if(spider && skull && creeper && zombie){

			World world = entity.worldObj;

			if(!world.isRemote){

			shouldSpawn = true;

			EntitySoulBossMob boss = new EntitySoulBossMob(world);

			boss.posX = x + 2;
			boss.posY= y + 2;
			boss.posZ = z +2 ;

			world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
			world.setBlockToAir(x, y, z);
			world.setBlock(x, y, z, Blocks.diamond_block); // for testing purposes. 
			world.spawnEntityInWorld(boss);
			}

		}


	}

}

Link to comment
Share on other sites

Ok this is the new code. Now it happens like you said it would. The boss spawns for a few seconds then disappears, although the diamond block and lightning work just fine.

 

 
public void onEntityCollidedWithBlock(World world1, int x,
		int y, int z, Entity entity) {


	if (!(entity instanceof EntityPlayer)) {
		entity.setDead();
	}

	System.out.println(entity);
	if (entity instanceof EntityItem) {
		if (entity.toString().contains("spideressence")) {
			spider = true;
		}

		if (entity.toString().contains("bonehelmet")) {
			skull = true;
		}
		if (entity.toString().contains("creeperessence")) {
			creeper = true;
		}

		if (entity.toString().contains("zombieeessence")) {
			zombie = true;
		}

		if(spider && skull && creeper && zombie){

			World world = entity.worldObj;

			if(!world.isRemote){

			shouldSpawn = true;

			EntitySoulBossMob boss = new EntitySoulBossMob(world);

			boss.posX = x + 2;
			boss.posY= y + 2;
			boss.posZ = z +2 ;

			world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
			world.setBlockToAir(x, y, z);
			world.setBlock(x, y, z, Blocks.diamond_block); // for testing purposes. 
			world.spawnEntityInWorld(boss);
			}

		}


	}

}

 

Not sure what the problem is.  You're now properly spawning on server, so any disappearance is for different reason.  I'm not sure it is even related to the spawn.

 

Is there other code in your boss that would either make it jump positions, render invisibly, or die unexpectedly?

 

I would put in a console statement in your boss entity that prints out the posX, posY, and posZ every tick.  Then you'll see if it has moved somewhere, and also will notice if it dies (since nothing will print).

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

I did the print line and the boss does spawn, then he just disappears (the coordinates print out about 5 times then he dies. Here is his code

 

http://pastebin.com/kf36ddZQ

 

OR

 

 
package somarani.soulcraft.mobboss;

import org.omg.CORBA.PUBLIC_MEMBER;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import scala.annotation.varargs;
import scala.reflect.internal.Trees.This;
import somarani.soulcraft.common.SoulCraft;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiNewChat;
import net.minecraft.enchantment.Enchantment;
import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.IRangedAttackMob;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIArrowAttack;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIFleeSun;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.ai.EntityAILookIdle;
import net.minecraft.entity.ai.EntityAINearestAttackableTarget;
import net.minecraft.entity.ai.EntityAIRestrictSun;
import net.minecraft.entity.ai.EntityAISwimming;
import net.minecraft.entity.ai.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.boss.BossStatus;
import net.minecraft.entity.boss.IBossDisplayData;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.monster.EntityCreeper;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.monster.EntitySkeleton;
import net.minecraft.entity.monster.EntitySpider;
import net.minecraft.entity.monster.EntityZombie;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagInt;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants.NBT;

public class EntitySoulBossMob extends EntityMob implements IRangedAttackMob,
	IBossDisplayData {

public static int count = 1000;
public static int lightning = 0;

public EntitySoulBossMob(World par1World) {
	super(par1World);

	this.stepHeight = 3f;

}

protected void applyEntityAttributes() {
	super.applyEntityAttributes();

	this.getEntityAttribute(SharedMonsterAttributes.maxHealth)
			.setBaseValue(200d);

}

public void onLivingUpdate() {

	System.out.println("X: " + this.posX + ",Y:" + this.posY + ",Z:" + this.posZ );

	BossStatus.setBossStatus(this, true);
	EntityPlayer entityplayer = this.worldObj
			.getClosestVulnerablePlayerToEntity(this, 16.0D);
	if (entityplayer != null) {
		int x = (int) entityplayer.posX;
		int y = (int) entityplayer.posY;
		int z = (int) entityplayer.posZ;

		EntityCreeper creeper = new EntityCreeper(worldObj);
		EntitySkeleton skeleton = new EntitySkeleton(worldObj);
		EntitySpider spider = new EntitySpider(worldObj);
		EntityZombie zombie = new EntityZombie(worldObj);

		creeper.setPosition(x + 10, y + 10, z);
		skeleton.setPosition(this.posX, this.posY, this.posZ);
		spider.setPosition(x + 5, y, z + 5);
		zombie.setPosition(this.posX, this.posY, this.posZ);

		if (count > 169) {
			if (!worldObj.isRemote) {

				worldObj.spawnEntityInWorld(creeper);
				worldObj.spawnEntityInWorld(creeper);
				worldObj.spawnEntityInWorld(skeleton);
				worldObj.spawnEntityInWorld(skeleton);
				worldObj.spawnEntityInWorld(skeleton);
				worldObj.spawnEntityInWorld(spider);
				worldObj.spawnEntityInWorld(zombie);
				worldObj.spawnEntityInWorld(zombie);

				count = 0;
			}
		}

		else
			count++;
	}

	if (this.getHealth() == 0) {

		if (!worldObj.isRemote) {
			this.worldObj.spawnEntityInWorld(new EntityLightningBolt(
					this.worldObj, this.posX, this.posY, this.posZ));
			this.worldObj.spawnEntityInWorld(new EntityLightningBolt(
					this.worldObj, this.posX, this.posY, this.posZ));
			this.worldObj.spawnEntityInWorld(new EntityLightningBolt(
					this.worldObj, this.posX, this.posY, this.posZ));
		}

	}

	if (worldObj.difficultySetting == EnumDifficulty.PEACEFUL) {
		this.kill();
	}

	if (lightning >= 150) {
		if (entityplayer != null) {
			int x = (int) entityplayer.posX;
			int y = (int) entityplayer.posY;
			int z = (int) entityplayer.posZ;

			if (!worldObj.isRemote) {
				this.worldObj.spawnEntityInWorld(new EntityLightningBolt(
						this.worldObj, x + 3, y, z + 2));
				lightning = 0;
			}
		}
	}

	else {
		lightning++;
	}

	super.onLivingUpdate();
}

public void onDeath(DamageSource par1DamageSource) {

	World world = worldObj;

	if (!world.isRemote) {
		int j = this.rand.nextInt(2) + 1;

		this.dropItem(SoulCraft.smallSoulFragment, j);
	}

	super.onDeath(par1DamageSource);

}

@Override
public void attackEntityWithRangedAttack(EntityLivingBase var1, float var2) {
	// TODO Auto-generated method stub

}

}

Link to comment
Share on other sites

Do the coordinates make sense?  Like are they where you expect them?

 

It is good to confirm the side that the instance is running on.  Furthermore, if the printouts stop after a few times, then the entity must be "dying" because the onUpdate() should fire.  You need to figure out why that is happening.  But to confirm that it is dying, maybe also print out the public isDead field.  So try this and post what the console says.

 

System.out.println("X: " + this.posX + ",Y:" + this.posY + ",Z:" + this.posZ+", client side? ="+worldObj.isRemote)+", isDead ="+this.isDead);

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Ok, this is what I get from that

 

 

X: -393.0,Y:6.0,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:6.0,Z:-482.0, client side? =false, isDead =false
X: -393.0,Y:6.0,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:5.9231925,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:5.772594574068951,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:5.551128321694432,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:5.261600127278932,Z:-482.0, client side? =true, isDead =false
X: -393.0,Y:4.906705242455058,Z:-482.0, client side? =true, isDead =false

 

Those are the first couple lines. After the 3rd time it stays the same. I'm not sure why client side is true since im only spawning when world is not remote. Also why is it true at first, turns false then goes back to true.

Link to comment
Share on other sites

It should alternate because both client and server run the onUpdate() method in the class.  But it is very weird that server stops  (then client does).

 

I still think it is dying, but maybe the isDead is being updated between updates or something.  Let's try to override the setDead() method directly to see if we can catch it actually happening.  In your class put the following:

    @Override
    public void setDead()
    {
        System.out.println("setDead() called");
        this.isDead = true;
    }

 

Capture the console output again and post it here.

 

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Ok here is the whole output from when he first spawned

 

 
Spawning entity on client
Entity spawned on client
X: -388.0,Y:6.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:6.0,Z:-494.0, client side? =false, isDead =false
X: -388.0,Y:6.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:5.9231925,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:5.772594574068951,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:5.551128321694432,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:5.261600127278932,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.906705242455058,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.489032186941148,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.011066975123075,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
X: -388.0,Y:4.0,Z:-494.0, client side? =true, isDead =false
setDead() called

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.