| 1 | package net.minecraft.client.renderer.blockentity;
|
| 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.renderer.SubmitNodeCollector;
|
| 8 | import net.minecraft.client.renderer.blockentity.state.BrushableBlockRenderState;
|
| 9 | import net.minecraft.client.renderer.feature.ModelFeatureRenderer;
|
| 10 | import net.minecraft.client.renderer.item.ItemModelResolver;
|
| 11 | import net.minecraft.client.renderer.state.level.CameraRenderState;
|
| 12 | import net.minecraft.client.renderer.texture.OverlayTexture;
|
| 13 | import net.minecraft.core.Direction;
|
| 14 | import net.minecraft.util.LightCoordsUtil;
|
| 15 | import net.minecraft.util.LightCoordsUtil.BrightnessGetter;
|
| 16 | import net.minecraft.world.item.ItemDisplayContext;
|
| 17 | import net.minecraft.world.level.block.entity.BrushableBlockEntity;
|
| 18 | import net.minecraft.world.level.block.state.properties.BlockStateProperties;
|
| 19 | import net.minecraft.world.phys.Vec3;
|
| 20 | import org.jspecify.annotations.Nullable;
|
| 21 |
|
| 22 | @Environment(EnvType.CLIENT)
|
| 23 | public class BrushableBlockRenderer implements BlockEntityRenderer<BrushableBlockEntity, BrushableBlockRenderState> {
|
| 24 | private final ItemModelResolver itemModelResolver;
|
| 25 |
|
| 26 | public BrushableBlockRenderer(final BlockEntityRendererProvider.Context context) {
|
| 27 | this.itemModelResolver = context.itemModelResolver();
|
| 28 | }
|
| 29 |
|
| 30 | public BrushableBlockRenderState createRenderState() {
|
| 31 | return new BrushableBlockRenderState();
|
| 32 | }
|
| 33 |
|
| 34 | public void extractRenderState(
|
| 35 | final BrushableBlockEntity blockEntity,
|
| 36 | final BrushableBlockRenderState state,
|
| 37 | final float partialTicks,
|
| 38 | final Vec3 cameraPosition,
|
| 39 | final ModelFeatureRenderer.@Nullable CrumblingOverlay breakProgress
|
| 40 | ) {
|
| 41 | BlockEntityRenderer.super.extractRenderState(blockEntity, state, partialTicks, cameraPosition, breakProgress);
|
| 42 | state.hitDirection = blockEntity.getHitDirection();
|
| 43 | state.dustProgress = blockEntity.getBlockState().getValue(BlockStateProperties.DUSTED);
|
| 44 | if (blockEntity.getLevel() != null && blockEntity.getHitDirection() != null) {
|
| 45 | state.lightCoords = LightCoordsUtil.getLightCoords(
|
| 46 | BrightnessGetter.DEFAULT, blockEntity.getLevel(), blockEntity.getBlockState(), blockEntity.getBlockPos().relative(blockEntity.getHitDirection())
|
| 47 | );
|
| 48 | }
|
| 49 |
|
| 50 | this.itemModelResolver.updateForTopItem(state.itemState, blockEntity.getItem(), ItemDisplayContext.FIXED, blockEntity.getLevel(), null, 0);
|
| 51 | }
|
| 52 |
|
| 53 | public void submit(
|
| 54 | final BrushableBlockRenderState state, final PoseStack poseStack, final SubmitNodeCollector submitNodeCollector, final CameraRenderState camera
|
| 55 | ) {
|
| 56 | if (state.dustProgress > 0 && state.hitDirection != null && !state.itemState.isEmpty()) {
|
| 57 | poseStack.pushPose();
|
| 58 | poseStack.translate(0.0F, 0.5F, 0.0F);
|
| 59 | float[] translations = this.translations(state.hitDirection, state.dustProgress);
|
| 60 | poseStack.translate(translations[0], translations[1], translations[2]);
|
| 61 | poseStack.rotateDegrees(Axis.YP, 75.0F);
|
| 62 | boolean eastWest = state.hitDirection == Direction.EAST || state.hitDirection == Direction.WEST;
|
| 63 | poseStack.rotateDegrees(Axis.YP, (eastWest ? 90 : 0) + 11);
|
| 64 | poseStack.scale(0.5F, 0.5F, 0.5F);
|
| 65 | state.itemState.submit(poseStack, submitNodeCollector, state.lightCoords, OverlayTexture.NO_OVERLAY, 0);
|
| 66 | poseStack.popPose();
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | private float[] translations(final Direction direction, final int completionState) {
|
| 71 | float[] xyzTranslations = new float[]{0.5F, 0.0F, 0.5F};
|
| 72 | float completionOffset = completionState / 10.0F * 0.75F;
|
| 73 | switch (direction) {
|
| 74 | case EAST:
|
| 75 | xyzTranslations[0] = 0.73F + completionOffset;
|
| 76 | break;
|
| 77 | case WEST:
|
| 78 | xyzTranslations[0] = 0.25F - completionOffset;
|
| 79 | break;
|
| 80 | case UP:
|
| 81 | xyzTranslations[1] = 0.25F + completionOffset;
|
| 82 | break;
|
| 83 | case DOWN:
|
| 84 | xyzTranslations[1] = -0.23F - completionOffset;
|
| 85 | break;
|
| 86 | case NORTH:
|
| 87 | xyzTranslations[2] = 0.25F - completionOffset;
|
| 88 | break;
|
| 89 | case SOUTH:
|
| 90 | xyzTranslations[2] = 0.73F + completionOffset;
|
| 91 | }
|
| 92 |
|
| 93 | return xyzTranslations;
|
| 94 | }
|
| 95 | }
|
| 96 |
|