Changes

2,581 bytes added ,  21:49, 6 October 2022
Correct mistakes in entity attributes
Line 71: Line 71:     
== Entity Attributes ==
 
== Entity Attributes ==
Since 1.17 and later versions, you **MUST** define attributes if your entity class extends <code>LivingEntity</code> or any other class that inherits from it. In Attributes, you can define:
+
All <code>LivingEntity</code>s must have an associated <code>AttributeSupplier</code>, which, as named, supplies the default settings for all the attributes an entity has. An <code>Attribute</code> represents a syncable piece of data representing a characteristic, quality, or feature of an entity. Vanilla and Forge both define attributes that an entity can use within <code>Attributes</code> and <code>ForgeMod</code>, respectively.
   −
* Max Health
+
To attach an attribute to a new entity, you must create an event listener for the <code>EntityAttributeCreationEvent</code> on the '''mod''' event bus and call <code>#put</code>, providing the <code>EntityType<T></code> and the attribute supplier.
* Movement Speed
  −
* Attack Damage
     −
and more...
+
{{Tabs/Code Snippets
 +
|java=// On the mod event bus
 +
public void newEntityAttributes(EntityAttributeCreationEvent event) {
 +
    event.put(EXAMPLE_ENTITY_TYPE.get(),
 +
        AttributeSupplier.builder()
 +
            .add(Attributes.MAX_HEALTH) // Sets max health to default value 20
 +
            .add(Attributes.KNOCKBACK_RESISTANCE, 4.0D) // Sets knockback resistance to 4
 +
            // ...
 +
    );
 +
}
 +
}}
   −
In your Entity class, create a static method that creates your attributes:
+
You can similarly append new attributes or modify existing attributes for existing entities by listening to <code>EntityAttributeModificationEvent</code> on the '''mod''' event bus and call <code>#add</code>, supplying the <code>EntityType<T></code>, the <code>Attribute</code>, and optionally the value if you do not want the default.
<syntaxhighlight lang="java">
+
 
public static AttributeSupplier.Builder createAttributes()
+
{{Tabs/Code Snippets
{
+
|java=// On the mod event bus
      return createHostileAttributes().add(Attributes.MAX_HEALTH, 40.0F).add(Attributes.ATTACK_DAMAGE, 10.0F);
+
public void existingEntityAttributes(EntityAttributeModificationEvent event) {
 +
    if (!event.has(EntityType.CREEPER, EXAMPLE_ATTRIBUTE.get())) {
 +
        event.add(EntityType.CREEPER,
 +
            EXAMPLE_ATTRIBUTE.get() // Applies new attribute to creeper
 +
        );
 +
    }
 
}
 
}
</syntaxhighlight>
+
}}
 +
 
 +
=== Reusable Attribute Suppliers ===
 +
 
 +
<code>LivingEntity</code> and its subclasses require specific attributes to be defined on the entity (<code>LivingEntity</code> requires max health, knockback resistance, movement speed, armor, armor toughness, swim speed, nametag distance, entity gravity, and step height addition). To make it such that subclasses can use the previously defined attribute suppliers while still being able to add or override the specified super value, a static method is used to hold the builder, which is then referenced in a later subclass. The resulting method can then be passed into the <code>EntityAttributeCreationEvent</code>.
 +
 
 +
{{Tabs/Code Snippets
 +
|java=
 +
// In your LivingEntity subclass
 +
public static AttributeSupplier.Builder createExampleAttributes() {
 +
    return LivingEntity.createLivingAttributes().add(Attributes.KNOCKBACK_RESISTANCE, 4.0D);
 +
}
 +
 
 +
// In some separate class on the mod event bus
 +
public void newEntityAttributes(EntityAttributeCreationEvent event) {
 +
    event.put(EXAMPLE_ENTITY_TYPE.get(), ExampleEntity.createExampleAttributes().build());
 +
}
 +
}}
 +
 
 +
{{Tip|Most mobs use this static method attribute builder, so if you are subclassing a different entity class (e.g., <code>Mob</code>), you should look in the superclasses for the associated static method to build the attributes from (e.g., <code>Mob#createMobAttributes</code>).}}
    
== Natural Spawning ==
 
== Natural Spawning ==
 
To make entities naturally spawn, they must be added to the spawn list for each biome that the entity should spawn in. This can be achieved with a [[Biome Modifiers#Add Spawns|<code>forge:add_spawns</code> biome modifier]].
 
To make entities naturally spawn, they must be added to the spawn list for each biome that the entity should spawn in. This can be achieved with a [[Biome Modifiers#Add Spawns|<code>forge:add_spawns</code> biome modifier]].