94 lines
4.4 KiB
Java
94 lines
4.4 KiB
Java
/*
|
|
* Decompiled with CFR 0.152.
|
|
*
|
|
* Could not load the following classes:
|
|
* it.unimi.dsi.fastutil.HashCommon
|
|
* org.joml.Quaternionfc
|
|
* org.jspecify.annotations.Nullable
|
|
*/
|
|
package net.minecraft.client.renderer.blockentity;
|
|
|
|
import com.mojang.blaze3d.vertex.PoseStack;
|
|
import com.mojang.math.Axis;
|
|
import it.unimi.dsi.fastutil.HashCommon;
|
|
import net.minecraft.client.renderer.SubmitNodeCollector;
|
|
import net.minecraft.client.renderer.blockentity.BlockEntityRenderer;
|
|
import net.minecraft.client.renderer.blockentity.BlockEntityRendererProvider;
|
|
import net.minecraft.client.renderer.blockentity.state.ShelfRenderState;
|
|
import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
|
import net.minecraft.client.renderer.item.ItemModelResolver;
|
|
import net.minecraft.client.renderer.item.ItemStackRenderState;
|
|
import net.minecraft.client.renderer.state.CameraRenderState;
|
|
import net.minecraft.client.renderer.texture.OverlayTexture;
|
|
import net.minecraft.core.Direction;
|
|
import net.minecraft.core.NonNullList;
|
|
import net.minecraft.world.item.ItemDisplayContext;
|
|
import net.minecraft.world.item.ItemStack;
|
|
import net.minecraft.world.level.block.ShelfBlock;
|
|
import net.minecraft.world.level.block.entity.ShelfBlockEntity;
|
|
import net.minecraft.world.phys.AABB;
|
|
import net.minecraft.world.phys.Vec3;
|
|
import org.joml.Quaternionfc;
|
|
import org.jspecify.annotations.Nullable;
|
|
|
|
public class ShelfRenderer
|
|
implements BlockEntityRenderer<ShelfBlockEntity, ShelfRenderState> {
|
|
private static final float ITEM_SIZE = 0.25f;
|
|
private static final float ALIGN_ITEMS_TO_BOTTOM = -0.25f;
|
|
private final ItemModelResolver itemModelResolver;
|
|
|
|
public ShelfRenderer(BlockEntityRendererProvider.Context context) {
|
|
this.itemModelResolver = context.itemModelResolver();
|
|
}
|
|
|
|
@Override
|
|
public ShelfRenderState createRenderState() {
|
|
return new ShelfRenderState();
|
|
}
|
|
|
|
@Override
|
|
public void extractRenderState(ShelfBlockEntity blockEntity, ShelfRenderState state, float partialTicks, Vec3 cameraPosition, @Nullable ModelFeatureRenderer.CrumblingOverlay breakProgress) {
|
|
BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
|
state.alignToBottom = blockEntity.getAlignItemsToBottom();
|
|
NonNullList<ItemStack> items = blockEntity.getItems();
|
|
int seed = HashCommon.long2int((long)blockEntity.getBlockPos().asLong());
|
|
for (int slot = 0; slot < items.size(); ++slot) {
|
|
ItemStack itemStack = items.get(slot);
|
|
if (itemStack.isEmpty()) continue;
|
|
ItemStackRenderState itemStackRenderState = new ItemStackRenderState();
|
|
this.itemModelResolver.updateForTopItem(itemStackRenderState, itemStack, ItemDisplayContext.ON_SHELF, blockEntity.level(), blockEntity, seed + slot);
|
|
state.items[slot] = itemStackRenderState;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void submit(ShelfRenderState state, PoseStack poseStack, SubmitNodeCollector submitNodeCollector, CameraRenderState camera) {
|
|
Direction direction = state.blockState.getValue(ShelfBlock.FACING);
|
|
float yRot = direction.getAxis().isHorizontal() ? -direction.toYRot() : 180.0f;
|
|
for (int slot = 0; slot < state.items.length; ++slot) {
|
|
ItemStackRenderState itemStackRenderState = state.items[slot];
|
|
if (itemStackRenderState == null) continue;
|
|
this.submitItem(state, itemStackRenderState, poseStack, submitNodeCollector, slot, yRot);
|
|
}
|
|
}
|
|
|
|
private void submitItem(ShelfRenderState state, ItemStackRenderState itemStackRenderState, PoseStack poseStack, SubmitNodeCollector submitNodeCollector, int slot, float yRot) {
|
|
float itemSlotPosition = (float)(slot - 1) * 0.3125f;
|
|
Vec3 itemOffset = new Vec3(itemSlotPosition, state.alignToBottom ? -0.25 : 0.0, -0.25);
|
|
poseStack.pushPose();
|
|
poseStack.translate(0.5f, 0.5f, 0.5f);
|
|
poseStack.mulPose((Quaternionfc)Axis.YP.rotationDegrees(yRot));
|
|
poseStack.translate(itemOffset);
|
|
poseStack.scale(0.25f, 0.25f, 0.25f);
|
|
AABB box = itemStackRenderState.getModelBoundingBox();
|
|
double offsetY = -box.minY;
|
|
if (!state.alignToBottom) {
|
|
offsetY += -(box.maxY - box.minY) / 2.0;
|
|
}
|
|
poseStack.translate(0.0, offsetY, 0.0);
|
|
itemStackRenderState.submit(poseStack, submitNodeCollector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0);
|
|
poseStack.popPose();
|
|
}
|
|
}
|
|
|