Hello guys. Today I wanted to make an Entity spawn out of another when it is killed. Reason I'm doing it, is because I have a "False Netherrack" Entity, that when "Mined", dies and spawns a "Netherman" Entity, possibly as a Reinforcement. Here is my Code for the classes I'm wanting to use in this event:
EntityNetherman
In general, you should override existing methods instead of using events wherever possible. This way your code will only run if your class (Block, Item, Entity, etc.) is involved.
In this case, override EntityLivingBase#onDeath in EntityFalseNetherrack instead of subscribing to LivingDeathEvent.
Do you have a problem/question, or is this meant to be an informative post?
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
In general, you should override existing methods instead of using events wherever possible. This way your code will only run if your class (Block, Item, Entity, etc.) is involved.
In this case, override EntityLivingBase#onDeath in EntityFalseNetherrack instead of subscribing to LivingDeathEvent.
Do you have a problem/question, or is this meant to be an informative post?
It's a problem I'm having, sorry if It came off in a wrong way :/
I'll get to work on this real quick, and return the results.
[/p]
[p]@Override
public void onDeath(DamageSource par1DamageSource)
{
{
Entity entity = par1DamageSource.getEntity();
if (entity != null)
{
if (entity instanceof EntityPlayer)
{
EntityNetherman man = new EntityNetherman(entity.worldObj);
man.setLocationAndAngles(entity.posX, entity.posY, entity.posZ, 0, 0);
if (!entity.worldObj.isRemote) {
entity.worldObj.spawnEntityInWorld(man);
}
}
}
}[/p]
[p]
But I have a question, you see, the Mob is spawned in the Player's Origin, is there any way to make it spawn at the Origin of the "EntityFalseNetherrack"?
Use the EntityFalseNetherrack's world/position fields instead of the EntityPlayer's. this is the EntityFalseNetherrack that was killed.
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Thanks, I got it to work just fine. Got one last question although, how do I fix the Rotation of the Entity not being like regular Netherrack (Or any real Block in general?) Take a look at the Attachment.
Anyone? I'd rather have the Mob aligned onto the Minecraft "Cube" Grid, with every other block, so Players won't be able to tell if it is real Netherrack or not. This behavior it has where it rotates a random direction only happens on Spawn, and then it stops.
Try set the entity's rotationYaw field to 0 in the constructor. The EntityLivingBase constructor sets it to a random value.
If you entity isn't actually alive like a regular mob, consider extending Entity (like EntityFallingBlock does) instead of EntityMob.
Rollback Post to RevisionRollBack
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
EntityNetherman
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureAttribute;
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.EntityAIWander;
import net.minecraft.entity.ai.EntityAIWatchClosest;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.util.DamageSource;
import net.minecraft.util.MathHelper;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World; public class EntityNetherman extends EntityMob{ public EntityNetherman(World par1World) {
super(par1World);
this.experienceValue = 45;
this.tasks.addTask(1, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1.0D, false));
this.tasks.addTask(7, new EntityAIWander(this, 1.0D));
this.tasks.addTask(2, new EntityAIWatchClosest(this, EntityPlayer.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, EntityPlayer.class, 0, true));
this.setSize(1.0F, 1.5F);
this.isImmuneToFire = true;
}
public EntityNetherman(World world, double x, double y, double z)
{
this(world);
setPosition(x, y, z);
}
@Override
protected boolean isAIEnabled(){
return true;
}
@Override
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
this.getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(16.0D);
this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.3D);
this.getEntityAttribute(SharedMonsterAttributes.knockbackResistance).setBaseValue(0.045D);
this.getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(2.0D);
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(25.5D);
}
protected void entityInit()
{
super.entityInit();
this.getDataWatcher().addObject(12, Byte.valueOf((byte)0));
this.getDataWatcher().addObject(13, Byte.valueOf((byte)0));
this.getDataWatcher().addObject(14, Byte.valueOf((byte)0));
}
public void onUpdate()
{
super.onUpdate(); if (!this.worldObj.isRemote && this.worldObj.difficultySetting == EnumDifficulty.PEACEFUL)
{
this.setDead();
}
}
protected String getLivingSound()
{
return null;
}
protected String getHurtSound()
{
return null;
}
protected String getDeathSound()
{
return null;
} @Override public boolean attackEntityAsMob(Entity entityTarget)
{
float attackDamage = (float)getEntityAttribute(SharedMonsterAttributes .attackDamage).getAttributeValue();
int knockbackModifier = 0; if (entityTarget instanceof EntityLivingBase)
{
attackDamage += EnchantmentHelper.getEnchantmentModifierLiving(this, (EntityLivingBase)entityTarget);
knockbackModifier += EnchantmentHelper.getKnockbackModifier(this, (EntityLivingBase)entityTarget);
} boolean isTargetHurt = entityTarget.attackEntityFrom(DamageSource .causeMobDamage(this), attackDamage); if (hasAttacked)
{
if (knockbackModifier > 0)
{
entityTarget.addVelocity((double)(-MathHelper.sin(rotationYaw * (float)Math.PI / 180.0F) * (float)knockbackModifier * 0.5F), 0.1D, (double)(MathHelper.cos(rotationYaw * (float)Math.PI / 180.0F) * (float)knockbackModifier * 0.5F));
motionX *= 0.6D;
motionZ *= 0.6D;
} int fireModifier = EnchantmentHelper.getFireAspectModifier(this); if (fireModifier > 0)
{
entityTarget.setFire(fireModifier * 4);
} if (entityTarget instanceof EntityLivingBase)
{
EnchantmentHelper.func_151384_a((EntityLivingBase)entityTarget, this);
} EnchantmentHelper.func_151385_b(this, entityTarget);
}
return isTargetHurt;
}
}
EntityFalseNetherrack
import java.util.UUID; import net.minecraft.entity.DataWatcher;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.InventoryPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.DamageSource;
import net.minecraft.world.EnumDifficulty;
import net.minecraft.world.World; public class EntityFalseNetherrack
extends EntityMob
{
public EntityFalseNetherrack(World world)
{
super(world);
setSize(1.0F, 1.0F);
}
@Override
protected void applyEntityAttributes()
{
super.applyEntityAttributes();
this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(0.5D);
}
public boolean isAIEnabled()
{
return false;
}
protected void entityInit()
{
super.entityInit();
this.dataWatcher.addObject(18, "");
}
protected void updateEntityActionState() {}
public void onUpdate(){
super.onUpdate(); if (!this.worldObj.isRemote && this.worldObj.difficultySetting == EnumDifficulty.PEACEFUL)
{
this.setDead();
}
}
public void moveEntity(double d, double d1, double d2) {}
public AxisAlignedBB getCollisionBox(Entity p_70114_1_)
{
return p_70114_1_.boundingBox;
}
public AxisAlignedBB getBoundingBox()
{
return this.boundingBox;
}
public boolean canBePushed()
{
return false;
}
public boolean isMovementCeased()
{
return true;
}
public boolean canBeCollidedWith()
{
return !this.isDead;
}
protected String getHurtSound()
{
return null;
}
protected String getDeathSound()
{
return null;
}
protected void dropFewItems(boolean par1, int par2)
{
dropItem(Item.getItemFromBlock(Blocks.netherrack), 1);
}
}
NetherrackEvent
import com.stormarmory.client.entity.EntityNetherman; import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.entity.passive.EntityPig;
import net.minecraft.init.Items;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.living.LivingDeathEvent; public class NetherrackEvent { @SubscribeEvent
public void spawnReinforcements(LivingDeathEvent event){
if (!(event.entity instanceof EntityFalseNetherrack)) {
return;
} for (int i = 0 ; i < 5 ; i++) {
EntityNetherman man = new EntityNetherman(event.entity.worldObj);
man.setLocationAndAngles(event.entity.posX, event.entity.posY, event.entity.posZ, 0, 0);
if (!event.entity.worldObj.isRemote) {
event.entity.worldObj.spawnEntityInWorld(man);
}
}
}
}
In general, you should override existing methods instead of using events wherever possible. This way your code will only run if your class (Block, Item, Entity, etc.) is involved.
In this case, override EntityLivingBase#onDeath in EntityFalseNetherrack instead of subscribing to LivingDeathEvent.
Do you have a problem/question, or is this meant to be an informative post?
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
It's a problem I'm having, sorry if It came off in a wrong way :/
I'll get to work on this real quick, and return the results.
Okay, I got it now, thanks again.
Here is what the code looks like:
But I have a question, you see, the Mob is spawned in the Player's Origin, is there any way to make it spawn at the Origin of the "EntityFalseNetherrack"?
Use the EntityFalseNetherrack's world/position fields instead of the EntityPlayer's. this is the EntityFalseNetherrack that was killed.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Thanks, I got it to work just fine. Got one last question although, how do I fix the Rotation of the Entity not being like regular Netherrack (Or any real Block in general?) Take a look at the Attachment.
Anyone? I'd rather have the Mob aligned onto the Minecraft "Cube" Grid, with every other block, so Players won't be able to tell if it is real Netherrack or not. This behavior it has where it rotates a random direction only happens on Spawn, and then it stops.
Try set the entity's rotationYaw field to 0 in the constructor. The EntityLivingBase constructor sets it to a random value.
If you entity isn't actually alive like a regular mob, consider extending Entity (like EntityFallingBlock does) instead of EntityMob.
Chisel Facades: For all your decorative pipe-hiding needs.
Please don't PM me to ask for help or to join your mod development team. Asking your question in a public thread preserves it for people who are having the same problem in the future. I'm not interested in developing mods with people.
Yep, this looks like Netherrack. Thanks for the help, these are just great little things to have in a Mod.