Jump to content

How to render items and armor on your humanoid mob


Recommended Posts

package com.example.examplemod.entity.renderer
import com.example.examplemod.ExampleMod;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.model.Model;
import net.minecraft.client.model.geom.ModelLayers;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.entity.HumanoidMobRenderer;
import net.minecraft.client.renderer.entity.MobRenderer;
import net.minecraft.client.renderer.entity.layers.HumanoidArmorLayer;
import net.minecraft.client.renderer.entity.layers.ItemInHandLayer;
import net.minecraft.resources.ResourceLocation;
  
  
  public class ExampleEntityRenderer<Type extends ExampleEntity> extends HumanoidMobRenderer<Type, ExampleEntityModel<Type>> {
    private static final ResourceLocation TEXTURE = new ResourceLocation(ExampleMod.MOD_ID,
        "textures/entity/example/exampleentity.png");

public ExampleEntityRenderer(EntityRendererProvider.Context context) {
        super(context, new ExampleEntityModel<>(context.bakeLayer(ExampleEntityModel.LAYER_LOCATION)), 0.6f);
        this.addLayer(new ItemInHandLayer<>(this));
        this.addLayer(new HumanoidArmorLayer<>(this,
                new HumanoidModel(context.bakeLayer(ModelLayers.PLAYER_INNER_ARMOR)),
                new HumanoidModel(context.bakeLayer(ModelLayers.PLAYER_OUTER_ARMOR))));
}

@Override
public ResourceLocation getTextureLocation(Type entity) {
        return TEXTURE;
        }
}

If your entity model extends the humanoid model and has the appropriate methods and items in its equipment slots, it will hopefully render them

Link to comment
Share on other sites

Ignoring the mismatched format, this doesn't really explain anything about what is needed. First, you have a bunch of random classes which you don't define: ExampleEntityModel, the layer location, model layers, etc. Even if they are not relevant, you should just hide them behind a `//...` so they don't detract from your main point. For example, what about registering the layer definitions? the entity renderer? Anything that is not explained should be mentioned to use the same method as usual. You should also explain why adding those two layers would do what you needed. Additionally, you need to explain when a different model layer should be used, since not all mobs will have the exact same dimensions as the player (e.g., piglin). 

2 hours ago, EaSports69 said:
package com.example.examplemod.entity.renderer

This will crash, it needs a semicolon.

2 hours ago, EaSports69 said:
        this.addLayer(new ItemInHandLayer<>(this));

This is already added for HumanoidMobRenderer, so it is redundant.

2 hours ago, EaSports69 said:
this.addLayer(new HumanoidArmorLayer<>(this,
                new HumanoidModel(context.bakeLayer(ModelLayers.PLAYER_INNER_ARMOR)),
                new HumanoidModel(context.bakeLayer(ModelLayers.PLAYER_OUTER_ARMOR))));

This will crash, you need to pass in a ModelManager from the context. Additionally, the type parameter for HumanoidModel should be inferred and not just removed.

 

Link to comment
Share on other sites

I use the exampleentity model as it's basically, an example. also in order for it to work(sorry for not clarifying again), you need to add in your Entity class

 the

    protected void populateDefaultEquipmentSlots(DifficultyInstance difficultyInstance) {
        super.populateDefaultEquipmentSlots(difficultyInstance);
        this.setItemSlot(EquipmentSlot.MAINHAND, new ItemStack(Items.DIAMOND_SWORD.get()));
        this.setItemSlot(EquipmentSlot.HEAD, new ItemStack(Items.DIAMOND_HELMET.get()));
        this.setItemSlot(EquipmentSlot.CHEST, new ItemStack(Items.DIAMOND_CHESTPLATE.get()));
        this.setItemSlot(EquipmentSlot.LEGS, new ItemStack(Items.DIAMOND_LEGGINGS.get()));
        this.setItemSlot(EquipmentSlot.FEET, new ItemStack(Items.DIAMOND_BOOTS.get()));
    }

//This adds the items in your entity's respective slots, here I'm using for an example diamond items

 In your model, you need to extend the HumanoidModel class, and it should look like this

 

public class PigmanEntityModel<Type extends ExampleEntity> extends HumanoidModel<Type> {
	// This extends the humanoid model in your class and makes possible the animations and the ability to hold items
	public static final ModelLayerLocation LAYER_LOCATION =
			new ModelLayerLocation(new ResourceLocation(ExampleMod.MOD_ID, "example"), "main");

	public ExampleEntityModel(ModelPart modelPart) {
		super(modelPart);

	}

	public static LayerDefinition createBodyLayer() {
		MeshDefinition meshdefinition = new MeshDefinition();
		PartDefinition partdefinition = meshdefinition.getRoot();

		PartDefinition head = partdefinition.addOrReplaceChild("head", CubeListBuilder.create().texOffs(0, 0).addBox(-4.0F, -8.0F, -4.0F, 8.0F, 8.0F, 8.0F, new CubeDeformation(0.0F)), PartPose.offset(0.0F, 0.0F, 0.0F));
		PartDefinition hat = partdefinition.addOrReplaceChild("hat", CubeListBuilder.create().texOffs(32, 0).addBox(-4.0F, -8.0F, -4.0F, 8.0F, 8.0F, 8.0F, new CubeDeformation(0.0F).extend(0.5F)), PartPose.offset(0.0F, 0.0F, 0.0F));
		PartDefinition body = partdefinition.addOrReplaceChild("body", CubeListBuilder.create().texOffs(16, 16).addBox(-4.0F, 0.0F, -2.0F, 8.0F, 12.0F, 4.0F, new CubeDeformation(0.0F)), PartPose.offset(0.0F, 0.0F, 0.0F));
		PartDefinition right_arm = partdefinition.addOrReplaceChild("right_arm", CubeListBuilder.create().texOffs(40, 16).addBox(-3.0F, -2.0F, -2.0F, 4.0F, 12.0F, 4.0F, new CubeDeformation(0.0F)), PartPose.offset(-5.0F, 2.0F, 0.0F));
		PartDefinition left_arm = partdefinition.addOrReplaceChild("left_arm", CubeListBuilder.create().texOffs(32, 48).mirror().addBox(-1.0F, -2.0F, -2.0F, 4.0F, 12.0F, 4.0F, new CubeDeformation(0.0F)), PartPose.offset(5.0F, 2.0F, 0.0F));
		PartDefinition right_leg = partdefinition.addOrReplaceChild("right_leg", CubeListBuilder.create().texOffs(0, 16).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, new CubeDeformation(0.0F)), PartPose.offset(-1.9F, 12.0F, 0.0F));
		PartDefinition left_leg = partdefinition.addOrReplaceChild("left_leg", CubeListBuilder.create().texOffs(16, 48).addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, new CubeDeformation(0.0F)), PartPose.offset(1.9F, 12.0F, 0.0F));
//This is if you use the player model only
		return LayerDefinition.create(meshdefinition, 64, 64);
	}

	public void prepareMobModel(Type p_103793_, float p_103794_, float p_103795_, float p_103796_) {
		this.rightArmPose = HumanoidModel.ArmPose.EMPTY;
		this.leftArmPose = HumanoidModel.ArmPose.EMPTY;
		ItemStack itemstack = p_103793_.getItemInHand(InteractionHand.MAIN_HAND);
		if (itemstack.is(Items.CROSSBOW) && p_103793_.isAggressive()) {
			if (p_103793_.getMainArm() == HumanoidArm.RIGHT) {
				this.rightArmPose = ArmPose.ITEM;
			} else {
				this.leftArmPose = ArmPose.ITEM;
			}
		}
//This adds the arm poses and the attack animations if the mob is hostile
		super.prepareMobModel(p_103793_, p_103794_, p_103795_, p_103796_);
	}
	public void setupAnim(Type p_103798_, float p_103799_, float p_103800_, float p_103801_, float p_103802_, float p_103803_) {
		super.setupAnim(p_103798_, p_103799_, p_103800_, p_103801_, p_103802_, p_103803_);
		ItemStack itemstack = p_103798_.getMainHandItem();
		if (p_103798_.isAggressive() && (itemstack.isEmpty() || !itemstack.is(Items.DIAMOND_SWORD))) {
			float f = Mth.sin(this.attackTime * (float)Math.PI);
			float f1 = Mth.sin((1.0F - (1.0F - this.attackTime) * (1.0F - this.attackTime)) * (float)Math.PI);
			this.rightArm.zRot = 0.0F;
			this.leftArm.zRot = 0.0F;
			this.rightArm.yRot = -(0.1F - f * 0.6F);
			this.leftArm.yRot = 0.1F - f * 0.6F;
			this.rightArm.xRot = (-(float)Math.PI / 2F);
			this.leftArm.xRot = (-(float)Math.PI / 2F);
			this.rightArm.xRot -= f * 1.2F - f1 * 0.4F;
			this.leftArm.xRot -= f * 1.2F - f1 * 0.4F;
			AnimationUtils.bobArms(this.rightArm, this.leftArm, p_103801_);
		}
//This adds the basic animations for the mob and the attack animations
	}
	public void translateToHand(HumanoidArm p_103778_, PoseStack p_103779_) {
		float f = p_103778_ == HumanoidArm.RIGHT ? 1.0F : -1.0F;
		ModelPart modelpart = this.getArm(p_103778_);
		modelpart.x += f;
		modelpart.translateAndRotate(p_103779_);
		modelpart.x -= f;
      //This is for the arm
	}

After importing the necessary classes it should work(Keep in mind this is for 1.18.2)

Edited by EaSports69
Didn't clarify
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.