Held Item Layer for Your Custom Model! 1.20.1 only

Started by FrancisPlayzz446 on

Topic category: User side tutorials

Joined Apr 2025
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Held Item Layer for Your Custom Model! 1.20.1 only


Hello There! This tutorial will demonstrate how to add a held item layer to custom entities, in short, this tutorial explains how to make custom entities hold items.

In this tutorial, we will add the item layer to both of the entity's hands, but you can add just one if you wish

 

First Step: Define the entity's arms
The first thing we need to do is define the entity's arms, to do this, we will open the model code and add the following code:

public void translateToHand(HumanoidArm arm, PoseStack pose) {
       this.Body.translateAndRotate(pose);
       if (arm == HumanoidArm.RIGHT) {
           this.RightArm.translateAndRotate(pose);
       } else {
           this.LeftArm.translateAndRotate(pose);
       }
   }

 

 

Basically, this will define the animation of the item and the relative position of the item in the model (the item will be rendered at the pivot point of the arm, but we will change its position later)

Don't forget to change "RightArm" and "LeftArm" to the group name referring to these arms in your model

Example:
If the group referring to your model's right arm is called "RightHand", then the code would be:

this.RightHand.translateAndRotate(pose);

 

Second Step:
Now we will import the model to mcreator and define the animations, make your entity and then lock the code, then we will open the entity render code to modify

We will start by adding the layer, locate the Renderer method and add the following line:

this.addLayer(new HeldItemLayer<>(this));


Don't forget to change "Modelruby" to your entity's model id

 

Now, we will define the rendering of the layer and item, add the following code:

 

@OnlyIn(Dist.CLIENT)
   public static class HeldItemLayer<T extends LivingEntity, M extends EntityModel<T>> extends RenderLayer<T, M> {
       public HeldItemLayer(RenderLayerParent<T, M> layer) {
           super(layer);
       }

       public void render(@NotNull PoseStack pose, @NotNull MultiBufferSource source, int i1, T entity, float f1, float f2, float f3, float f4, float f5, float f6) {
           ItemStack RightArmItem = entity.getItemBySlot(EquipmentSlot.MAINHAND);
           if (!RightArmItem.isEmpty()) {
               pose.pushPose();
               if (this.getParentModel() instanceof Modelruby<? extends Entity> model)
                   model.translateToHand(HumanoidArm.RIGHT, pose);
               pose.mulPose(Axis.XP.rotationDegrees(-90.0F));
               pose.mulPose(Axis.YP.rotationDegrees(180.0F));
               pose.translate(0.0D, 0.0D, 0.0D);
               Minecraft.getInstance().gameRenderer.itemInHandRenderer.renderItem(entity, right, ItemDisplayContext.THIRD_PERSON_RIGHT_HAND, false, pose, source, i1);
               pose.popPose();
           }
           ItemStack LeftArmItem = entity.getItemBySlot(EquipmentSlot.OFFHAND);
           if (!LeftArmItem.isEmpty()) {
               pose.pushPose();
               if (this.getParentModel() instanceof Modelruby<? extends Entity> model)
                   model.translateToHand(HumanoidArm.LEFT, pose);
               pose.mulPose(Axis.XP.rotationDegrees(-90.0F));
               pose.mulPose(Axis.YP.rotationDegrees(180.0F));
               pose.translate(0.0D, 0.0D, 0.0D);
               Minecraft.getInstance().gameRenderer.itemInHandRenderer.renderItem(entity, left, ItemDisplayContext.THIRD_PERSON_LEFT_HAND, false, pose, source, i1);
               pose.popPose();
           }
       }
   }

 

Don't forget to change "Modelruby" to your entity's model id

 

Third Step: Changing item position
Now we will change the position where the item is rendered, to do this, we will find 'pose.translate'

However, it is necessary to take into account that the item is rotated, this means that the position of the item is defined by different coordinates, so the numbers in pose.translate will behave like "X, Y and Z" instead of "X,Z and Y"


It is possible to notice that we have 3 numbers, each number represents a position, x, y, z

 

And that's it, your entity will now render the items in your hands, I hope this tutorial helped you !