Jump to content

How do I make my mob attack attackers like wolves?


UntouchedWagons

Recommended Posts

This is my entity's code:

 

package untouchedwagons.minecraft.mindcrackermobs.entity.mindcrackers;

import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

import java.util.Random;

public class EntityCustomMob extends EntityAnimal {
    public EntityCustomMob(World world) {
        super(world);

        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIWander(this, 1D));
        this.tasks.addTask(3, new EntityAILookIdle(this));
        this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1, true));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));

        this.isImmuneToFire = true;
    }

    @Override
    public boolean attackEntityFrom(DamageSource p_70097_1_, float p_70097_2_)
    {
        if (this.isEntityInvulnerable())
            return false;

        Entity damage_source = p_70097_1_.getEntity();

        if (damage_source instanceof EntityPlayer)
        {
            this.setRevengeTarget((EntityPlayer) damage_source);
            this.attackingPlayer = (net.minecraft.entity.player.EntityPlayer) damage_source;
            this.entityToAttack = damage_source;
        }

        return super.attackEntityFrom(p_70097_1_, p_70097_2_);
    }

    @Override
    public void applyEntityAttributes()
    {
        super.applyEntityAttributes();

        this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(16.0D);
    }
}

 

I see that your entity has an 'updateAITasks' method which does some stuff if the entity is angry. I guess I need to implement that stuff

I like trains.

Link to comment
Share on other sites

I've managed to get my mob to path to the attacker but never actually does any damage:

 

package untouchedwagons.minecraft.mindcrackermobs.entity.mindcrackers;

import net.minecraft.enchantment.EnchantmentHelper;
import net.minecraft.entity.Entity;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.ai.EntityAIAttackOnCollide;
import net.minecraft.entity.ai.EntityAIHurtByTarget;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

import java.util.Random;

public class EntityCustomMob extends EntityAnimal {
    public EntityCustomMob(World world) {
        super(world);

        this.tasks.addTask(0, new EntityAISwimming(this));
        this.tasks.addTask(1, new EntityAIWander(this, 1D));
        this.tasks.addTask(3, new EntityAILookIdle(this));
        this.tasks.addTask(2, new EntityAIAttackOnCollide(this, EntityPlayer.class, 1, true));
        this.targetTasks.addTask(1, new EntityAIHurtByTarget(this, false));

        this.isImmuneToFire = true;
    }

    @Override
    public boolean attackEntityFrom(DamageSource p_70097_1_, float p_70097_2_)
    {
        if (this.isEntityInvulnerable())
            return false;

        boolean state = super.attackEntityFrom(p_70097_1_, p_70097_2_);

        Entity damage_source = p_70097_1_.getEntity();

        if (damage_source instanceof EntityPlayer)
        {
            this.setTarget(damage_source);
            this.setRevengeTarget((EntityPlayer)damage_source);
        }

        return state;
    }

    @Override
    public void applyEntityAttributes()
    {
        super.applyEntityAttributes();

        this.getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(6.0D);
    }

    @Override
    public boolean attackEntityAsMob(Entity target)
    {
        float damage = (float)this.getEntityAttribute(SharedMonsterAttributes.attackDamage).getAttributeValue();
        int knockback = 0;

        if (target instanceof EntityLivingBase)
        {
            damage += EnchantmentHelper.getEnchantmentModifierLiving(this, (EntityLivingBase)target);
            knockback += EnchantmentHelper.getKnockbackModifier(this, (EntityLivingBase)target);
        }

        boolean flag = target.attackEntityFrom(DamageSource.causeMobDamage(this), damage);

        if (flag)
        {
            if (knockback > 0)
            {
                target.addVelocity((double) (-MathHelper.sin(this.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback * 0.5F),
                        0.1D,
                        (double) (MathHelper.cos(this.rotationYaw * (float) Math.PI / 180.0F) * (float) knockback * 0.5F)
                );

                this.motionX *= 0.6D;
                this.motionZ *= 0.6D;
            }

            int j = EnchantmentHelper.getFireAspectModifier(this);

            if (j > 0)
            {
                target.setFire(j * 4);
            }

            if (target instanceof EntityLivingBase)
            {
                EnchantmentHelper.func_151384_a((EntityLivingBase)target, this);
            }

            EnchantmentHelper.func_151385_b(this, target);
        }

        return flag;
    }
}

I like trains.

Link to comment
Share on other sites

There's another AI task you need to use (I'm not at my computer now so I don't know what it is) and make sure attackEntityAsMob returns false if the mob isn't angry, or it will hurt you to run into it.

 

Okay

 

For the problem of an entity trying to attack without doing damage, I have a tutorial for that: http://jabelarminecraft.blogspot.com/p/minecraft-forge-1721710-custom-entity.html

 

Cool I'll take a look at that

I like trains.

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.