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.
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.
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:
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:
Hope that helps explain living entity attributes. Please let me know if you see anything I need to correct in this tutorial!