Files
Base finder
src
build.gradle
gradle.properties
settings.gradle
Code
6 403
src/main/java/com/kodari/basefinder/BaseFinderClient.java
package com.kodari.basefinder;
import com.mojang.blaze3d.systems.RenderSystem;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderEvents;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.MobSpawnerBlockEntity;
import net.minecraft.block.spawner.MobSpawnerLogic;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.VertexFormat;
import net.minecraft.client.render.VertexFormats;
import net.minecraft.client.render.VertexRendering;
import net.minecraft.client.gl.ShaderProgramKeys;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityType;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.WorldChunk;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
public final class BaseFinderClient implements ClientModInitializer {
private static final Path CONFIG_FILE = Path.of("config", "basefinder.properties");
private static final int DEFAULT_RADIUS_CHUNKS = 16;
private static final int MIN_RADIUS_CHUNKS = 1;
private static final int MAX_RADIUS_CHUNKS = 32;
private static final int CHUNKS_PER_TICK = 1;
private static final int MAX_RENDERED_DETECTIONS = 2048;
private static final int CLUSTER_THRESHOLD = 8;
private static final int SUSPICIOUS_CHUNK_THRESHOLD = 48;
private static final int BASE_COLOR = 0x5CFF52;
private static final int STORAGE_COLOR = 0x28D8FF;
private static final int SPAWNER_COLOR = 0xFF4545;
private static final int CLUSTER_COLOR = 0xFFD447;
private static final int CHUNK_COLOR = 0xC56BFF;
private static final Set<String> EXPLICIT_BUILD_BLOCKS = Set.of(
"minecraft:glass",
"minecraft:obsidian",
"minecraft:cobblestone",
"minecraft:mossy_cobblestone",
"minecraft:stone_bricks",
"minecraft:cracked_stone_bricks",
"minecraft:mossy_stone_bricks",
"minecraft:chiseled_stone_bricks",
"minecraft:bricks",
"minecraft:quartz_block",
"minecraft:polished_deepslate",
"minecraft:deepslate_bricks",
"minecraft:deepslate_tiles",
"minecraft:nether_bricks",
"minecraft:red_nether_bricks",
"minecraft:purpur_block"
);
private static final RenderLayer XRAY_LINES = new RenderLayer(
"basefinder_xray_lines",
VertexFormats.LINES,
VertexFormat.DrawMode.LINES,
1536,
false,
false,
() -> {
RenderSystem.setShader(ShaderProgramKeys.RENDERTYPE_LINES);
RenderSystem.disableDepthTest();
},
RenderSystem::enableDepthTest
) {
};
private static final Set<Long> scannedChunks = new HashSet<>();
private static final Map<Long, List<Detection>> detectionsByChunk = new HashMap<>();
private static final ArrayDeque<Detection> chatQueue = new ArrayDeque<>();
private static ClientWorld scannedWorld;
private static int radiusChunks = DEFAULT_RADIUS_CHUNKS;
@Override
public void onInitializeClient() {
loadConfig();
ClientTickEvents.END_CLIENT_TICK.register(BaseFinderClient::onClientTick);
WorldRenderEvents.AFTER_ENTITIES.register(BaseFinderClient::renderDetections);
}
private static void loadConfig() {
Properties properties = new Properties();
try {
Files.createDirectories(CONFIG_FILE.getParent());
if (Files.notExists(CONFIG_FILE)) {
properties.setProperty("radius_chunks", Integer.toString(DEFAULT_RADIUS_CHUNKS));
try (var writer = Files.newBufferedWriter(CONFIG_FILE)) {
properties.store(writer, "Base Finder settings");
}
} else {
try (var reader = Files.newBufferedReader(CONFIG_FILE)) {
properties.load(reader);
}
}
radiusChunks = Math.max(MIN_RADIUS_CHUNKS, Math.min(MAX_RADIUS_CHUNKS,
Integer.parseInt(properties.getProperty("radius_chunks", Integer.toString(DEFAULT_RADIUS_CHUNKS)).trim())));
} catch (IOException | NumberFormatException exception) {
radiusChunks = DEFAULT_RADIUS_CHUNKS;
}
}
private static void onClientTick(MinecraftClient client) {
ClientPlayerEntity player = client.player;
ClientWorld world = client.world;
if (player == null || world == null) {
clearScanState();
scannedWorld = null;
return;
}
if (scannedWorld != world) {
clearScanState();
scannedWorld = world;
}
BlockPos playerPos = player.getBlockPos();
int centerChunkX = playerPos.getX() >> 4;
int centerChunkZ = playerPos.getZ() >> 4;
scannedChunks.removeIf(key -> !isWithinRadius(key, centerChunkX, centerChunkZ));
detectionsByChunk.keySet().removeIf(key -> !isWithinRadius(key, centerChunkX, centerChunkZ));
int chunksScanned = 0;
for (int offsetX = -radiusChunks; offsetX <= radiusChunks && chunksScanned < CHUNKS_PER_TICK; offsetX++) {
for (int offsetZ = -radiusChunks; offsetZ <= radiusChunks && chunksScanned < CHUNKS_PER_TICK; offsetZ++) {
int chunkX = centerChunkX + offsetX;
int chunkZ = centerChunkZ + offsetZ;
long key = chunkKey(chunkX, chunkZ);
if (scannedChunks.contains(key)) {
continue;
}
WorldChunk chunk = world.getChunkManager().getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
if (chunk == null) {
continue;
}
scannedChunks.add(key);
List<Detection> found = scanChunk(world, chunk, chunkX, chunkZ);
detectionsByChunk.put(key, found);
chatQueue.addAll(found);
chunksScanned++;
}
}
Detection detection = chatQueue.pollFirst();
if (detection != null) {
BlockPos pos = detection.pos();
player.sendMessage(Text.literal("[" + detection.type() + "] X: " + pos.getX()
+ " Y: " + pos.getY() + " Z: " + pos.getZ()), false);
}
}
private static List<Detection> scanChunk(ClientWorld world, WorldChunk chunk, int chunkX, int chunkZ) {
List<Detection> detections = new ArrayList<>();
Map<ClusterKey, ClusterAccumulator> clusters = new HashMap<>();
BlockPos.Mutable cursor = new BlockPos.Mutable();
int buildBlockCount = 0;
BlockPos firstBuildBlock = null;
int startX = chunkX << 4;
int startZ = chunkZ << 4;
for (int x = startX; x < startX + 16; x++) {
for (int z = startZ; z < startZ + 16; z++) {
for (int y = world.getBottomY(); y <= world.getTopYInclusive(); y++) {
cursor.set(x, y, z);
BlockState state = chunk.getBlockState(cursor);
if (state.isAir()) {
continue;
}
String blockId = Registries.BLOCK.getId(state.getBlock()).toString();
if (isStorage(blockId)) {
detections.add(new Detection("Storage", new BlockPos(x, y, z), STORAGE_COLOR));
} else if ("minecraft:spawner".equals(blockId)) {
BlockEntity blockEntity = chunk.getBlockEntity(cursor);
if (blockEntity instanceof MobSpawnerBlockEntity spawner) {