Jump to content

[1.10] How to Entity Make Attack


terraya

Recommended Posts

Hello@All ,

 

so i got only 1 problem,

my entity dont want to attack me , but i know whats missing,

 

in 1.7.10 there was a function like :" AIAttackOnColide"

i cant find it for 1.10 -_-

 

heres my entity class:

 

public class EntityModelTest extends EntityMob{

public EntityModelTest(World worldIn) {
	super(worldIn);

	//AI Components
	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, new EntityAIAttackMelee(this, 1.0D, false));
	this.tasks.addTask(3, new EntityAIMoveTowardsRestriction(this, 1.0D));
	this.tasks.addTask(4, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 20F));
	this.tasks.addTask(6, new EntityAILookIdle(this));

	this.setSize(0.6f, 1.5f);
}

@Override
protected void applyEntityAttributes()
{
  super.applyEntityAttributes();
	this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(35.0D);
	this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.23000000417232513D);
	this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(4.0D);
	this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(1000.0D);
}

 @Override
 public void readEntityFromNBT(NBTTagCompound nbt)
 {
  super.readEntityFromNBT(nbt);
 }

 @Override
 public void writeEntityToNBT(NBTTagCompound nbt)
 {
  super.writeEntityToNBT(nbt);
 }

 @Override
 public boolean attackEntityAsMob(Entity entity){
  super.attackEntityAsMob(entity);
  return entity.attackEntityFrom(DamageSource.causeMobDamage(this), 1);
 }

protected boolean isAIEnabled()
{
return true;
}

  public boolean onUpdate(EntityLivingBase mob)
  {
    if ((getAttackTarget() != null) && 
      ((getAttackTarget() instanceof EntityPlayer))) {
      tryAbility(mob, getAttackTarget());
    }
    return onUpdate(mob);
  }

  private long nextAbilityUse = 0L;
  
  private void tryAbility(EntityLivingBase mob, EntityLivingBase target)
  {
    if ((target == null) || (target.getRidingEntity() != null) || (!mob.canEntityBeSeen(target))) {
      return;
    }
    long time = System.currentTimeMillis();
    if ((time > this.nextAbilityUse) && 
      (mob.getDistanceToEntity(target) > 3.0F) && 
      (target.worldObj.canBlockSeeSky(new BlockPos(MathHelper.floor_double(target.posX), MathHelper.floor_double(target.posY), MathHelper.floor_double(target.posZ)))))
    {
      this.nextAbilityUse = (time + 15000L);
      mob.worldObj.addWeatherEffect(new EntityLightningBolt(mob.worldObj, target.posX, target.posY - 1.0D, target.posZ, false));
    }
  }

    protected boolean canDropLoot()
    {
        return false;
    }

}

Link to comment
Share on other sites

Let me just go look at vanilla code for a second.

 

Oh look at that.

 

EntityAIZombieAttack extends EntityAIAttackMelee

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

EntityAIAttackMelee isn't an "attack you when you attack it" it's the "I bump into you and deal damage" behavior.

 

You might also want to look at what makes EntityAIZombieAttack different.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

EntityAIAttackMelee isn't an "attack you when you attack it" it's the "I bump into you and deal damage" behavior.

 

You might also want to look at what makes EntityAIZombieAttack different.

 

EDIT: i want to try it with "EntityAIAttackMelee" , so my entity is attacking me now ..

 

but hes not using "tryability" ... idk why ..

 

hers the code:

 

public class EntityModelTest extends EntityMob{

public EntityModelTest(World worldIn) {
	super(worldIn);

	//AI Components
	this.tasks.addTask(1, new EntityAISwimming(this));
	this.tasks.addTask(2, new EntityAIAttackMelee(this, 1.0D, false));
	this.tasks.addTask(3, new EntityAIMoveTowardsRestriction(this, 1.0D));
	this.tasks.addTask(4, new EntityAIWander(this, 1.0D));
	this.tasks.addTask(5, new EntityAIWatchClosest(this, EntityPlayer.class, 20F));
	this.tasks.addTask(6, new EntityAILookIdle(this));

	this.targetTasks.addTask(0, new EntityAINearestAttackableTarget(this, EntityPlayer.class , true));

	this.setSize(0.6f, 1.5f);
}

@Override
protected void applyEntityAttributes()
{
  super.applyEntityAttributes();
	this.getEntityAttribute(SharedMonsterAttributes.FOLLOW_RANGE).setBaseValue(35.0D);
	this.getEntityAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.23000000417232513D);
	this.getEntityAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(4.0D);
	this.getEntityAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(1000.0D);
}

protected void addRandomArmor() {

	this.setItemStackToSlot(EntityEquipmentSlot.OFFHAND, new ItemStack(Items.SHIELD));
}

protected boolean isAIEnabled()
{
return true;
}

  public boolean onUpdate(EntityLivingBase mob)
  {
    if ((getAttackTarget() != null) && 
      ((getAttackTarget() instanceof EntityPlayer))) {
      tryAbility(mob, getAttackTarget());
    }
    return onUpdate(mob);
  }

  private long nextAbilityUse = 0L;
  
  private void tryAbility(EntityLivingBase mob, EntityLivingBase target)
  {
    if ((target == null) || (target.getRidingEntity() != null) || (!mob.canEntityBeSeen(target))) {
      return;
    }
    long time = System.currentTimeMillis();
    if ((time > this.nextAbilityUse) && 
      (mob.getDistanceToEntity(target) > 3.0F) && 
      (target.worldObj.canBlockSeeSky(new BlockPos(MathHelper.floor_double(target.posX), MathHelper.floor_double(target.posY), MathHelper.floor_double(target.posZ)))))
    {
      this.nextAbilityUse = (time + 15000L);
      mob.worldObj.addWeatherEffect(new EntityLightningBolt(mob.worldObj, target.posX, target.posY - 1.0D, target.posZ, false));
    }
  }

    protected boolean canDropLoot()
    {
        return false;
    }
    
    protected SoundEvent getAmbientSound() {
    	return null;
    }
    
    protected SoundEvent getHurtSound() {
    	return null;
    }
    
    protected SoundEvent getDeathSound() {
    	return null;
    }
    
	 @Override
	 public void readEntityFromNBT(NBTTagCompound nbt)
	 {
	  super.readEntityFromNBT(nbt);
	 }

	 @Override
	 public void writeEntityToNBT(NBTTagCompound nbt)
	 {
	  super.writeEntityToNBT(nbt);
	 }

}

 

if someone could help me :S

Link to comment
Share on other sites

EDIT :S

EntityAIAttackMelee is a vanilla class and wont call you private method of tryAbility.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

EDIT :S

EntityAIAttackMelee is a vanilla class and wont call you private method of tryAbility.

 

 

:/ hmm so how could i do it,

 

i did a class "OnUpdate" and he doesnt use it, or do i need to use it in a class?

 

(Please know that its my first entityclass with abilitys that i ever code, so for any help im very thankfull)

 

	  public boolean onUpdate(EntityLivingBase mob)
  {
    if ((getAttackTarget() != null) && 
      ((getAttackTarget() instanceof EntityPlayer))) {
      tryAbility(mob, getAttackTarget());
    }
    return onUpdate(mob);
  }

  private long nextAbilityUse = 0L;
  
  private void tryAbility(EntityLivingBase mob, EntityLivingBase target)
  {
    if ((target == null) || (target.getRidingEntity() != null) || (!mob.canEntityBeSeen(target))) {
      return;
    }
    long time = System.currentTimeMillis();
    if ((time > this.nextAbilityUse) && 
      (mob.getDistanceToEntity(target) > 0.0F));
      //(target.worldObj.canBlockSeeSky(new BlockPos(MathHelper.floor_double(target.posX), MathHelper.floor_double(target.posY), MathHelper.floor_double(target.posZ)))))
    {
      this.nextAbilityUse = (time + 0L);
      mob.worldObj.addWeatherEffect(new EntityLightningBolt(mob.worldObj, target.posX, target.posY - 1.0D, target.posZ, false));
    }
  }

 

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.