tutorial:entity
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| tutorial:entity [2021/07/15 18:41] – [Registering Entity Renderer] Updating the page for recent versions. logdawg970 | tutorial:entity [2026/03/02 22:49] (current) – Mojmap and 1.21.11 cassiancc | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ===== Creating an Entity ===== | ===== Creating an Entity ===== | ||
| - | //The source code for this project can be found [[https:// | + | //The source code for this project can be found [[https:// |
| Entities are a movable object in a world with logic attached to them. A few examples include: | Entities are a movable object in a world with logic attached to them. A few examples include: | ||
| 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 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.fromNamespaceAndPath(" |
| - | | + | |
| ); | ); | ||
| 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 |
| - | return new Identifier(" | + | return new CubeEntityRenderState(); |
| - | } | + | } |
| + | |||
| + | @Override | ||
| + | public | ||
| + | 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); | return new CubeEntityRenderer(context); | ||
| }); | }); | ||
| + | | ||
| + | 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 161: | Line 179: | ||
| Standard models define " | Standard models define " | ||
| - | initialize them in the constructor, | + | initialize them in the constructor, obtain data in the '' |
| Note that '' | Note that '' | ||
| - | |||
| - | '' | ||
| - | * the current model instance | ||
| - | * textureOffsetU as an '' | ||
| - | * textureOffsetV as an '' | ||
| - | |||
| - | Texture offsets provide the location of the model' | ||
| - | Our entity is a single cube, so the base '' | ||
| <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() { | + | public CubeEntityModel(ModelPart modelPart) { |
| - | base = new ModelPart(this, 0, 0); | + | |
| } | } | ||
| | | ||
| Line 185: | Line 195: | ||
| </ | </ | ||
| - | After creating a part, we need to add a shape to it. | + | After creating a part, we need to add a shape to it. |
| + | To do so, we must add a child to the root. The texture location for the new part is located in '' | ||
| + | the offset for it is located in the first 3 numbers of '' | ||
| Note that the origin of a model starts at the corner, so you will need to offset the part to center it: | Note that the origin of a model starts at the corner, so you will need to offset the part to center it: | ||
| <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() { | public CubeEntityModel() { | ||
| - | | + | |
| - | base.addCuboid(-6, | + | |
| } | } | ||
| | | ||
| - | | + | |
| - | } | + | public static LayerDefinition getTexturedModelData() { |
| + | MeshDefinition modelData = new MeshDefinition(); | ||
| + | PartDefinition modelPartData = modelData.getRoot(); | ||
| + | modelPartData.addOrReplaceChild(PartNames.CUBE, | ||
| + | return LayerDefinition.create(modelData, | ||
| + | } | ||
| </ | </ | ||
| 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; | ||
| | | ||
| public CubeEntityModel() [...] | public CubeEntityModel() [...] | ||
| + | | ||
| + | public static LayerDefinition getTexturedModelData() [...] | ||
| @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) { | + | |
| - | // translate model down | + | |
| - | matrices.translate(0, | + | |
| - | + | ||
| - | // render cube | + | |
| - | base.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 changing '' | + | 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 237: | 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; | ||
| - | public | + | |
| - | this.textureHeight = 64; | + | |
| - | this.textureWidth = 64; | + | |
| - | + | ||
| [...] | [...] | ||
| + | return LayerDefinition.create(modelData, | ||
| } | } | ||
| Line 254: | Line 260: | ||
| ===== Spawning your Entity ===== | ===== Spawning your Entity ===== | ||
| + | Be sure to add your client entrypoint to fabric.mod.json. | ||
| + | You can do this like so: | ||
| + | <code json> | ||
| + | |||
| + | " | ||
| + | " | ||
| + | " | ||
| + | ], | ||
| + | " | ||
| + | " | ||
| + | ] | ||
| + | }, | ||
| + | </ | ||
| You can spawn your entity by typing ''/ | You can spawn your entity by typing ''/ | ||
| {{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 [[: | + | To add activities see [[tutorial: |
tutorial/entity.1626374496.txt.gz · Last modified: 2021/07/15 18:41 by logdawg970