Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 186
src/client/java/com/kodari/chestesp/ChestEspClient.java
package com.kodari.chestesp;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.hud.HudElementRegistry;
import net.minecraft.client.DeltaTracker;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.Identifier;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.ChestBlockEntity;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import net.minecraft.world.phys.Vec3;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class ChestEspClient implements ClientModInitializer {
private static final int SEARCH_RADIUS = 64;
private static final int MAX_DISPLAYED_CHESTS = 8;
private static final long SCAN_INTERVAL_NANOS = 500_000_000L;
private static final int TEXT_COLOR = 0xFFFFAA00;
private static final int SHADOW_COLOR = 0xFFFFFFFF;
private static ClientLevel cachedLevel;
private static long lastScanNanos;
private static List<ChestMarker> cachedChests = List.of();
@Override
public void onInitializeClient() {
HudElementRegistry.addLast(
Identifier.fromNamespaceAndPath("chestesp", "chest_list"),
ChestEspClient::renderHud
);
}
private static void renderHud(GuiGraphicsExtractor graphics, DeltaTracker deltaTracker) {
Minecraft minecraft = Minecraft.getInstance();
ClientLevel level = minecraft.level;
var player = minecraft.player;
if (level == null || player == null) {
cachedLevel = level;
cachedChests = List.of();
return;
}
long now = System.nanoTime();
if (level != cachedLevel || now - lastScanNanos >= SCAN_INTERVAL_NANOS) {
refreshChests(level, player.position());
cachedLevel = level;
lastScanNanos = now;
}
graphics.text(minecraft.font, "Láda ESP · " + SEARCH_RADIUS + " blokk", 8, 8, TEXT_COLOR, true);
if (cachedChests.isEmpty()) {
graphics.text(minecraft.font, "Nincs láda a betöltött chunkokban.", 8, 20, SHADOW_COLOR, true);
return;
}
int displayed = Math.min(cachedChests.size(), MAX_DISPLAYED_CHESTS);
for (int i = 0; i < displayed; i++) {
ChestMarker chest = cachedChests.get(i);
int distance = (int) Math.round(Math.sqrt(chest.distanceSquared()));
String label = "Láda %d %d %d · %d blokk".formatted(chest.x(), chest.y(), chest.z(), distance);
graphics.text(minecraft.font, label, 8, 20 + i * 12, SHADOW_COLOR, true);
}
}
private static void refreshChests(ClientLevel level, Vec3 playerPosition) {
int playerBlockX = (int) Math.floor(playerPosition.x);
int playerBlockZ = (int) Math.floor(playerPosition.z);
int centerChunkX = Math.floorDiv(playerBlockX, 16);
int centerChunkZ = Math.floorDiv(playerBlockZ, 16);
int chunkRadius = (SEARCH_RADIUS + 15) / 16;
double maxDistanceSquared = (double) SEARCH_RADIUS * SEARCH_RADIUS;
List<ChestMarker> found = new ArrayList<>();
for (int chunkX = centerChunkX - chunkRadius; chunkX <= centerChunkX + chunkRadius; chunkX++) {
for (int chunkZ = centerChunkZ - chunkRadius; chunkZ <= centerChunkZ + chunkRadius; chunkZ++) {
LevelChunk chunk = level.getChunkSource().getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
if (chunk == null) {
continue;
}
for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
if (!(blockEntity instanceof ChestBlockEntity)) {
continue;
}
BlockPos pos = blockEntity.getBlockPos();
double dx = pos.getX() + 0.5 - playerPosition.x;
double dy = pos.getY() + 0.5 - playerPosition.y;
double dz = pos.getZ() + 0.5 - playerPosition.z;
double distanceSquared = dx * dx + dy * dy + dz * dz;
if (distanceSquared <= maxDistanceSquared) {
found.add(new ChestMarker(pos.getX(), pos.getY(), pos.getZ(), distanceSquared));
}
}
}
}
found.sort(Comparator.comparingDouble(ChestMarker::distanceSquared));
cachedChests = List.copyOf(found);
}
private record ChestMarker(int x, int y, int z, double distanceSquared) {
}
}