tutorial:entity
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| tutorial:entity [2025/03/31 13:02] – new Identifier -> Identifier.of slainlight | tutorial:entity [2026/03/02 22:49] (current) – Mojmap and 1.21.11 cassiancc | ||
|---|---|---|---|
| Line 10: | Line 10: | ||
| Living Entities are Entities that have health and can deal damage. | Living Entities are Entities that have health and can deal damage. | ||
| There are various classes that branch off `LivingEntity` for different purposes, including: | There are various classes that branch off `LivingEntity` for different purposes, including: | ||
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| What you extend depends on what your needs and goals are. | What you extend depends on what your needs and goals are. | ||
| As you get further down the chain, the entity logic becomes more specific and curated to certain tasks. | As you get further down the chain, the entity logic becomes more specific and curated to certain tasks. | ||
| The two generic entity classes that come after '' | The two generic entity classes that come after '' | ||
| - | * '' | + | * '' |
| - | * '' | + | * '' |
| - | '' | + | '' |
| for pathfinding favor, and various AI tasks require this to operate. | for pathfinding favor, and various AI tasks require this to operate. | ||
| - | In this tutorial, we will look at creating a cube entity that extends '' | + | In this tutorial, we will look at creating a cube entity that extends '' |
| This entity will have a model & texture. Movement and mechanics will be covered in a future tutorial. | This entity will have a model & texture. Movement and mechanics will be covered in a future tutorial. | ||
| ===== Creating & Registering an Entity ===== | ===== Creating & Registering an Entity ===== | ||
| - | Create a class that extends '' | + | Create a class that extends '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| /* | /* | ||
| - | * Our Cube Entity extends | + | * Our Cube Entity extends |
| * | * | ||
| - | * LivingEntity has health and can deal damage. | + | |
| - | | + | |
| - | | + | |
| */ | */ | ||
| - | public class CubeEntity extends | + | public class CubeEntity extends |
| - | public CubeEntity(EntityType<? | + | public CubeEntity(EntityType<? |
| - | super(entityType, | + | super(entityType, |
| } | } | ||
| } | } | ||
| Line 57: | Line 57: | ||
| * Registers our Cube Entity under the ID " | * Registers our Cube Entity under the ID " | ||
| * | * | ||
| - | * The entity is registered under the SpawnGroup#CREATURE category, which is what most animals and passive/ | + | * The entity is registered under the MobCategory#CREATURE category, which is what most animals and passive/ |
| * It has a hitbox size of .75x.75, or 12 " | * It has a hitbox size of .75x.75, or 12 " | ||
| */ | */ | ||
| public static final EntityType< | public static final EntityType< | ||
| - | | + | |
| - | Identifier.of(" | + | Identifier.fromNamespaceAndPath(" |
| - | EntityType.Builder.create(CubeEntity:: | + | EntityType.Builder.create(CubeEntity:: |
| ); | ); | ||
| Line 76: | Line 76: | ||
| ===== Registering Entity Attributes ===== | ===== Registering Entity Attributes ===== | ||
| + | |||
| + | ~~REDIRECT> | ||
| **Attributes** define the properties of the mob: how much health does it have? How much damage does it do? Does it have any default armor points? | **Attributes** define the properties of the mob: how much health does it have? How much damage does it do? Does it have any default armor points? | ||
| - | Most vanilla entities have a static method that returns their attributes (such as '' | + | Most vanilla entities have a static method that returns their attributes (such as '' |
| - | Our custom entity doesn' | + | Our custom entity doesn' |
| Vanilla has a '' | Vanilla has a '' | ||
| Line 99: | Line 101: | ||
| * | * | ||
| * In 1.15, this was done by a method override inside the entity class. | * In 1.15, this was done by a method override inside the entity class. | ||
| - | * Most vanilla entities have a static method (eg. ZombieEntity#createZombieAttributes) for initializing their attributes. | + | * Most vanilla entities have a static method (eg. Zombie#createAttributes) for initializing their attributes. |
| */ | */ | ||
| FabricDefaultAttributeRegistry.register(CUBE, | FabricDefaultAttributeRegistry.register(CUBE, | ||
| Line 109: | Line 111: | ||
| The last requirement of an entity is a **Renderer**. Renderers define *what* the entity looks like, generally by providing a model. | The last requirement of an entity is a **Renderer**. Renderers define *what* the entity looks like, generally by providing a model. | ||
| - | '' | + | '' |
| and wants 3 parameters for the super constructor: | and wants 3 parameters for the super constructor: | ||
| * '' | * '' | ||
| Line 123: | Line 125: | ||
| * A renderer is used to provide an entity model, shadow size, and texture. | * A renderer is used to provide an entity model, shadow size, and texture. | ||
| */ | */ | ||
| - | public class CubeEntityRenderer extends | + | public class CubeEntityRenderer extends |
| - | public CubeEntityRenderer(EntityRendererFactory.Context context) { | + | public CubeEntityRenderer(EntityRendererProvider.Context context) { |
| - | super(context, | + | super(context, |
| } | } | ||
| @Override | @Override | ||
| - | | + | public CubeEntityRenderState createRenderState() { |
| - | return Identifier.of(" | + | return new CubeEntityRenderState(); |
| - | } | + | } |
| + | |||
| + | @Override | ||
| + | public Identifier | ||
| + | return Identifier.fromNamespaceAndPath(" | ||
| + | } | ||
| } | } | ||
| </ | </ | ||
| - | To register this renderer, use '' | + | To register this renderer, use '' |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | @Environment(EnvType.CLIENT) | ||
| public class EntityTestingClient implements ClientModInitializer { | public class EntityTestingClient implements ClientModInitializer { | ||
| - | public static final EntityModelLayer | + | public static final ModelLayerLocation |
| @Override | @Override | ||
| public void onInitializeClient() { | public void onInitializeClient() { | ||
| Line 148: | Line 154: | ||
| * Entity Renderers can also manipulate the model before it renders based on entity context (EndermanEntityRenderer# | * Entity Renderers can also manipulate the model before it renders based on entity context (EndermanEntityRenderer# | ||
| */ | */ | ||
| - | | + | |
| - | return new CubeEntityRenderer(context); | + | |
| - | }); | + | |
| - | // In 1.17, use EntityRendererRegistry.register (seen below) instead of EntityRendererRegistry.INSTANCE.register (seen above) | + | |
| - | EntityRendererRegistry.register(EntityTesting.CUBE, | + | |
| return new CubeEntityRenderer(context); | return new CubeEntityRenderer(context); | ||
| }); | }); | ||
| Line 158: | Line 160: | ||
| EntityModelLayerRegistry.registerModelLayer(MODEL_CUBE_LAYER, | EntityModelLayerRegistry.registerModelLayer(MODEL_CUBE_LAYER, | ||
| } | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ===== Creating a Render State ===== | ||
| + | |||
| + | The render state is a class that can be used to store data for the renderer. It is passed into the renderer and model, and can be used to store data about the entity, like the entity' | ||
| + | <code java [enable_line_numbers=" | ||
| + | |||
| + | public class CubeEntityRenderState extends EntityRenderState { | ||
| + | // You can add fields here to store data for the renderer and model. | ||
| } | } | ||
| </ | </ | ||
| Line 171: | Line 183: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public class CubeEntityModel extends EntityModel< | + | public class CubeEntityModel extends EntityModel< |
| private final ModelPart base; | private final ModelPart base; | ||
| public CubeEntityModel(ModelPart modelPart) { | public CubeEntityModel(ModelPart modelPart) { | ||
| - | this.base = modelPart.getChild(EntityModelPartNames.CUBE); | + | this.base = modelPart.getChild(PartNames.CUBE); |
| } | } | ||
| | | ||
| Line 189: | Line 201: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public class CubeEntityModel extends EntityModel< | + | public class CubeEntityModel extends EntityModel< |
| private final ModelPart base; | private final ModelPart base; | ||
| Line 198: | Line 210: | ||
| | | ||
| // You can use BlockBench, make your model and export it to get this method for your entity model. | // You can use BlockBench, make your model and export it to get this method for your entity model. | ||
| - | | + | public static |
| - | | + | MeshDefinition |
| - | | + | PartDefinition |
| - | modelPartData.addChild(EntityModelPartNames.CUBE, | + | modelPartData.addOrReplaceChild(PartNames.CUBE, |
| - | return | + | return |
| - | } | + | } |
| </ | </ | ||
| Our entity model now has a single cube that is 12x12x12 wide (75% of a block) centered around 0, 0, 0. | Our entity model now has a single cube that is 12x12x12 wide (75% of a block) centered around 0, 0, 0. | ||
| - | '' | + | '' |
| - | '' | + | |
| - | than the entity hitbox, so we translate the model down to account for this. | + | |
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public class CubeEntityModel extends EntityModel< | + | public class CubeEntityModel extends EntityModel< |
| | | ||
| private final ModelPart base; | private final ModelPart base; | ||
| Line 218: | Line 228: | ||
| public CubeEntityModel() [...] | public CubeEntityModel() [...] | ||
| | | ||
| - | public static | + | public static |
| @Override | @Override | ||
| - | public void setAngles(CubeEntity | + | public void setupAnim(CubeEntityRenderState |
| - | } | + | |
| - | + | ||
| - | @Override | + | |
| - | public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | + | |
| - | ImmutableList.of(this.base).forEach((modelRenderer) -> { | + | |
| - | modelRenderer.render(matrices, | + | |
| - | }); | + | |
| } | } | ||
| } | } | ||
| </ | </ | ||
| - | To complete our model, we need to add a texture file. The default texture size is 64 wide and 32 tall; | + | To complete our model, we need to add a texture file. The default texture size is 64 pixels |
| - | you can change this by adding a return of your texturedModelData | + | you can change this by adding a return of your LayerDefinition with a custom width and height. |
| We will set it to 64x64 for our texture: | We will set it to 64x64 for our texture: | ||
| Line 240: | Line 243: | ||
| <code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
| - | public class CubeEntityModel extends EntityModel< | + | public class CubeEntityModel extends EntityModel< |
| private final ModelPart base; | private final ModelPart base; | ||
| Line 246: | Line 249: | ||
| [...] | [...] | ||
| | | ||
| - | public static | + | public static |
| [...] | [...] | ||
| - | return | + | return |
| } | } | ||
| Line 273: | Line 276: | ||
| {{https:// | {{https:// | ||
| - | **NOTE:** If your entity does not extend '' | + | **NOTE:** If your entity does not extend '' |
| ===== Adding tasks & activities ===== | ===== Adding tasks & activities ===== | ||
| To add activities see [[tutorial: | To add activities see [[tutorial: | ||
tutorial/entity.txt · Last modified: 2026/03/02 22:49 by cassiancc