|
| 1 | +package de.rettichlp.render; |
| 2 | + |
| 3 | +import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext; |
| 4 | +import net.minecraft.client.MinecraftClient; |
| 5 | +import net.minecraft.client.network.ClientPlayerEntity; |
| 6 | +import net.minecraft.client.render.BufferBuilderStorage; |
| 7 | +import net.minecraft.client.render.OutlineVertexConsumerProvider; |
| 8 | +import net.minecraft.client.util.math.MatrixStack; |
| 9 | +import net.minecraft.client.world.ClientWorld; |
| 10 | +import net.minecraft.util.math.BlockPos; |
| 11 | +import net.minecraft.util.math.Vec3d; |
| 12 | +import net.minecraft.world.World; |
| 13 | +import org.jetbrains.annotations.NotNull; |
| 14 | + |
| 15 | +import java.util.HashSet; |
| 16 | +import java.util.Set; |
| 17 | +import java.util.function.Predicate; |
| 18 | + |
| 19 | +import static de.rettichlp.PKUtilsClient.addonPlayer; |
| 20 | +import static net.minecraft.block.Blocks.FERN; |
| 21 | +import static net.minecraft.block.Blocks.PODZOL; |
| 22 | +import static net.minecraft.block.Blocks.TNT; |
| 23 | +import static net.minecraft.util.math.BlockPos.iterate; |
| 24 | + |
| 25 | +public class WorldBlockHighlighter { |
| 26 | + |
| 27 | + private static final int RENDER_DISTANCE = 32; // Render distance for bomb spots |
| 28 | + |
| 29 | + private final WorldRenderContext context; |
| 30 | + |
| 31 | + public WorldBlockHighlighter(WorldRenderContext context) { |
| 32 | + this.context = context; |
| 33 | + } |
| 34 | + |
| 35 | + public void highlightBomb(@NotNull World world) { |
| 36 | + MatrixStack matrices = this.context.matrixStack(); |
| 37 | + |
| 38 | + if (matrices == null) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + Set<BlockPos> filteredBlocks = filterBlocks(blockPos -> world.getBlockState(blockPos).isOf(TNT)); |
| 43 | + |
| 44 | + for (BlockPos filteredBlock : filteredBlocks) { |
| 45 | + drawOutline(255, 0, 0, 1, matrices, filteredBlock); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + public void highlightPlant(@NotNull World world) { |
| 50 | + MatrixStack matrices = this.context.matrixStack(); |
| 51 | + |
| 52 | + if (matrices == null) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + Set<BlockPos> filteredBlocks = filterBlocks(blockPos -> world.getBlockState(blockPos).isOf(FERN) && world.getBlockState(blockPos.down()).isOf(PODZOL)); |
| 57 | + |
| 58 | + for (BlockPos filteredBlock : filteredBlocks) { |
| 59 | + drawOutline(0, 255, 0, 1, matrices, filteredBlock); |
| 60 | + drawOutline(0, 255, 255, 1, matrices, filteredBlock.down()); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + private @NotNull Set<BlockPos> filterBlocks(Predicate<BlockPos> predicate) { |
| 65 | + ClientPlayerEntity clientPlayerEntity = MinecraftClient.getInstance().player; |
| 66 | + |
| 67 | + if (clientPlayerEntity == null) { |
| 68 | + return Set.of(); |
| 69 | + } |
| 70 | + |
| 71 | + BlockPos playerPos = addonPlayer.getBlockPos(); |
| 72 | + BlockPos renderStartPos = playerPos.add(-RENDER_DISTANCE, -RENDER_DISTANCE, -RENDER_DISTANCE); |
| 73 | + BlockPos renderEndPos = playerPos.add(RENDER_DISTANCE, RENDER_DISTANCE, RENDER_DISTANCE); |
| 74 | + |
| 75 | + Set<BlockPos> positions = new HashSet<>(); |
| 76 | + |
| 77 | + for (BlockPos pos : iterate(renderStartPos, renderEndPos)) { |
| 78 | + if (predicate.test(pos)) { |
| 79 | + positions.add(pos); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + return positions; |
| 84 | + } |
| 85 | + |
| 86 | + private void drawOutline(int red, int green, int blue, int alpha, MatrixStack matrices, BlockPos pos) { |
| 87 | + MinecraftClient client = MinecraftClient.getInstance(); |
| 88 | + ClientWorld world = client.world; |
| 89 | + |
| 90 | + if (world == null) { |
| 91 | + return; |
| 92 | + } |
| 93 | + |
| 94 | + BufferBuilderStorage bufferBuilders = client.getBufferBuilders(); |
| 95 | + |
| 96 | + // Get the outline provider (special buffer that renders outlines visible through walls) |
| 97 | + OutlineVertexConsumerProvider outlineProvider = bufferBuilders.getOutlineVertexConsumers(); |
| 98 | + |
| 99 | + // Set the outline color (RGBA) |
| 100 | + bufferBuilders.getOutlineVertexConsumers().setColor(red, green, blue, alpha); |
| 101 | + |
| 102 | + matrices.push(); |
| 103 | + |
| 104 | + // Translate the rendering position relative to the camera, |
| 105 | + // so the block outline is drawn at the correct world coordinates |
| 106 | + Vec3d cameraPos = client.gameRenderer.getCamera().getPos(); |
| 107 | + matrices.translate(pos.getX() - cameraPos.x, pos.getY() - cameraPos.y, pos.getZ() - cameraPos.z); |
| 108 | + |
| 109 | + // Render the block state into the outline buffer |
| 110 | + client.getBlockRenderManager().renderBlockAsEntity( |
| 111 | + world.getBlockState(pos), // fetch the real block state at this position |
| 112 | + matrices, |
| 113 | + outlineProvider, |
| 114 | + 15728880, // full light value |
| 115 | + 0 // overlay |
| 116 | + ); |
| 117 | + |
| 118 | + matrices.pop(); |
| 119 | + |
| 120 | + // Flush the outline buffer so it actually gets drawn |
| 121 | + outlineProvider.draw(); |
| 122 | + } |
| 123 | +} |
0 commit comments