Skip to content

Instantly share code, notes, and snippets.

@gigaherz
Last active February 23, 2023 18:32
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gigaherz/7115024820f55717bc40a6e2247c6aca to your computer and use it in GitHub Desktop.
Save gigaherz/7115024820f55717bc40a6e2247c6aca to your computer and use it in GitHub Desktop.
How to add entity renderers and layer renderers

How to make a (in code) model for your entity/layer/blockentity/misc First, you need a layer location:

    public static ModelLayerLocation BELT_LAYER = new ModelLayerLocation(new ResourceLocation("minecraft:player"), "toolbelt_belt");

then, you need to register a model layer for it

        @SubscribeEvent
        public static void registerLayer(EntityRenderersEvent.RegisterLayerDefinitions event)
        {
            event.registerLayerDefinition(BELT_LAYER, ToolBeltLayer.BeltModel::createBodyLayer);
        }

the createBodyLayer there is what defines the cube definitions

        public static LayerDefinition createBodyLayer() {
            MeshDefinition meshdefinition = new MeshDefinition();
            PartDefinition partdefinition = meshdefinition.getRoot();
            partdefinition.addOrReplaceChild(BELT, CubeListBuilder.create()
                    .texOffs(0, 0)
                    .addBox(-5, 10, -3, 10, 4, 6), PartPose.ZERO);
            partdefinition.addOrReplaceChild(BUCKLE, CubeListBuilder.create()
                    .texOffs(10, 10)
                    .addBox(-2.5f, 9.5f, -3.5f, 5, 5, 1), PartPose.ZERO);
            partdefinition.addOrReplaceChild(LEFT_POCKET, CubeListBuilder.create()
                    .texOffs(0, 10)
                    .addBox(-2, 12, 5, 4, 4, 1), PartPose.rotation(0,-90,0));
            partdefinition.addOrReplaceChild(RIGHT_POCKET, CubeListBuilder.create()
                    .texOffs(0, 10)
                    .addBox(-2, 12, 5, 4, 4, 1), PartPose.rotation(0,90,0));
            return LayerDefinition.create(meshdefinition, 64, 32);
        }

and then when constructing the renderer/layer renderer, you bake the layers:

    public ToolBeltLayer(LivingEntityRenderer<T, M> owner)
    {
        super(owner);

        beltModel = new BeltModel(Minecraft.getInstance().getEntityModels().bakeLayer(ClientEvents.BELT_LAYER));
    }

which, get passed into the Model class, letting you query the parts:

    public static class BeltModel<T extends LivingEntity> extends EntityModel<T>
    {
        private static final String BELT = "belt";
        private static final String BUCKLE = "buckle";
        private static final String LEFT_POCKET = "left_pocket";
        private static final String RIGHT_POCKET = "right_pocket";
        private final ModelPart belt;
        private final ModelPart buckle;
        private final ModelPart left_pocket;
        private final ModelPart right_pocket;

        public BeltModel(ModelPart part) {
            super(RenderType::entityCutoutNoCull);
            this.belt = part.getChild(BELT);
            this.buckle = part.getChild(BUCKLE);
            this.left_pocket = part.getChild(LEFT_POCKET);
            this.right_pocket = part.getChild(RIGHT_POCKET);
        }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment