minecraft-src/net/minecraft/client/renderer/entity/layers/PlayerItemInHandLayer.java
2025-07-04 02:49:36 +03:00

69 lines
3.1 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.ItemRenderer;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.state.PlayerRenderState;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class PlayerItemInHandLayer<S extends PlayerRenderState, M extends EntityModel<S> & ArmedModel & HeadedModel> extends ItemInHandLayer<S, M> {
private final ItemRenderer itemRenderer;
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, ItemRenderer itemRenderer) {
super(renderer, itemRenderer);
this.itemRenderer = itemRenderer;
}
protected void renderArmWithItem(
S renderState,
@Nullable BakedModel itemModel,
ItemStack item,
ItemDisplayContext displayContext,
HumanoidArm arm,
PoseStack poseStack,
MultiBufferSource bufferSource,
int packedLight
) {
if (itemModel != null) {
InteractionHand interactionHand = arm == renderState.mainArm ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
if (renderState.isUsingItem && renderState.useItemHand == interactionHand && renderState.attackTime < 1.0E-5F && item.is(Items.SPYGLASS)) {
this.renderArmWithSpyglass(itemModel, item, arm, poseStack, bufferSource, packedLight);
} else {
super.renderArmWithItem(renderState, itemModel, item, displayContext, arm, poseStack, bufferSource, packedLight);
}
}
}
private void renderArmWithSpyglass(BakedModel model, ItemStack item, 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);
this.itemRenderer.render(item, ItemDisplayContext.HEAD, false, poseStack, bufferSource, packedLight, OverlayTexture.NO_OVERLAY, model);
poseStack.popPose();
}
}