LTXmc Posted November 26, 2012 Share Posted November 26, 2012 I am quite new to minecraft modding and I am having trouble trying to detect entities (mobs/animals) near my auto laser shooter block. Please help out. Quote Link to comment Share on other sites More sharing options...
Blade Avuari Posted November 28, 2012 Share Posted November 28, 2012 Take a look at BlockMobSpawner and TileEntityMobSpawner Block package net.minecraft.src; import cpw.mods.fml.common.Side; import cpw.mods.fml.common.asm.SideOnly; import java.util.Random; public class BlockMobSpawner extends BlockContainer { protected BlockMobSpawner(int par1, int par2) { super(par1, par2, Material.rock); } /** * Returns a new instance of a block's tile entity class. Called on placing the block. */ public TileEntity createNewTileEntity(World par1World) { return new TileEntityMobSpawner(); } /** * Returns the ID of the items to drop on destruction. */ public int idDropped(int par1, Random par2Random, int par3) { return 0; } /** * Returns the quantity of items to drop on block destruction. */ public int quantityDropped(Random par1Random) { return 0; } /** * Drops the block items with a specified chance of dropping the specified items */ public void dropBlockAsItemWithChance(World par1World, int par2, int par3, int par4, int par5, float par6, int par7) { super.dropBlockAsItemWithChance(par1World, par2, par3, par4, par5, par6, par7); int var8 = 15 + par1World.rand.nextInt(15) + par1World.rand.nextInt(15); this.dropXpOnBlockBreak(par1World, par2, par3, par4, var8); } /** * Is this block (a) opaque and (b) a full 1m cube? This determines whether or not to render the shared face of two * adjacent blocks and also whether the player can attach torches, redstone wire, etc to this block. */ public boolean isOpaqueCube() { return false; } @SideOnly(Side.CLIENT) /** * only called by clickMiddleMouseButton , and passed to inventory.setCurrentItem (along with isCreative) */ public int idPicked(World par1World, int par2, int par3, int par4) { return 0; } } Tile Entity package net.minecraft.src; import cpw.mods.fml.common.Side; import cpw.mods.fml.common.asm.SideOnly; import java.util.Iterator; public class TileEntityMobSpawner extends TileEntity { /** The stored delay before a new spawn. */ public int delay = -1; /** * The string ID of the mobs being spawned from this spawner. Defaults to pig, apparently. */ private String mobID = "Pig"; /** The extra NBT data to add to spawned entities */ private NBTTagCompound spawnerTags = null; public double yaw; public double yaw2 = 0.0D; private int minSpawnDelay = 200; private int maxSpawnDelay = 800; private int spawnCount = 4; @SideOnly(Side.CLIENT) private Entity spawnedMob; private int field_82350_j = 6; private int field_82349_r = 16; private int field_82348_s = 4; public TileEntityMobSpawner() { this.delay = 20; } @SideOnly(Side.CLIENT) public String getMobID() { return this.mobID; } public void setMobID(String par1Str) { this.mobID = par1Str; } /** * Returns true if there is a player in range (using World.getClosestPlayer) */ public boolean anyPlayerInRange() { return this.worldObj.getClosestPlayer((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D, (double)this.field_82349_r) != null; } /** * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count * ticks and creates a new spawn inside its implementation. */ public void updateEntity() { if (this.anyPlayerInRange()) { if (this.worldObj.isRemote) { double var1 = (double)((float)this.xCoord + this.worldObj.rand.nextFloat()); double var3 = (double)((float)this.yCoord + this.worldObj.rand.nextFloat()); double var5 = (double)((float)this.zCoord + this.worldObj.rand.nextFloat()); this.worldObj.spawnParticle("smoke", var1, var3, var5, 0.0D, 0.0D, 0.0D); this.worldObj.spawnParticle("flame", var1, var3, var5, 0.0D, 0.0D, 0.0D); if (this.delay > 0) { --this.delay; } this.yaw2 = this.yaw; this.yaw = (this.yaw + (double)(1000.0F / ((float)this.delay + 200.0F))) % 360.0D; } else { if (this.delay == -1) { this.updateDelay(); } if (this.delay > 0) { --this.delay; return; } for (int var11 = 0; var11 < this.spawnCount; ++var11) { Entity var2 = EntityList.createEntityByName(this.mobID, this.worldObj); if (var2 == null) { return; } int var12 = this.worldObj.getEntitiesWithinAABB(var2.getClass(), AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)this.xCoord, (double)this.yCoord, (double)this.zCoord, (double)(this.xCoord + 1), (double)(this.yCoord + 1), (double)(this.zCoord + 1)).expand((double)(this.field_82348_s * 2), 4.0D, (double)(this.field_82348_s * 2))).size(); if (var12 >= this.field_82350_j) { this.updateDelay(); return; } if (var2 != null) { double var4 = (double)this.xCoord + (this.worldObj.rand.nextDouble() - this.worldObj.rand.nextDouble()) * (double)this.field_82348_s; double var6 = (double)(this.yCoord + this.worldObj.rand.nextInt(3) - 1); double var8 = (double)this.zCoord + (this.worldObj.rand.nextDouble() - this.worldObj.rand.nextDouble()) * (double)this.field_82348_s; EntityLiving var10 = var2 instanceof EntityLiving ? (EntityLiving)var2 : null; var2.setLocationAndAngles(var4, var6, var8, this.worldObj.rand.nextFloat() * 360.0F, 0.0F); if (var10 == null || var10.getCanSpawnHere()) { this.writeNBTTagsTo(var2); this.worldObj.spawnEntityInWorld(var2); this.worldObj.playAuxSFX(2004, this.xCoord, this.yCoord, this.zCoord, 0); if (var10 != null) { var10.spawnExplosionParticle(); } this.updateDelay(); } } } } super.updateEntity(); } } public void writeNBTTagsTo(Entity par1Entity) { if (this.spawnerTags != null) { NBTTagCompound var2 = new NBTTagCompound(); par1Entity.addEntityID(var2); Iterator var3 = this.spawnerTags.getTags().iterator(); while (var3.hasNext()) { NBTBase var4 = (NBTBase)var3.next(); var2.setTag(var4.getName(), var4.copy()); } par1Entity.readFromNBT(var2); } else if (par1Entity instanceof EntityLiving && par1Entity.worldObj != null) { ((EntityLiving)par1Entity).initCreature(); } } /** * Sets the delay before a new spawn (base delay of 200 + random number up to 600). */ private void updateDelay() { if (this.maxSpawnDelay <= this.minSpawnDelay) { this.delay = this.minSpawnDelay; } else { this.delay = this.minSpawnDelay + this.worldObj.rand.nextInt(this.maxSpawnDelay - this.minSpawnDelay); } this.worldObj.addBlockEvent(this.xCoord, this.yCoord, this.zCoord, this.getBlockType().blockID, 1, 0); } /** * Reads a tile entity from NBT. */ public void readFromNBT(NBTTagCompound par1NBTTagCompound) { super.readFromNBT(par1NBTTagCompound); this.mobID = par1NBTTagCompound.getString("EntityId"); this.delay = par1NBTTagCompound.getShort("Delay"); if (par1NBTTagCompound.hasKey("SpawnData")) { this.spawnerTags = par1NBTTagCompound.getCompoundTag("SpawnData"); } else { this.spawnerTags = null; } if (par1NBTTagCompound.hasKey("MinSpawnDelay")) { this.minSpawnDelay = par1NBTTagCompound.getShort("MinSpawnDelay"); this.maxSpawnDelay = par1NBTTagCompound.getShort("MaxSpawnDelay"); this.spawnCount = par1NBTTagCompound.getShort("SpawnCount"); } if (par1NBTTagCompound.hasKey("MaxNearbyEntities")) { this.field_82350_j = par1NBTTagCompound.getShort("MaxNearbyEntities"); this.field_82349_r = par1NBTTagCompound.getShort("RequiredPlayerRange"); } if (par1NBTTagCompound.hasKey("SpawnRange")) { this.field_82348_s = par1NBTTagCompound.getShort("SpawnRange"); } } /** * Writes a tile entity to NBT. */ public void writeToNBT(NBTTagCompound par1NBTTagCompound) { super.writeToNBT(par1NBTTagCompound); par1NBTTagCompound.setString("EntityId", this.mobID); par1NBTTagCompound.setShort("Delay", (short)this.delay); par1NBTTagCompound.setShort("MinSpawnDelay", (short)this.minSpawnDelay); par1NBTTagCompound.setShort("MaxSpawnDelay", (short)this.maxSpawnDelay); par1NBTTagCompound.setShort("SpawnCount", (short)this.spawnCount); par1NBTTagCompound.setShort("MaxNearbyEntities", (short)this.field_82350_j); par1NBTTagCompound.setShort("RequiredPlayerRange", (short)this.field_82349_r); par1NBTTagCompound.setShort("SpawnRange", (short)this.field_82348_s); if (this.spawnerTags != null) { par1NBTTagCompound.setCompoundTag("SpawnData", this.spawnerTags); } } @SideOnly(Side.CLIENT) /** * will create the entity from the internalID the first time it is accessed */ public Entity getMobEntity() { if (this.spawnedMob == null) { Entity var1 = EntityList.createEntityByName(this.getMobID(), (World)null); this.writeNBTTagsTo(var1); this.spawnedMob = var1; } return this.spawnedMob; } /** * Overriden in a sign to provide the text. */ public Packet getDescriptionPacket() { NBTTagCompound var1 = new NBTTagCompound(); this.writeToNBT(var1); return new Packet132TileEntityData(this.xCoord, this.yCoord, this.zCoord, 1, var1); } /** * Called when a client event is received with the event number and argument, see World.sendClientEvent */ public void receiveClientEvent(int par1, int par2) { if (par1 == 1 && this.worldObj.isRemote) { this.delay = this.minSpawnDelay; } } } Check out these two functions anyPlayerInRange() public boolean anyPlayerInRange() { return this.worldObj.getClosestPlayer((double)this.xCoord + 0.5D, (double)this.yCoord + 0.5D, (double)this.zCoord + 0.5D, (double)this.field_82349_r) != null; } updateEntity() /** * Allows the entity to update its state. Overridden in most subclasses, e.g. the mob spawner uses this to count * ticks and creates a new spawn inside its implementation. */ public void updateEntity() { if (this.anyPlayerInRange()) { if (this.worldObj.isRemote) { double var1 = (double)((float)this.xCoord + this.worldObj.rand.nextFloat()); double var3 = (double)((float)this.yCoord + this.worldObj.rand.nextFloat()); double var5 = (double)((float)this.zCoord + this.worldObj.rand.nextFloat()); this.worldObj.spawnParticle("smoke", var1, var3, var5, 0.0D, 0.0D, 0.0D); this.worldObj.spawnParticle("flame", var1, var3, var5, 0.0D, 0.0D, 0.0D); if (this.delay > 0) { --this.delay; } this.yaw2 = this.yaw; this.yaw = (this.yaw + (double)(1000.0F / ((float)this.delay + 200.0F))) % 360.0D; } else { if (this.delay == -1) { this.updateDelay(); } if (this.delay > 0) { --this.delay; return; } for (int var11 = 0; var11 < this.spawnCount; ++var11) { Entity var2 = EntityList.createEntityByName(this.mobID, this.worldObj); if (var2 == null) { return; } int var12 = this.worldObj.getEntitiesWithinAABB(var2.getClass(), AxisAlignedBB.getAABBPool().addOrModifyAABBInPool((double)this.xCoord, (double)this.yCoord, (double)this.zCoord, (double)(this.xCoord + 1), (double)(this.yCoord + 1), (double)(this.zCoord + 1)).expand((double)(this.field_82348_s * 2), 4.0D, (double)(this.field_82348_s * 2))).size(); if (var12 >= this.field_82350_j) { this.updateDelay(); return; } if (var2 != null) { double var4 = (double)this.xCoord + (this.worldObj.rand.nextDouble() - this.worldObj.rand.nextDouble()) * (double)this.field_82348_s; double var6 = (double)(this.yCoord + this.worldObj.rand.nextInt(3) - 1); double var8 = (double)this.zCoord + (this.worldObj.rand.nextDouble() - this.worldObj.rand.nextDouble()) * (double)this.field_82348_s; EntityLiving var10 = var2 instanceof EntityLiving ? (EntityLiving)var2 : null; var2.setLocationAndAngles(var4, var6, var8, this.worldObj.rand.nextFloat() * 360.0F, 0.0F); if (var10 == null || var10.getCanSpawnHere()) { this.writeNBTTagsTo(var2); this.worldObj.spawnEntityInWorld(var2); this.worldObj.playAuxSFX(2004, this.xCoord, this.yCoord, this.zCoord, 0); if (var10 != null) { var10.spawnExplosionParticle(); } this.updateDelay(); } } } } super.updateEntity(); } } Quote http://i.imgur.com/ivK3J.png[/img] I'm a little surprised that I am still ranked as a "Forge Modder," having not posted a single mod since my animals mod... I have to complete Digging Deeper!, fast! Link to comment Share on other sites More sharing options...
Recommended Posts
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.