Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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();

}

}

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);
        }
    }

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
}

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.