[1.14.4] Entity with custom model doesn't show item in main hand

Started by DanilkaD on Sun, 05/03/2020 - 12:44

Topic category: Advanced modding

Last seen on 16:20, 4. Dec 2020
Joined Feb 2017
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
[1.14.4] Entity with custom model doesn't show item in main hand
Sun, 05/03/2020 - 12:46 (edited)

Hello everybody. I ran into a problem: because the items render only in zombie or biped model, the custom model of a zombified piglin doesn't show the golden sword. I have two questions:

* Is it possible to bypass that? 

* Is it possible to replace a vanilla mob and change it's id?

I will be very thankfull to anyone who will help with this. 

Also, the source code for the mob (that is the testificate) is:


package net.mcreator.nether.entity;

import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.fml.network.FMLPlayMessages;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.fml.client.registry.RenderingRegistry;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.client.event.ModelRegistryEvent;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.World;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.DamageSource;
import net.minecraft.item.SpawnEggItem;
import net.minecraft.item.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.monster.MonsterEntity;
import net.minecraft.entity.ai.goal.RandomWalkingGoal;
import net.minecraft.entity.ai.goal.NearestAttackableTargetGoal;
import net.minecraft.entity.ai.goal.LookRandomlyGoal;
import net.minecraft.entity.ai.goal.LeapAtTargetGoal;
import net.minecraft.entity.ai.goal.HurtByTargetGoal;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.EntityType;
import net.minecraft.entity.EntityClassification;
import net.minecraft.entity.Entity;
import net.minecraft.entity.CreatureAttribute;
import net.minecraft.client.renderer.model.ModelBox;
import net.minecraft.client.renderer.entity.model.RendererModel;
import net.minecraft.client.renderer.entity.model.EntityModel;
import net.minecraft.client.renderer.entity.MobRenderer;

import net.mcreator.nether.TheNetherUpdateElements;

@TheNetherUpdateElements.ModElement.Tag
public class ZombifiedPiglinTestEntity extends TheNetherUpdateElements.ModElement {
	public static EntityType entity = null;
	public ZombifiedPiglinTestEntity(TheNetherUpdateElements instance) {
		super(instance, 67);
		FMLJavaModLoadingContext.get().getModEventBus().register(this);
	}

	@Override
	public void initElements() {
		entity = (EntityType.Builder.<CustomEntity>create(CustomEntity::new, EntityClassification.MONSTER).setShouldReceiveVelocityUpdates(true)
				.setTrackingRange(64).setUpdateInterval(3).setCustomClientFactory(CustomEntity::new).size(0.6f, 1.8f)).build("zombified_piglin_test")
						.setRegistryName("zombified_piglin_test");
		elements.entities.add(() -> entity);
		elements.items
				.add(() -> new SpawnEggItem(entity, -1, -1, new Item.Properties().group(ItemGroup.MISC)).setRegistryName("zombified_piglin_test"));
	}

	@SubscribeEvent
	@OnlyIn(Dist.CLIENT)
	public void registerModels(ModelRegistryEvent event) {
		RenderingRegistry.registerEntityRenderingHandler(CustomEntity.class, renderManager -> {
			return new MobRenderer(renderManager, new Modelcustom_model(), 0.5f) {
				protected ResourceLocation getEntityTexture(Entity entity) {
					return new ResourceLocation("nether:textures/zombified_piglin.png");
				}
			};
		});
	}
	public static class CustomEntity extends MonsterEntity {
		public CustomEntity(FMLPlayMessages.SpawnEntity packet, World world) {
			this(entity, world);
		}

		public CustomEntity(EntityType<CustomEntity> type, World world) {
			super(type, world);
			experienceValue = 0;
			setNoAI(false);
			enablePersistence();
			this.setItemStackToSlot(EquipmentSlotType.MAINHAND, new ItemStack(Items.GOLDEN_SWORD, (int) (1)));
		}

		@Override
		protected void registerGoals() {
			super.registerGoals();
			this.goalSelector.addGoal(1, new RandomWalkingGoal(this, 1));
			this.goalSelector.addGoal(2, new LookRandomlyGoal(this));
			this.goalSelector.addGoal(3, new LeapAtTargetGoal(this, (float) 0.8));
			this.targetSelector.addGoal(4, new HurtByTargetGoal(this).setCallsForHelp(this.getClass()));
			this.targetSelector.addGoal(5, new NearestAttackableTargetGoal(this, PlayerEntity.class, false, false));
		}

		@Override
		public CreatureAttribute getCreatureAttribute() {
			return CreatureAttribute.UNDEFINED;
		}

		@Override
		public boolean canDespawn(double distanceToClosestPlayer) {
			return false;
		}

		protected void dropSpecialItems(DamageSource source, int looting, boolean recentlyHitIn) {
			super.dropSpecialItems(source, looting, recentlyHitIn);
		}

		@Override
		public net.minecraft.util.SoundEvent getAmbientSound() {
			return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.zombie_pigman.ambient"));
		}

		@Override
		public net.minecraft.util.SoundEvent getHurtSound(DamageSource ds) {
			return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.zombie_pigman.hurt"));
		}

		@Override
		public net.minecraft.util.SoundEvent getDeathSound() {
			return (net.minecraft.util.SoundEvent) ForgeRegistries.SOUND_EVENTS.getValue(new ResourceLocation("entity.zombie_pigman.death"));
		}

		@Override
		protected float getSoundVolume() {
			return 1.0F;
		}

		@Override
		protected void registerAttributes() {
			super.registerAttributes();
			if (this.getAttribute(SharedMonsterAttributes.ARMOR) != null)
				this.getAttribute(SharedMonsterAttributes.ARMOR).setBaseValue(0);
			if (this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED) != null)
				this.getAttribute(SharedMonsterAttributes.MOVEMENT_SPEED).setBaseValue(0.3);
			if (this.getAttribute(SharedMonsterAttributes.MAX_HEALTH) != null)
				this.getAttribute(SharedMonsterAttributes.MAX_HEALTH).setBaseValue(10);
			if (this.getAttribute(SharedMonsterAttributes.ATTACK_DAMAGE) != null)
				this.getAttribute(SharedMonsterAttributes.ATTACK_DAMAGE).setBaseValue(3);
		}
	}

	// Made with Blockbench
	// Paste this code into your mod.
	public static class Modelcustom_model extends EntityModel<Entity> {
		private final RendererModel head;
		private final RendererModel head_sub_0;
		private final RendererModel head_sub_1;
		private final RendererModel headwear;
		private final RendererModel body;
		private final RendererModel left_arm;
		private final RendererModel right_arm;
		private final RendererModel left_leg;
		private final RendererModel right_leg;
		public Modelcustom_model() {
			textureWidth = 128;
			textureHeight = 64;
			head = new RendererModel(this);
			head.setRotationPoint(0.0F, 0.0F, 0.0F);
			head.cubeList.add(new ModelBox(head, 0, 0, -5.0F, -8.0F, -4.0F, 10, 8, 8, 0.0F, false));
			head_sub_0 = new RendererModel(this);
			head_sub_0.setRotationPoint(0.0F, 0.0F, 0.0F);
			head.addChild(head_sub_0);
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 2, 0, 1.0F, -1.0F, -5.0F, 1, 1, 1, 0.0F, false));
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 2, 4, -2.0F, -1.0F, -5.0F, 1, 1, 1, 0.0F, false));
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 2, 4, 2.0F, -2.0F, -5.0F, 1, 2, 1, 0.0F, false));
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 31, 1, -2.0F, -4.0F, -5.0F, 4, 3, 1, 0.0F, false));
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 39, 8, -1.0F, -1.0F, -5.0F, 2, 1, 1, 0.0F, false));
			head_sub_0.cubeList.add(new ModelBox(head_sub_0, 2, 0, -3.0F, -2.0F, -5.0F, 1, 2, 1, 0.0F, false));
			head_sub_1 = new RendererModel(this);
			head_sub_1.setRotationPoint(0.0F, 0.0F, 0.0F);
			setRotationAngle(head_sub_1, 0.0F, 0.0F, -0.5236F);
			head.addChild(head_sub_1);
			head_sub_1.cubeList.add(new ModelBox(head_sub_1, 57, 38, 6.0F, -3.0F, -2.0F, 1, 5, 4, 0.0F, false));
			headwear = new RendererModel(this);
			headwear.setRotationPoint(0.0F, 0.0F, 0.0F);
			body = new RendererModel(this);
			body.setRotationPoint(0.0F, 0.0F, 0.0F);
			body.cubeList.add(new ModelBox(body, 16, 16, -4.0F, 0.0F, -2.0F, 8, 12, 4, 0.0F, false));
			left_arm = new RendererModel(this);
			left_arm.setRotationPoint(-5.0F, 2.0F, 0.0F);
			left_arm.cubeList.add(new ModelBox(left_arm, 40, 16, 9.0F, -2.0F, -2.0F, 4, 12, 4, 0.0F, false));
			right_arm = new RendererModel(this);
			right_arm.setRotationPoint(5.0F, 2.0F, 0.0F);
			right_arm.cubeList.add(new ModelBox(right_arm, 32, 48, -13.0F, -2.0F, -2.0F, 4, 12, 4, 0.0F, true));
			left_leg = new RendererModel(this);
			left_leg.setRotationPoint(-1.9F, 12.0F, 0.0F);
			left_leg.cubeList.add(new ModelBox(left_leg, 0, 16, 1.9F, 0.0F, -2.0F, 4, 12, 4, 0.0F, false));
			right_leg = new RendererModel(this);
			right_leg.setRotationPoint(1.9F, 12.0F, 0.0F);
			right_leg.cubeList.add(new ModelBox(right_leg, 16, 48, -5.9F, 0.0F, -2.0F, 4, 12, 4, 0.0F, false));
		}

		@Override
		public void render(Entity entity, float f, float f1, float f2, float f3, float f4, float f5) {
			head.render(f5);
			headwear.render(f5);
			body.render(f5);
			left_arm.render(f5);
			right_arm.render(f5);
			left_leg.render(f5);
			right_leg.render(f5);
		}

		public void setRotationAngle(RendererModel modelRenderer, float x, float y, float z) {
			modelRenderer.rotateAngleX = x;
			modelRenderer.rotateAngleY = y;
			modelRenderer.rotateAngleZ = z;
		}

		public void setRotationAngles(Entity e, float f, float f1, float f2, float f3, float f4, float f5) {
			super.setRotationAngles(e, f, f1, f2, f3, f4, f5);
			this.head.rotateAngleY = f3 / (180F / (float) Math.PI);
			this.head.rotateAngleX = f4 / (180F / (float) Math.PI);
			this.headwear.rotateAngleY = f3 / (180F / (float) Math.PI);
			this.headwear.rotateAngleX = f4 / (180F / (float) Math.PI);
			this.right_arm.rotateAngleX = MathHelper.cos(f * 0.6662F + (float) Math.PI) * f1;
			this.left_leg.rotateAngleX = MathHelper.cos(f * 1.0F) * -1.0F * f1;
			this.left_arm.rotateAngleX = MathHelper.cos(f * 0.6662F) * f1;
			this.right_leg.rotateAngleX = MathHelper.cos(f * 1.0F) * 1.0F * f1;
			this.head_sub_0.rotateAngleY = f3 / (180F / (float) Math.PI);
			this.head_sub_0.rotateAngleX = f4 / (180F / (float) Math.PI);
			this.head_sub_1.rotateAngleY = f4 / (180F / (float) Math.PI);
		}
	}
}

P.S. I know there is one for MCreator 1.7.5, but it's quite outdated. My MCreator version is 2020.3 EAP 18613 (a.k.a Snapshot 2)

Edited by DanilkaD on Sun, 05/03/2020 - 12:46
Yes it it, but you would…
Mon, 05/04/2020 - 07:33

Yes it it, but you would need to code custom render layer for this.

Last seen on 16:20, 4. Dec 2020
Joined Feb 2017
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Unfortunately, I lack Java…
Tue, 05/05/2020 - 11:26

Unfortunately, I lack Java coding skills. Time to ask the forge forum, or finally learn Java and JS

Last seen on 13:32, 19. Nov 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I am having the same problem…
Wed, 10/21/2020 - 08:59

I am having the same problem now. Did you find anything to render this item? I would really appreciate your help.

You need to code custom…
Wed, 10/21/2020 - 10:14

You need to code custom renderer for custom model and specify item render layers.

Last seen on 13:32, 19. Nov 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
I've already tried it, but…
Wed, 10/21/2020 - 17:20

I've already tried it, but all my attempts have ended in error because I'm new to coding. Please help me, write the code that should display the item, I no longer know what to do. I would be very grateful to you!

I don't have code at hand,…
Wed, 10/21/2020 - 18:14

I don't have code at hand, if you want more advanced features, you will need to dive into Java first and then how Minecraft code is structured

Last seen on 13:32, 19. Nov 2020
Joined Sep 2020
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
It's a pity, but not too bad…
Wed, 10/21/2020 - 21:25

It's a pity, but not too bad. I do not plan to add any advanced features, I only need to display the item in the custom mob hand. Then I'll go look for the answer elsewhere.