package net.minecraft.client.renderer.entity.layers; import com.mojang.blaze3d.vertex.PoseStack; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.model.ArmedModel; import net.minecraft.client.model.EntityModel; import net.minecraft.client.model.HeadedModel; import net.minecraft.client.model.geom.ModelPart; import net.minecraft.client.renderer.ItemInHandRenderer; import net.minecraft.client.renderer.MultiBufferSource; import net.minecraft.client.renderer.entity.RenderLayerParent; import net.minecraft.util.Mth; import net.minecraft.world.entity.HumanoidArm; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemDisplayContext; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.Items; @Environment(EnvType.CLIENT) public class PlayerItemInHandLayer & ArmedModel & HeadedModel> extends ItemInHandLayer { private final ItemInHandRenderer itemInHandRenderer; private static final float X_ROT_MIN = (float) (-Math.PI / 6); private static final float X_ROT_MAX = (float) (Math.PI / 2); public PlayerItemInHandLayer(RenderLayerParent renderer, ItemInHandRenderer itemInHandRenderer) { super(renderer, itemInHandRenderer); this.itemInHandRenderer = itemInHandRenderer; } @Override protected void renderArmWithItem( LivingEntity livingEntity, ItemStack itemStack, ItemDisplayContext displayContext, HumanoidArm arm, PoseStack poseStack, MultiBufferSource buffer, int packedLight ) { if (itemStack.is(Items.SPYGLASS) && livingEntity.getUseItem() == itemStack && livingEntity.swingTime == 0) { this.renderArmWithSpyglass(livingEntity, itemStack, arm, poseStack, buffer, packedLight); } else { super.renderArmWithItem(livingEntity, itemStack, displayContext, arm, poseStack, buffer, packedLight); } } private void renderArmWithSpyglass(LivingEntity entity, ItemStack stack, HumanoidArm arm, PoseStack poseStack, MultiBufferSource buffer, int combinedLight) { poseStack.pushPose(); ModelPart modelPart = this.getParentModel().getHead(); float f = modelPart.xRot; modelPart.xRot = Mth.clamp(modelPart.xRot, (float) (-Math.PI / 6), (float) (Math.PI / 2)); modelPart.translateAndRotate(poseStack); modelPart.xRot = f; CustomHeadLayer.translateToHead(poseStack, false); boolean bl = arm == HumanoidArm.LEFT; poseStack.translate((bl ? -2.5F : 2.5F) / 16.0F, -0.0625F, 0.0F); this.itemInHandRenderer.renderItem(entity, stack, ItemDisplayContext.HEAD, false, poseStack, buffer, combinedLight); poseStack.popPose(); } }