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

Posted (edited)

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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • https://pastebin.com/FaQ43RnJ  Este es un error que me lanza al momento de querer iniciar forge Intente de todo pero no sirve nada. This is an error that I get when I want to start forge. I tried everything but nothing works.
    • It was a stupid Eclipse problem ,the debugger was set to a lower version of java.  In what world would i want my development version different from the debugger version.   ty for looking
    • Caused by: java.lang.NullPointerException: Cannot read field "level" because "this.minecraft" is null This is my first big error when coding. I looked it up, but there are a bunch of references to Dawncraft and not enough to figure out what was going wrong. I found this, but it involves a command and my GUI only opens when you shift + right-click while holding an item. They fixed it by not calling it with a command, but I am not using a command. Here's the render of my menu, which is the only part that changed between it working and not:   @Override public void render(@Nonnull PoseStack mstack, int mouseX, int mouseY, float partialTicks) { int midx = this.width / 2; int midy = this.height / 2; this.renderBackground(mstack); drawCenteredString(mstack, font, I18n.get("config.shape_tool"), midx, 4, 0xC3D1D8); Utils.renderImage(midx - 12, midy, 20, 20, "textures/gui/plus.png", mstack); addRenderableWidget(new Button(midx - 12, midy, 10, 10, Component.translatable(""), this::increaseax)); Utils.renderImage(midx + 12, midy, 20, 20, "textures/gui/minus.png", mstack); addRenderableWidget(new Button(midx + 12, midy, 10, 10, Component.translatable(""), this::decreaseax)); drawCenteredString(mstack, font, String.valueOf(shapetoolax), midx, midy + 10, 0xC3D1D8); super.render(mstack, mouseX, mouseY, partialTicks); } Utils.renderImage is this:   public static void renderImage(int x, int y, int width, int height, String img, PoseStack mstack) { RenderSystem.setShaderColor(1, 1, 1, 1); RenderSystem.enableBlend(); RenderSystem.defaultBlendFunc(); RenderSystem.setShaderTexture(0, new ResourceLocation(PNegative.MODID, img)); blit(mstack, x, y, 0, 0, width, height, width, height); RenderSystem.disableBlend(); } I have used it before and it has worked, so it can't be a problem. 
    • I found a topic  with the very simmilar problems. I run modpack Direwolf20 1.19.2 version from FTB with dedicated server with my friends and i am the only who have this type of crashes. It has been through login process and after 5-6 crashes i can connect. And randombly it could crash me in game but mostly when i am tp'ing using Nether portals etc. I already setted up all privileges for javaw in firewall, flushed dns, reset network settings and etc. Changed java version to from FAQ too. Here's some logs from client side https://gnomebot.dev/paste/1158502890168664194 https://gnomebot.dev/paste/1158187264657063946    
    • Don't know why game keeps crashing after a few seconds or walking towards something, tried removing larger mods and someaddon performance mods but it's not working. Minecraft launchers telling me "Exit Code 1"
  • Topics

×
×
  • Create New...

Important Information

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