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

73 lines
2.5 KiB
Java

package net.minecraft.client.renderer.entity.layers;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
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.renderer.MultiBufferSource;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.renderer.entity.state.LivingEntityRenderState;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.item.ItemDisplayContext;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;
@Environment(EnvType.CLIENT)
public class ItemInHandLayer<S extends LivingEntityRenderState, M extends EntityModel<S> & ArmedModel> extends RenderLayer<S, M> {
private final ItemRenderer itemRenderer;
public ItemInHandLayer(RenderLayerParent<S, M> renderer, ItemRenderer itemRenderer) {
super(renderer);
this.itemRenderer = itemRenderer;
}
public void render(PoseStack poseStack, MultiBufferSource bufferSource, int packedLight, S renderState, float yRot, float xRot) {
this.renderArmWithItem(
renderState,
renderState.rightHandItemModel,
renderState.rightHandItem,
ItemDisplayContext.THIRD_PERSON_RIGHT_HAND,
HumanoidArm.RIGHT,
poseStack,
bufferSource,
packedLight
);
this.renderArmWithItem(
renderState,
renderState.leftHandItemModel,
renderState.leftHandItem,
ItemDisplayContext.THIRD_PERSON_LEFT_HAND,
HumanoidArm.LEFT,
poseStack,
bufferSource,
packedLight
);
}
protected void renderArmWithItem(
S renderState,
@Nullable BakedModel itemModel,
ItemStack item,
ItemDisplayContext displayContext,
HumanoidArm arm,
PoseStack poseStack,
MultiBufferSource bufferSource,
int packedLight
) {
if (itemModel != null && !item.isEmpty()) {
poseStack.pushPose();
this.getParentModel().translateToHand(arm, poseStack);
poseStack.mulPose(Axis.XP.rotationDegrees(-90.0F));
poseStack.mulPose(Axis.YP.rotationDegrees(180.0F));
boolean bl = arm == HumanoidArm.LEFT;
poseStack.translate((bl ? -1 : 1) / 16.0F, 0.125F, -0.625F);
this.itemRenderer.render(item, displayContext, bl, poseStack, bufferSource, packedLight, OverlayTexture.NO_OVERLAY, itemModel);
poseStack.popPose();
}
}
}