Changes

3,159 bytes added ,  21:49, 6 October 2022
Correct mistakes in entity attributes
Line 45: Line 45:  
After creating and configuring the entity type builder, it must be built. This requires the modid and name of the entity type. For an entity type with the name "example_monster" in the mod "examplemod", this would look like <code>builder.build("examplemod:example_monster")</code>.
 
After creating and configuring the entity type builder, it must be built. This requires the modid and name of the entity type. For an entity type with the name "example_monster" in the mod "examplemod", this would look like <code>builder.build("examplemod:example_monster")</code>.
   −
===Tying It Together===
+
=== Tying It Together ===
Given the above information, a basic EntityType might look like the following:{{Tabs/Code Snippets
+
Given the above information, a basic EntityType might look like the following:
 +
{{Tabs/Code Snippets
 
|java=EntityType.Builder.of(ExampleMonsterEntity::new, MobCategory.MONSTER)
 
|java=EntityType.Builder.of(ExampleMonsterEntity::new, MobCategory.MONSTER)
 
     .sized(1.0F, 2.0F)
 
     .sized(1.0F, 2.0F)
Line 52: Line 53:  
     .updateInterval(1)
 
     .updateInterval(1)
 
     .build("examplemod:example_monster")
 
     .build("examplemod:example_monster")
}}To function, this example requires that an entity class of the name <code>ExampleMonsterEntity</code> exist along with the entity type being [[Registration|registered]].
+
}}
 +
To function, this example requires that an entity class of the name <code>ExampleMonsterEntity</code> exist along with the entity type being [[Registration|registered]].
    
== Entity Class ==
 
== Entity Class ==
Line 67: Line 69:     
To define custom behavior, methods must be overriden. Your IDE should have a feature to list all possible methods to override, although some common ones might be: <code>customServerAiStep</code> for server AI, <code>tick</code> for code to execute each tick on client/server, and <code>registerGoals</code> to setup AI goals.
 
To define custom behavior, methods must be overriden. Your IDE should have a feature to list all possible methods to override, although some common ones might be: <code>customServerAiStep</code> for server AI, <code>tick</code> for code to execute each tick on client/server, and <code>registerGoals</code> to setup AI goals.
 +
 +
== Entity Attributes ==
 +
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.
 +
 +
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.
 +
 +
{{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
 +
            // ...
 +
    );
 +
}
 +
}}
 +
 +
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.
 +
 +
{{Tabs/Code Snippets
 +
|java=// On the mod event bus
 +
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
 +
        );
 +
    }
 +
}
 +
}}
 +
 +
=== 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]].