Files
src
build.gradle
gradle.properties
settings.gradle
Code
8 366
src/client/java/com/kodari/locator/PrivateLocator.java
package com.kodari.locator;
import com.kodari.locator.mixin.RenderLayerInvoker;
import com.mojang.blaze3d.pipeline.BlendFunction;
import com.mojang.blaze3d.pipeline.RenderPipeline;
import com.mojang.blaze3d.platform.DepthTestFunction;
import com.mojang.blaze3d.vertex.VertexFormat;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gl.UniformType;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.DrawStyle;
import net.minecraft.client.render.RenderSetup;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.util.InputUtil;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.registry.RegistryKey;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.ChunkSection;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.debug.gizmo.GizmoDrawing;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public final class PrivateLocator implements ClientModInitializer {
private static PrivateLocator instance;
private static final int CHUNK_RADIUS = 32;
private static final int BLOCK_RADIUS = 100;
private boolean previousU, previousI, previousO, previousP;
private Scan scan;
private ClientWorld highlightWorld;
private BlockPos highlight;
private Block highlightFirst, highlightSecond;
private boolean spawnerHighlight;
private RenderLayer highlightLayer;
@Override
public void onInitializeClient() {
instance = this;
ClientTickEvents.END_CLIENT_TICK.register(this::tick);
}
public static void renderTarget() {
PrivateLocator locator = instance;
MinecraftClient client = MinecraftClient.getInstance();
if (locator == null || locator.highlight == null || client.player == null || client.world != locator.highlightWorld) return;
WorldChunk chunk = client.world.getChunkManager().getChunk(locator.highlight.getX() >> 4, locator.highlight.getZ() >> 4, ChunkStatus.FULL, false);
if (chunk == null) return;
var state = client.world.getBlockState(locator.highlight);
if (!state.isOf(locator.highlightFirst) && !state.isOf(locator.highlightSecond)
&& !(locator.highlightFirst == Blocks.GOLD_ORE && state.isOf(Blocks.NETHER_GOLD_ORE))) {
locator.highlight = null;
locator.highlightWorld = null;
return;
}
try (var scope = client.worldRenderer.startDrawingGizmos()) {
int color = locator.spawnerHighlight ? 0xFF00FFFF : 0xFFFF0000;
Vec3d start = client.gameRenderer.getCamera().getCameraPos().add(client.player.getRotationVec(1.0f).multiply(0.5));
GizmoDrawing.line(start, Vec3d.ofCenter(locator.highlight), color, 4.0f).ignoreOcclusion();
GizmoDrawing.box(new Box(locator.highlight), DrawStyle.stroked(color, 4.0f)).ignoreOcclusion();
}
}
private void tick(MinecraftClient client) {
if (client.player == null || client.world == null) {
scan = null;
highlight = null;
highlightWorld = null;
previousU = previousI = previousO = previousP = false;
return;
}
boolean u = pressed(client, GLFW.GLFW_KEY_U);
boolean i = pressed(client, GLFW.GLFW_KEY_I);
boolean o = pressed(client, GLFW.GLFW_KEY_O);
boolean p = pressed(client, GLFW.GLFW_KEY_P);
boolean f5 = pressed(client, GLFW.GLFW_KEY_F5);
boolean f6 = pressed(client, GLFW.GLFW_KEY_F6);
boolean f7 = pressed(client, GLFW.GLFW_KEY_F7);
if (client.currentScreen == null) {
if (f6 && u && !previousU) start(client, "Diamond", Blocks.DIAMOND_ORE, Blocks.DEEPSLATE_DIAMOND_ORE);
else if (f6 && i && !previousI) start(client, "Iron", Blocks.IRON_ORE, Blocks.DEEPSLATE_IRON_ORE);
else if (f6 && o && !previousO) start(client, "Lapis", Blocks.LAPIS_ORE, Blocks.DEEPSLATE_LAPIS_ORE);
else if (f6 && p && !previousP) start(client, "Redstone", Blocks.REDSTONE_ORE, Blocks.DEEPSLATE_REDSTONE_ORE);
else if (f5 && u && !previousU) start(client, "Spawner", Blocks.SPAWNER, Blocks.SPAWNER);
else if (f5 && i && !previousI) start(client, "Bell (possible village)", Blocks.BELL, Blocks.BELL);
else if (f5 && o && !previousO) start(client, "Biomes", null, null);
else if (f7 && u && !previousU) start(client, "Ancient debris", Blocks.ANCIENT_DEBRIS, Blocks.ANCIENT_DEBRIS);
else if (f7 && i && !previousI) start(client, "Coal", Blocks.COAL_ORE, Blocks.DEEPSLATE_COAL_ORE);
else if (f7 && o && !previousO) start(client, "Gold", Blocks.GOLD_ORE, Blocks.DEEPSLATE_GOLD_ORE);
else if (f7 && p && !previousP) message(client, "Unlooted chests cannot be reliably identified from client data.");
}
previousU = u;
previousI = i;
previousO = o;
previousP = p;
if (scan != null) {
if (scan.world != client.world) {
scan = null;
} else if (scan.step()) {
scan.report(client);
if (scan.first != null) {
highlight = scan.nearest;
highlightWorld = scan.world;
highlightFirst = scan.first;
highlightSecond = scan.second;
spawnerHighlight = scan.first == Blocks.SPAWNER;
}
scan = null;
}
}
}
private static boolean pressed(MinecraftClient client, int key) {
return InputUtil.isKeyPressed(client.getWindow(), key);
}
private void start(MinecraftClient client, String name, Block first, Block second) {
if (first != null) {
highlight = null;
highlightWorld = null;
}
scan = new Scan(client.world, client.player.getBlockPos(), name, first, second);
message(client, "Scanning received chunks for " + name + "... (new scan cancels the previous one)");
}
private static void message(MinecraftClient client, String text) {
client.player.sendMessage(Text.literal("[Locator] " + text), false);
}
private static final class Scan {
private final ClientWorld world;
private final BlockPos origin;
private final String name;
private final Block first, second;
private final List<int[]> chunks = new ArrayList<>();
private final Map<String, BlockPos> biomes = new LinkedHashMap<>();
private int index;
private BlockPos nearest;
private double nearestDistance = Double.MAX_VALUE;
private Scan(ClientWorld world, BlockPos origin, String name, Block first, Block second) {
this.world = world;
this.origin = origin;
this.name = name;
this.first = first;
this.second = second;
int centerX = origin.getX() >> 4;
int centerZ = origin.getZ() >> 4;
int radius = first == null || first == Blocks.BELL ? CHUNK_RADIUS : 7;
for (int dx = -radius; dx <= radius; dx++) {
for (int dz = -radius; dz <= radius; dz++) {
if (dx * dx + dz * dz <= radius * radius) chunks.add(new int[]{centerX + dx, centerZ + dz});
}
}
chunks.sort(Comparator.comparingInt(c -> {
int dx = c[0] - centerX, dz = c[1] - centerZ;
return dx * dx + dz * dz;
}));
}
private boolean step() {
int budget = first == null || first == Blocks.BELL ? 24 : 2;
while (budget-- > 0 && index < chunks.size()) {
int[] pos = chunks.get(index++);
WorldChunk chunk = world.getChunkManager().getChunk(pos[0], pos[1], ChunkStatus.FULL, false);
if (chunk == null) continue;
if (first == null) scanBiome(pos[0], pos[1]);
else scanBlocks(chunk, pos[0], pos[1]);
}
return index >= chunks.size();
}
private void scanBlocks(WorldChunk chunk, int chunkX, int chunkZ) {
ChunkSection[] sections = chunk.getSectionArray();
for (int s = 0; s < sections.length; s++) {
ChunkSection section = sections[s];
if (section.isEmpty()) continue;
if (!section.hasAny(state -> state.isOf(first) || state.isOf(second)
|| (first == Blocks.GOLD_ORE && state.isOf(Blocks.NETHER_GOLD_ORE)))) continue;
int baseY = chunk.getBottomY() + s * 16;
if (first != Blocks.BELL && (baseY > origin.getY() + BLOCK_RADIUS || baseY + 15 < origin.getY() - BLOCK_RADIUS)) continue;
for (int x = 0; x < 16; x++) for (int z = 0; z < 16; z++) {
int bx = chunkX * 16 + x, bz = chunkZ * 16 + z;
int dx = bx - origin.getX(), dz = bz - origin.getZ();
