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/06/18 12:12] – Update to 1.17 redgrapefruit | tutorial:entity [2025/03/31 13:02] (current) – new Identifier -> Identifier.of slainlight | ||
|---|---|---|---|
| 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 '' | ||
| Line 61: | Line 61: | ||
| */ | */ | ||
| public static final EntityType< | public static final EntityType< | ||
| - | | + | |
| - | | + | Identifier.of(" |
| - | | + | |
| ); | ); | ||
| Line 126: | Line 126: | ||
| public CubeEntityRenderer(EntityRendererFactory.Context context) { | public CubeEntityRenderer(EntityRendererFactory.Context context) { | ||
| - | super(context, | + | super(context, |
| } | } | ||
| @Override | @Override | ||
| public Identifier getTexture(CubeEntity entity) { | public Identifier getTexture(CubeEntity entity) { | ||
| - | return | + | return Identifier.of(" |
| } | } | ||
| } | } | ||
| Line 140: | Line 140: | ||
| @Environment(EnvType.CLIENT) | @Environment(EnvType.CLIENT) | ||
| public class EntityTestingClient implements ClientModInitializer { | public class EntityTestingClient implements ClientModInitializer { | ||
| + | public static final EntityModelLayer MODEL_CUBE_LAYER = new EntityModelLayer(Identifier.of(" | ||
| @Override | @Override | ||
| public void onInitializeClient() { | public void onInitializeClient() { | ||
| Line 151: | Line 151: | ||
| return new CubeEntityRenderer(context); | 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); | ||
| + | }); | ||
| + | | ||
| + | EntityModelLayerRegistry.registerModelLayer(MODEL_CUBE_LAYER, | ||
| } | } | ||
| } | } | ||
| Line 161: | Line 167: | ||
| 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=" | ||
| Line 177: | Line 175: | ||
| private final ModelPart base; | private final ModelPart base; | ||
| - | public CubeEntityModel() { | + | public CubeEntityModel(ModelPart modelPart) { |
| - | base = new ModelPart(this, 0, 0); | + | |
| } | } | ||
| | | ||
| Line 185: | Line 183: | ||
| </ | </ | ||
| - | 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: | ||
| Line 194: | Line 194: | ||
| public CubeEntityModel() { | public CubeEntityModel() { | ||
| - | | + | |
| - | base.addCuboid(-6, | + | |
| } | } | ||
| | | ||
| - | | + | |
| - | } | + | public static TexturedModelData getTexturedModelData() { |
| + | ModelData modelData = new ModelData(); | ||
| + | ModelPartData modelPartData = modelData.getRoot(); | ||
| + | modelPartData.addChild(EntityModelPartNames.CUBE, | ||
| + | return TexturedModelData.of(modelData, | ||
| + | } | ||
| </ | </ | ||
| Line 213: | Line 217: | ||
| | | ||
| public CubeEntityModel() [...] | public CubeEntityModel() [...] | ||
| + | | ||
| + | public static TexturedModelData getTexturedModelData() [...] | ||
| @Override | @Override | ||
| public void setAngles(CubeEntity entity, float limbAngle, float limbDistance, | public void setAngles(CubeEntity entity, float limbAngle, float limbDistance, | ||
| - | |||
| } | } | ||
| @Override | @Override | ||
| public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | public void render(MatrixStack matrices, VertexConsumer vertices, int light, int overlay, float red, float green, float blue, float alpha) { | ||
| - | | + | |
| - | matrices.translate(0, 1.125, 0); | + | |
| - | + | }); | |
| - | // render cube | + | |
| - | base.render(matrices, | + | |
| } | } | ||
| } | } | ||
| Line 231: | Line 234: | ||
| 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 wide and 32 tall; | ||
| - | you can change this by changing '' | + | you can change this by adding a return of your texturedModelData |
| We will set it to 64x64 for our texture: | We will set it to 64x64 for our texture: | ||
| Line 241: | Line 244: | ||
| private final ModelPart base; | private final ModelPart base; | ||
| - | public | + | |
| - | this.textureHeight = 64; | + | |
| - | this.textureWidth = 64; | + | |
| - | + | ||
| [...] | [...] | ||
| + | return TexturedModelData.of(modelData, | ||
| } | } | ||
| Line 254: | Line 257: | ||
| ===== 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:// | ||
| Line 260: | Line 276: | ||
| ===== Adding tasks & activities ===== | ===== Adding tasks & activities ===== | ||
| - | To add activities see [[: | + | To add activities see [[tutorial: |
tutorial/entity.1624018376.txt.gz · Last modified: 2021/06/18 12:12 by redgrapefruit