Jump to content

[1.8] Custom entity attacking when attacked


expert700

Recommended Posts

I'm trying to get my custom entity to attack the player if the player attacks it, basically acting like a wolf. Adding EntityAIAttackOnCollide to the tasks as with a wolf didn't seem to work though.

Here's my code.

 

Sorry for lack of spoiler, doesn't seem to want to work for me.

 

package com.internetcraft.Entity;

 

import com.internetcraft.Item.Items;

import net.minecraft.block.Block;

import net.minecraft.entity.EntityAgeable;

import net.minecraft.entity.SharedMonsterAttributes;

import net.minecraft.entity.ai.*;

import net.minecraft.entity.passive.EntityAnimal;

import net.minecraft.entity.passive.EntityWolf;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.item.Item;

import net.minecraft.pathfinding.PathNavigateGround;

import net.minecraft.util.BlockPos;

import net.minecraft.world.World;

 

public class EntityDoge extends EntityAnimal

{

 

public EntityDoge(World worldIn)

{

super(worldIn);

this.setSize(1.0F, 1.0F);

((PathNavigateGround) this.getNavigator()).func_179690_a(true);

this.tasks.addTask(0, new EntityAISwimming(this));

this.tasks.addTask(1, new EntityAIAttackOnCollide(this, 1.0D, false));

// No mating for now

// this.tasks.addTask(2, new EntityAIMate(this, 1.0D));

//TODO; Doges follow doritos

this.tasks.addTask(3, new EntityAITempt(this, 1.25D, Items.dorito, false));

this.tasks.addTask(4, new EntityAIFollowParent(this, 1.25D));

this.tasks.addTask(5, new EntityAIWander(this, 1.0D));

this.tasks.addTask(6, new EntityAIWatchClosest(this, EntityPlayer.class, 6.0F));

this.tasks.addTask(7, new EntityAILookIdle(this));

}

 

protected void applyEntityAttributes()

{

super.applyEntityAttributes();

this.getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(10.0D);

        this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.6D);

}

 

public boolean canBreatheUnderwater()

{

return false;

}

 

public boolean isPushedByWater()

{

return false;

}

 

public void onLivingUpdate()

{

super.onLivingUpdate();

}

 

protected String getLivingSound()

{

return "mob.rabbit.idle";

}

 

protected String getHurtSound()

{

return "mob.wolf.hurt";

}

 

protected String getDeathSound()

{

return "mob.wolf.hurt";

}

 

protected void playStepSound(BlockPos p_180429_1_, Block p_180429_2_)

{

this.playSound("mob.cow.step", 0.15F, 1.0F);

}

 

protected float getSoundVolume()

{

return 0.4F;

}

 

protected Item getDropItem()

{

return com.internetcraft.Item.Items.bacon;

}

 

protected void dropFewItems(boolean recentlyhit, int modifier)

{

this.dropItem(com.internetcraft.Item.Items.bacon, 1);

}

 

public boolean interact(EntityPlayer player)

{

return super.interact(player);

}

 

public EntityDoge createChild(EntityAgeable ageable)

{

return new EntityDoge(this.worldObj);

}

 

public float getEyeHeight()

{

return this.height;

}

 

public boolean getCanSpawnHere()

{

return super.getCanSpawnHere();

}

}

Link to comment
Share on other sites

Override this method below to do whatever you want when the entity is attacked. The way the wolf does it for making it attack the player, (I'm assuming you want it to keep attacking after it's hit rather than hit for it) is using data watchers.

 

Method to override...

    /**
     * Called when the entity is attacked.
     */
    public boolean attackEntityFrom(DamageSource source, float amount)
    {
            if (entity != null && entity instanceof EntityPlayer)
            {
                //do stuff here
            }

            return super.attackEntityFrom(source, amount);
        }
    }

Link to comment
Share on other sites

Just FYI, null checks are automatically accounted for when using instanceof:

if (source.getEntity() instanceof EntityPlayer) {
    // The entity was an EntityPlayer instance, i.e. both not null and an EntityPlayer type
} else {
    // entity was either null or some other type that could not be cast to EntityPlayer
}

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.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.