Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 345
src/client/java/com/example/respawnanchorhighlighter/RespawnAnchorHighlighterClient.java
package com.example.respawnanchorhighlighter;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import com.mojang.blaze3d.PrimitiveTopology;
import com.mojang.blaze3d.pipeline.BlendFunction;
import com.mojang.blaze3d.pipeline.ColorTargetState;
import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.blaze3d.vertex.VertexConsumer;
import com.mojang.blaze3d.vertex.DefaultVertexFormat;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderContext;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.BindGroupLayouts;
import net.minecraft.client.renderer.rendertype.RenderSetup;
import net.minecraft.client.renderer.rendertype.RenderType;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.phys.Vec3;
import net.minecraft.world.level.block.Block;
import com.example.respawnanchorhighlighter.mixin.RenderTypeAccessor;
public class RespawnAnchorHighlighterClient implements ClientModInitializer {
private static final int HORIZONTAL_RADIUS = 64;
private static final int VERTICAL_RADIUS = 64;
private static final int REFRESH_INTERVAL_TICKS = 20;
private static final Set<BlockPos> NO_POSITIONS = Set.of();
private static final RenderPipeline FINDER_LINES_PIPELINE = RenderPipeline.builder()
.withLocation("block_finder:finder_lines")
.withBindGroupLayout(BindGroupLayouts.GLOBALS)
.withBindGroupLayout(BindGroupLayouts.MATRICES_PROJECTION)
.withBindGroupLayout(BindGroupLayouts.FOG)
.withVertexShader("core/rendertype_lines")
.withFragmentShader("core/rendertype_lines")
.withColorTargetState(new ColorTargetState(BlendFunction.TRANSLUCENT))
.withCull(false)
.withVertexBinding(0, DefaultVertexFormat.POSITION_COLOR_NORMAL_LINE_WIDTH)
.withPrimitiveTopology(PrimitiveTopology.LINES)
.withDepthStencilState(Optional.empty())
.build();
private static final RenderType FINDER_LINES = RenderTypeAccessor.blockFinder$create(
"block_finder_finder_lines",
RenderSetup.builder(FINDER_LINES_PIPELINE).createRenderSetup()
);
private static ClientLevel scannedLevel;
private static Set<BlockPos> foundPositions = NO_POSITIONS;
private static Set<Block> activeTargets = Set.of();
private static int ticksUntilRefresh;
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, buildContext) -> dispatcher.register(
ClientCommands.literal("find")
.then(ClientCommands.literal("clear").executes(context -> {
TargetBlockList.clear();
resetTargets();
context.getSource().sendFeedback(Component.literal("Block finder targets cleared."));
return 1;
}))
.then(ClientCommands.argument("block_id", StringArgumentType.word()).executes(context -> {
String input = StringArgumentType.getString(context, "block_id");
Identifier id = Identifier.tryParse(input);
if (id == null || BuiltInRegistries.BLOCK.getOptional(id).isEmpty()) {
context.getSource().sendError(Component.literal("Unknown block ID: " + input));
return 0;
}
if (!TargetBlockList.add(id)) {
context.getSource().sendFeedback(Component.literal("Already finding " + id + "."));
return 1;
}
resetTargets();
context.getSource().sendFeedback(Component.literal("Added " + id + " to the block finder."));
return 1;
}))
));
ClientTickEvents.END_CLIENT_TICK.register(RespawnAnchorHighlighterClient::refreshTargets);
LevelRenderEvents.COLLECT_SUBMITS.register(RespawnAnchorHighlighterClient::renderTargets);
}
private static void resetTargets() {
activeTargets = TargetBlockList.resolveTargets();
foundPositions = NO_POSITIONS;
ticksUntilRefresh = 0;
}
private static void refreshTargets(Minecraft client) {
if (client.level == null || client.player == null) {
scannedLevel = null;
foundPositions = NO_POSITIONS;
ticksUntilRefresh = 0;
return;
}
if (client.level != scannedLevel) {
scannedLevel = client.level;
foundPositions = NO_POSITIONS;
ticksUntilRefresh = 0;
}
if (ticksUntilRefresh-- > 0) {
return;
}
ticksUntilRefresh = REFRESH_INTERVAL_TICKS;
ClientLevel level = client.level;
activeTargets = TargetBlockList.resolveTargets();
if (activeTargets.isEmpty()) {
foundPositions = NO_POSITIONS;
return;
}
BlockPos center = client.player.blockPosition();
int minX = center.getX() - HORIZONTAL_RADIUS;
int maxX = center.getX() + HORIZONTAL_RADIUS;
int minY = Math.max(level.getMinY(), center.getY() - VERTICAL_RADIUS);
int maxY = Math.min(level.getMaxY(), center.getY() + VERTICAL_RADIUS);
int minZ = center.getZ() - HORIZONTAL_RADIUS;
int maxZ = center.getZ() + HORIZONTAL_RADIUS;
BlockPos.MutableBlockPos scanPosition = new BlockPos.MutableBlockPos(0, 0, 0);
Set<BlockPos> found = new HashSet<>();
for (int x = minX; x <= maxX; x++) {
for (int y = minY; y <= maxY; y++) {
for (int z = minZ; z <= maxZ; z++) {
scanPosition.set(x, y, z);
if (level.isLoaded(scanPosition) && activeTargets.contains(level.getBlockState(scanPosition).getBlock())) {
found.add(scanPosition.immutable());
}
}
}
}
foundPositions = Set.copyOf(found);
}
private static void renderTargets(LevelRenderContext context) {
PoseStack poseStack = context.poseStack();
Minecraft client = Minecraft.getInstance();
if (poseStack == null || client.level == null || client.level != scannedLevel || foundPositions.isEmpty()) {
return;
}
ClientLevel level = client.level;
Vec3 cameraPosition = context.levelState().cameraRenderState.pos;
for (BlockPos position : foundPositions) {
if (!level.isLoaded(position) || !activeTargets.contains(level.getBlockState(position).getBlock())) {
continue;
}
float x = (float) (position.getX() - cameraPosition.x());
float y = (float) (position.getY() - cameraPosition.y());
float z = (float) (position.getZ() - cameraPosition.z());
context.submitNodeCollector().submitCustomGeometry(poseStack, FINDER_LINES, (pose, buffer) -> drawTargetBox(pose, buffer, x, y, z));
}
}
private static void drawTargetBox(PoseStack.Pose pose, VertexConsumer buffer, float x, float y, float z) {
float min = 0.002F;
float max = 0.998F;
float x0 = x + min;
float y0 = y + min;
float z0 = z + min;
float x1 = x + max;
float y1 = y + max;
float z1 = z + max;
addLine(pose, buffer, x0, y0, z0, x1, y0, z0);
addLine(pose, buffer, x1, y0, z0, x1, y0, z1);
addLine(pose, buffer, x1, y0, z1, x0, y0, z1);
addLine(pose, buffer, x0, y0, z1, x0, y0, z0);
addLine(pose, buffer, x0, y1, z0, x1, y1, z0);
addLine(pose, buffer, x1, y1, z0, x1, y1, z1);
addLine(pose, buffer, x1, y1, z1, x0, y1, z1);
addLine(pose, buffer, x0, y1, z1, x0, y1, z0);
addLine(pose, buffer, x0, y0, z0, x0, y1, z0);
addLine(pose, buffer, x1, y0, z0, x1, y1, z0);
addLine(pose, buffer, x1, y0, z1, x1, y1, z1);
addLine(pose, buffer, x0, y0, z1, x0, y1, z1);
}
private static void addLine(PoseStack.Pose pose, VertexConsumer buffer, float x1, float y1, float z1, float x2, float y2, float z2) {
buffer.addVertex(pose, x1, y1, z1)
.setColor(1.0F, 0.58F, 0.12F, 1.0F)
.setNormal(pose, 0.0F, 1.0F, 0.0F)
.setLineWidth(2.5F);