minecraft-src/net/minecraft/client/renderer/entity/layers/PlayerItemInHandLayer.java
2025-09-18 12:27:44 +00:00

55 lines
2.6 KiB
Java

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<S extends PlayerRenderState, M extends EntityModel<S> & ArmedModel & HeadedModel> extends ItemInHandLayer<S, M> {
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<S, M> 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();
}
}