| 1 | package net.minecraft.client.renderer.entity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.client.renderer.MapRenderer;
|
| 9 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 10 | import net.minecraft.client.renderer.block.BlockModelResolver;
|
| 11 | import net.minecraft.client.renderer.block.model.BlockDisplayContext;
|
| 12 | import net.minecraft.client.renderer.entity.state.ItemFrameRenderState;
|
| 13 | import net.minecraft.client.renderer.item.ItemModelResolver;
|
| 14 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 15 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 16 | import net.minecraft.core.BlockPos;
|
| 17 | import net.minecraft.core.Direction;
|
| 18 | import net.minecraft.network.chat.Component;
|
| 19 | import net.minecraft.world.entity.EntityTypes;
|
| 20 | import net.minecraft.world.entity.decoration.ItemFrame;
|
| 21 | import net.minecraft.world.item.ItemDisplayContext;
|
| 22 | import net.minecraft.world.item.ItemStack;
|
| 23 | import net.minecraft.world.level.saveddata.maps.MapId;
|
| 24 | import net.minecraft.world.level.saveddata.maps.MapItemSavedData;
|
| 25 | import net.minecraft.world.phys.Vec3;
|
| 26 |
|
| 27 | @Environment(EnvType.CLIENT)
|
| 28 | public class ItemFrameRenderer<T extends ItemFrame> extends EntityRenderer<T, ItemFrameRenderState> {
|
| 29 | public static final BlockDisplayContext BLOCK_DISPLAY_CONTEXT = BlockDisplayContext.create();
|
| 30 | public static final int GLOW_FRAME_BRIGHTNESS = 5;
|
| 31 | public static final int BRIGHT_MAP_LIGHT_ADJUSTMENT = 30;
|
| 32 | private final BlockModelResolver blockModelResolver;
|
| 33 | private final ItemModelResolver itemModelResolver;
|
| 34 | private final MapRenderer mapRenderer;
|
| 35 |
|
| 36 | public ItemFrameRenderer(final EntityRendererProvider.Context context) {
|
| 37 | super(context);
|
| 38 | this.blockModelResolver = context.getBlockModelResolver();
|
| 39 | this.itemModelResolver = context.getItemModelResolver();
|
| 40 | this.mapRenderer = context.getMapRenderer();
|
| 41 | }
|
| 42 |
|
| 43 | protected int getBlockLightLevel(final T entity, final BlockPos blockPos) {
|
| 44 | return entity.is(EntityTypes.GLOW_ITEM_FRAME) ? Math.max(5, super.getBlockLightLevel(entity, blockPos)) : super.getBlockLightLevel(entity, blockPos);
|
| 45 | }
|
| 46 |
|
| 47 | public void submit(final ItemFrameRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 48 | super.submit(state, poseStack, submitNodeCollector, camera);
|
| 49 | poseStack.pushPose();
|
| 50 | Direction direction = state.direction;
|
| 51 | Vec3 renderOffset = this.getRenderOffset(state);
|
| 52 | poseStack.translate(-renderOffset.x(), -renderOffset.y(), -renderOffset.z());
|
| 53 | double offs = 0.46875;
|
| 54 | poseStack.translate(direction.getStepX() * 0.46875, direction.getStepY() * 0.46875, direction.getStepZ() * 0.46875);
|
| 55 | float xRot;
|
| 56 | float yRot;
|
| 57 | if (direction.getAxis().isHorizontal()) {
|
| 58 | xRot = 0.0F;
|
| 59 | yRot = 180.0F - direction.toYRot();
|
| 60 | } else {
|
| 61 | xRot = -90 * direction.getAxisDirection().getStep();
|
| 62 | yRot = 180.0F;
|
| 63 | }
|
| 64 |
|
| 65 | poseStack.rotateDegrees(Axis.XP, xRot);
|
| 66 | poseStack.rotateDegrees(Axis.YP, yRot);
|
| 67 | if (!state.frameModel.isEmpty()) {
|
| 68 | poseStack.pushPose();
|
| 69 | poseStack.translate(-0.5F, -0.5F, -0.5F);
|
| 70 | state.frameModel.submitWithZOffset(poseStack, submitNodeCollector, state.lightCoords, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 71 | poseStack.popPose();
|
| 72 | }
|
| 73 |
|
| 74 | if (state.isInvisible) {
|
| 75 | poseStack.translate(0.0F, 0.0F, 0.5F);
|
| 76 | } else {
|
| 77 | poseStack.translate(0.0F, 0.0F, 0.4375F);
|
| 78 | }
|
| 79 |
|
| 80 | if (state.mapId != null) {
|
| 81 | int rotation = state.rotation % 4 * 2;
|
| 82 | poseStack.rotateDegrees(Axis.ZP, rotation * 360.0F / 8.0F);
|
| 83 | poseStack.rotateDegrees(Axis.ZP, 180.0F);
|
| 84 | float s = 0.0078125F;
|
| 85 | poseStack.scale(0.0078125F, 0.0078125F, 0.0078125F);
|
| 86 | poseStack.translate(-64.0F, -64.0F, 0.0F);
|
| 87 | poseStack.translate(0.0F, 0.0F, -1.0F);
|
| 88 | int lightCoords = this.getLightCoords(state.isGlowFrame, 15728850, state.lightCoords);
|
| 89 | this.mapRenderer.render(state.mapRenderState, poseStack, submitNodeCollector, true, lightCoords);
|
| 90 | } else if (!state.item.isEmpty()) {
|
| 91 | poseStack.rotateDegrees(Axis.ZP, state.rotation * 360.0F / 8.0F);
|
| 92 | int lightVal = this.getLightCoords(state.isGlowFrame, 15728880, state.lightCoords);
|
| 93 | poseStack.scale(0.5F, 0.5F, 0.5F);
|
| 94 | state.item.submit(poseStack, submitNodeCollector, lightVal, OverlayTexture.NO_OVERLAY, state.outlineColor);
|
| 95 | }
|
| 96 |
|
| 97 | poseStack.popPose();
|
| 98 | }
|
| 99 |
|
| 100 | private int getLightCoords(final boolean isGlowFrame, final int glowLightCoords, final int originalLightCoords) {
|
| 101 | return isGlowFrame ? glowLightCoords : originalLightCoords;
|
| 102 | }
|
| 103 |
|
| 104 | public Vec3 getRenderOffset(final ItemFrameRenderState state) {
|
| 105 | return new Vec3(state.direction.getStepX() * 0.3F, -0.25, state.direction.getStepZ() * 0.3F);
|
| 106 | }
|
| 107 |
|
| 108 | protected boolean shouldShowName(final T entity, final double distanceToCameraSq) {
|
| 109 | return !Minecraft.getInstance().gui.hud.isHidden() && this.entityRenderDispatcher.crosshairPickEntity == entity && entity.getItem().getCustomName() != null;
|
| 110 | }
|
| 111 |
|
| 112 | protected Component getNameTag(final T entity) {
|
| 113 | return entity.getItem().getHoverName();
|
| 114 | }
|
| 115 |
|
| 116 | public ItemFrameRenderState createRenderState() {
|
| 117 | return new ItemFrameRenderState();
|
| 118 | }
|
| 119 |
|
| 120 | public void extractRenderState(final T entity, final ItemFrameRenderState state, final float partialTicks) {
|
| 121 | super.extractRenderState(entity, state, partialTicks);
|
| 122 | state.direction = entity.getDirection();
|
| 123 | ItemStack itemStack = entity.getItem();
|
| 124 | this.itemModelResolver.updateForNonLiving(state.item, itemStack, ItemDisplayContext.FIXED, entity);
|
| 125 | state.rotation = entity.getRotation();
|
| 126 | state.isGlowFrame = entity.is(EntityTypes.GLOW_ITEM_FRAME);
|
| 127 | state.mapId = null;
|
| 128 | if (!itemStack.isEmpty()) {
|
| 129 | MapId framedMapId = entity.getFramedMapId(itemStack);
|
| 130 | if (framedMapId != null) {
|
| 131 | MapItemSavedData mapData = entity.level().getMapData(framedMapId);
|
| 132 | if (mapData != null) {
|
| 133 | this.mapRenderer.extractRenderState(framedMapId, mapData, state.mapRenderState);
|
| 134 | state.mapId = framedMapId;
|
| 135 | }
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | if (!state.isInvisible) {
|
| 140 | this.blockModelResolver.updateForItemFrame(state.frameModel, state.isGlowFrame, state.mapId != null);
|
| 141 | } else {
|
| 142 | state.frameModel.clear();
|
| 143 | }
|
| 144 | }
|
| 145 | }
|
| 146 |
|