| 1 | package net.minecraft.client.renderer.blockentity;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.vertex.PoseStack;
|
| 4 | import com.mojang.math.Axis;
|
| 5 | import com.mojang.math.Transformation;
|
| 6 | import java.util.Map;
|
| 7 | import net.fabricmc.api.EnvType;
|
| 8 | import net.fabricmc.api.Environment;
|
| 9 | import net.minecraft.client.model.geom.ModelLayerLocation;
|
| 10 | import net.minecraft.client.model.geom.ModelLayers;
|
| 11 | import net.minecraft.client.model.object.chest.ChestModel;
|
| 12 | import net.minecraft.client.renderer.MultiblockChestResources;
|
| 13 | import net.minecraft.client.renderer.Sheets;
|
| 14 | import net.minecraft.client.renderer.SubmitNodeCollector;
|
| 15 | import net.minecraft.client.renderer.blockentity.state.ChestRenderState;
|
| 16 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 17 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 18 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 19 | import net.minecraft.client.resources.model.sprite.SpriteGetter;
|
| 20 | import net.minecraft.client.resources.model.sprite.SpriteId;
|
| 21 | import net.minecraft.core.Direction;
|
| 22 | import net.minecraft.util.SpecialDates;
|
| 23 | import net.minecraft.util.Util;
|
| 24 | import net.minecraft.world.level.block.Blocks;
|
| 25 | import net.minecraft.world.level.block.ChestBlock;
|
| 26 | import net.minecraft.world.level.block.CopperChestBlock;
|
| 27 | import net.minecraft.world.level.block.DoubleBlockCombiner.Combiner;
|
| 28 | import net.minecraft.world.level.block.DoubleBlockCombiner.NeighborCombineResult;
|
| 29 | import net.minecraft.world.level.block.entity.BlockEntity;
|
| 30 | import net.minecraft.world.level.block.entity.ChestBlockEntity;
|
| 31 | import net.minecraft.world.level.block.entity.EnderChestBlockEntity;
|
| 32 | import net.minecraft.world.level.block.entity.LidBlockEntity;
|
| 33 | import net.minecraft.world.level.block.entity.TrappedChestBlockEntity;
|
| 34 | import net.minecraft.world.level.block.state.BlockState;
|
| 35 | import net.minecraft.world.level.block.state.properties.ChestType;
|
| 36 | import net.minecraft.world.phys.Vec3;
|
| 37 | import org.joml.Matrix4f;
|
| 38 | import org.jspecify.annotations.Nullable;
|
| 39 |
|
| 40 | @Environment(EnvType.CLIENT)
|
| 41 | public class ChestRenderer<T extends BlockEntity & LidBlockEntity> implements BlockEntityRenderer<T, ChestRenderState> {
|
| 42 | public static final MultiblockChestResources<ModelLayerLocation> LAYERS = new MultiblockChestResources<>(
|
| 43 | ModelLayers.CHEST, ModelLayers.DOUBLE_CHEST_LEFT, ModelLayers.DOUBLE_CHEST_RIGHT
|
| 44 | );
|
| 45 | private static final Map<Direction, Transformation> TRANSFORMATIONS = Util.makeEnumMap(Direction.class, ChestRenderer::createModelTransformation);
|
| 46 | private final SpriteGetter sprites;
|
| 47 | private final MultiblockChestResources<ChestModel> models;
|
| 48 | private final boolean xmasTextures;
|
| 49 |
|
| 50 | public ChestRenderer(final BlockEntityRendererProvider.Context context) {
|
| 51 | this.sprites = context.sprites();
|
| 52 | this.xmasTextures = xmasTextures();
|
| 53 | this.models = LAYERS.map(layer -> new ChestModel(context.bakeLayer(layer)));
|
| 54 | }
|
| 55 |
|
| 56 | public static boolean xmasTextures() {
|
| 57 | return SpecialDates.isExtendedChristmas();
|
| 58 | }
|
| 59 |
|
| 60 | public ChestRenderState createRenderState() {
|
| 61 | return new ChestRenderState();
|
| 62 | }
|
| 63 |
|
| 64 | public void extractRenderState(
|
| 65 | final T blockEntity,
|
| 66 | final ChestRenderState state,
|
| 67 | final float partialTicks,
|
| 68 | final Vec3 cameraPosition,
|
| 69 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 70 | ) {
|
| 71 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 72 | boolean hasLevel = blockEntity.getLevel() != null;
|
| 73 | BlockState blockState = hasLevel ? blockEntity.getBlockState() : Blocks.CHEST.defaultBlockState().setValue(ChestBlock.FACING, Direction.SOUTH);
|
| 74 | state.type = blockState.hasProperty(ChestBlock.TYPE) ? blockState.getValue(ChestBlock.TYPE) : ChestType.SINGLE;
|
| 75 | state.facing = blockState.getValue(ChestBlock.FACING);
|
| 76 | state.material = getChestMaterial(blockEntity, this.xmasTextures);
|
| 77 | NeighborCombineResult<? extends ChestBlockEntity> combineResult;
|
| 78 | if (hasLevel && blockState.getBlock() instanceof ChestBlock chestBlock) {
|
| 79 | combineResult = chestBlock.combine(blockState, blockEntity.getLevel(), blockEntity.getBlockPos(), true);
|
| 80 | } else {
|
| 81 | combineResult = Combiner::acceptNone;
|
| 82 | }
|
| 83 |
|
| 84 | state.open = combineResult.apply(ChestBlock.opennessCombiner(blockEntity)).get(partialTicks);
|
| 85 | if (state.type != ChestType.SINGLE) {
|
| 86 | state.lightCoords = combineResult.apply(new BrightnessCombiner<>()).applyAsInt(state.lightCoords);
|
| 87 | }
|
| 88 | }
|
| 89 |
|
| 90 | public void submit(final ChestRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera) {
|
| 91 | poseStack.pushPose();
|
| 92 | poseStack.mulPose(modelTransformation(state.facing));
|
| 93 | float open = state.open;
|
| 94 | open = 1.0F - open;
|
| 95 | open = 1.0F - open * open * open;
|
| 96 | SpriteId spriteId = Sheets.chooseSprite(state.material, state.type);
|
| 97 | ChestModel model = this.models.select(state.type);
|
| 98 | submitNodeCollector.submitModel(model, open, poseStack, state.lightCoords, OverlayTexture.NO_OVERLAY, -1, spriteId, this.sprites, 0);
|
| 99 | if (state.breakProgress != null) {
|
| 100 | submitNodeCollector.order(1)
|
| 101 | .submitCrumblingOverlay(
|
| 102 | model, open, poseStack, spriteId.renderType(model.renderType()), state.lightCoords, OverlayTexture.NO_OVERLAY, -1, state.breakProgress
|
| 103 | );
|
| 104 | }
|
| 105 |
|
| 106 | poseStack.popPose();
|
| 107 | }
|
| 108 |
|
| 109 | private static ChestRenderState.ChestMaterialType getChestMaterial(final BlockEntity entity, final boolean xmasTextures) {
|
| 110 | if (entity.getBlockState().getBlock() instanceof CopperChestBlock copperChestBlock) {
|
| 111 | return switch (copperChestBlock.getState()) {
|
| 112 | case UNAFFECTED -> ChestRenderState.ChestMaterialType.COPPER_UNAFFECTED;
|
| 113 | case EXPOSED -> ChestRenderState.ChestMaterialType.COPPER_EXPOSED;
|
| 114 | case WEATHERED -> ChestRenderState.ChestMaterialType.COPPER_WEATHERED;
|
| 115 | case OXIDIZED -> ChestRenderState.ChestMaterialType.COPPER_OXIDIZED;
|
| 116 | };
|
| 117 | } else if (entity instanceof EnderChestBlockEntity) {
|
| 118 | return ChestRenderState.ChestMaterialType.ENDER_CHEST;
|
| 119 | } else if (xmasTextures) {
|
| 120 | return ChestRenderState.ChestMaterialType.CHRISTMAS;
|
| 121 | } else {
|
| 122 | return entity instanceof TrappedChestBlockEntity ? ChestRenderState.ChestMaterialType.TRAPPED : ChestRenderState.ChestMaterialType.REGULAR;
|
| 123 | }
|
| 124 | }
|
| 125 |
|
| 126 | public static Transformation modelTransformation(final Direction facing) {
|
| 127 | return TRANSFORMATIONS.get(facing);
|
| 128 | }
|
| 129 |
|
| 130 | private static Transformation createModelTransformation(final Direction facing) {
|
| 131 | return new Transformation(new Matrix4f().rotationAround(Axis.YP.rotationDegrees(-facing.toYRot()), 0.5F, 0.0F, 0.5F));
|
| 132 | }
|
| 133 | }
|
| 134 |
|