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.MultiBufferSource; import net.minecraft.client.renderer.entity.RenderLayerParent; import net.minecraft.client.renderer.entity.state.PlayerRenderState; import net.minecraft.client.renderer.item.ItemStackRenderState; import net.minecraft.client.renderer.texture.OverlayTexture; import net.minecraft.util.Mth; import net.minecraft.world.InteractionHand; import net.minecraft.world.entity.HumanoidArm; @Environment(EnvType.CLIENT) public class PlayerItemInHandLayer & ArmedModel & HeadedModel> extends ItemInHandLayer { 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) { super(renderer); } protected void renderArmWithItem( S renderState, ItemStackRenderState itemStackRenderState, HumanoidArm arm, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight ) { if (!itemStackRenderState.isEmpty()) { InteractionHand interactionHand = arm == renderState.mainArm ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND; if (renderState.isUsingItem && renderState.useItemHand == interactionHand && renderState.attackTime < 1.0E-5F && !renderState.heldOnHead.isEmpty()) { this.renderItemHeldToEye(renderState.heldOnHead, arm, poseStack, bufferSource, packedLight); } else { super.renderArmWithItem(renderState, itemStackRenderState, arm, poseStack, bufferSource, packedLight); } } } private void renderItemHeldToEye(ItemStackRenderState renderState, HumanoidArm arm, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) { poseStack.pushPose(); this.getParentModel().root().translateAndRotate(poseStack); 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, CustomHeadLayer.Transforms.DEFAULT); boolean bl = arm == HumanoidArm.LEFT; poseStack.translate((bl ? -2.5F : 2.5F) / 16.0F, -0.0625F, 0.0F); renderState.render(poseStack, bufferSource, packedLight, OverlayTexture.NO_OVERLAY); poseStack.popPose(); } }