Minecraft Forge 1.7.2/1.7.10 Apply Attributes To A Custom Entity

Introduction

Living entities (i.e. mobs and animals) have some common attributes for health, speed, and such that you always control in your custom living entities.  Note that for a non-living entity (like a minecart), these are not applicable.

These are automatically synchronized between server and client for you, so no packet required.

Note that the Gamepedia wiki article (Minecraft Gamepedia Article On Entity Attributes) has an error becauseattackDamage is NOT available on all living entities.  It turns out that everything except attackDamage is available as I'll explain below.

The attributes are available through the SharedMonsterAttributes class, and they need to be both registered and a value set for your entity. 

Built-In Attributes

When you're extending classes to create your own custom entity, you should be aware of the full list of built-in attributes (as instantiated in SharedMonsterAttributes) and where they are registered and values set:
  • maxHealth: registered in EntityLivingBase, default value is 20.0D.
  • knockbackResistance: registered in EntityLivingBase, default value is 0.0D.
  • movementSpeed: registered in EntityLivingBaase, default value is 0.7D.
  • followRange: registered in EntityLiving, set to value of 16.0D (overriding the default value of 32.0D)
  • attackDamage: not registered except in actual mob entity classes, default value is 2.0D.  You'll probably need to register this (and set it) for your custom entity.
As you can see, if your custom entity extends EntityLiving or a subclass it will inherit all these.

Note: knockbackResistance is basically a percentage (from 0.0D to 1.0D).  A value of 1.0D means you cannot knock it back.

Custom Attributes

In theory, you can create your own additional custom attributes.  I have not tried to do this (I'll update the tutorial if I do).  I suspect that what you would do is instantiate IAttribute with something like:


public static final IAttribute myAttribute = (new RangedAttribute("generic.myAttribute", 
      MY_DEFAULT_VALUE, MY_MIN_VALUE, MY_MAX_VALUE))
      .setDescription("My Attribute").setShouldWatch(true);

I haven't confirmed it, but I believe that if theSetShouldWatch() is true then Minecraft should sync the server and client. 

Apply Entity Attributes In Your Entity Class

If you extend EntityLiving or a subclass, you should override the applyEntityAttributes() method to set up the attributes for your custom entity's health, movement speed, etc.  

Note that you do not need to call applyEntityAttributes() yourself as it is automatically called during the entity initialization.  In fact, if you call it from the constructor of your entity I find it causes an error.

In the applyEntityAttributes() I usually call the super.applyEntityAttributes() since this ensures most of the registrations (except attackDamage) are done.

I think it is good practice to explicitly set all the attributes, although of course it is allowable to leave them as they are in the superclass that you've extended.   For example:

// you don't have to call this as it is called automatically during EntityLiving subclass creation
@Override
protected void applyEntityAttributes()
{
    super.applyEntityAttributes(); 

    // standard attributes registered to EntityLivingBase
   getEntityAttribute(SharedMonsterAttributes.maxHealth).setBaseValue(10.0D);
   getEntityAttribute(SharedMonsterAttributes.movementSpeed).setBaseValue(0.20D);
   getEntityAttribute(SharedMonsterAttributes.knockbackResistance).setBaseValue(0.8D);
   getEntityAttribute(SharedMonsterAttributes.followRange).setBaseValue(16.0D);

    // need to register any additional attributes
   getAttributeMap().registerAttribute(SharedMonsterAttributes.attackDamage);
   getEntityAttribute(SharedMonsterAttributes.attackDamage).setBaseValue(4.0D);
}

Conclusion

Hope that helps explain living entity attributes.  Please let me know if you see anything I need to correct in this tutorial!

3 comments:

  1. Hello, I was reading this and I thought it had to do with the NBT Tags to add more attributes, let's say I want to create a mob that follows the RPG stats set (HP, MP, Attack, Defense, Speed, etc.) I would have to create those and apply them in a different wat to the default minecraft mobs/animals stats. So if you know what I am talking about can you do a tutorial about it please, or maybe a tutorial to comprehend better the NBT Tags since all of them are for command blocks and not actual coding.

    ReplyDelete
    Replies
    1. Hi, thanks for your comment. Yeah this tutorial was specifically about the Attributes that are an actual class in Minecraft. You are right that more generically you can also make "attributes" using NBT data. When I get time maybe I'll add to this tutorial to mention that option.

      Delete
    2. Yo im searching for this exact thing, any info on how to do it would be apreciated

      Delete