Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 325
src/client/java/com/kodari/kodariesp/KodariEspClient.java
package com.kodari.kodariesp;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.block.entity.BlockEntityType;
import net.minecraft.block.entity.ChestBlockEntity;
import net.minecraft.block.entity.MobSpawnerBlockEntity;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.AbstractClientPlayerEntity;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.client.render.DrawStyle;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.particle.ParticlesMode;
import net.minecraft.registry.Registries;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Box;
import net.minecraft.world.chunk.ChunkStatus;
import net.minecraft.world.chunk.WorldChunk;
import net.minecraft.world.debug.gizmo.GizmoDrawing;
import org.lwjgl.glfw.GLFW;
public final class KodariEspClient implements ClientModInitializer {
private static final String MOD_ID = "kodariesp";
private static final int SCAN_INTERVAL_TICKS = 10;
private static final int BLOCK_SCAN_CHUNK_RADIUS = 3;
private static final double BLOCK_RANGE = 48.0;
private static final double PLAYER_RANGE = 64.0;
private static final int CHEST_COLOR = 0xFFFFC247;
private static final int SPAWNER_COLOR = 0xFFFF4D8D;
private static final int PLAYER_COLOR = 0xFF45D9FF;
private static final KeyBinding.Category KEY_CATEGORY = KeyBinding.Category.create(Identifier.of(MOD_ID, "esp"));
private static final KeyBinding TOGGLE_PLAYERS = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.kodariesp.toggle_players", GLFW.GLFW_KEY_P, KEY_CATEGORY));
private static final KeyBinding TOGGLE_CHESTS = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.kodariesp.toggle_chests", GLFW.GLFW_KEY_O, KEY_CATEGORY));
private static final KeyBinding TOGGLE_SPAWNERS = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.kodariesp.toggle_spawners", GLFW.GLFW_KEY_I, KEY_CATEGORY));
private static final KeyBinding TOGGLE_PERFORMANCE = KeyBindingHelper.registerKeyBinding(
new KeyBinding("key.kodariesp.toggle_performance", GLFW.GLFW_KEY_F8, KEY_CATEGORY));
private static boolean playersEnabled = true;
private static boolean chestsEnabled = true;
private static boolean spawnersEnabled = true;
private static boolean performanceMode;
private static ParticlesMode previousParticleMode;
private static Double previousEntityDistance;
private static boolean scanRequested = true;
private static int ticksSinceScan;
private static ClientWorld scannedWorld;
private static List<BlockPos> chestTargets = List.of();
private static List<BlockPos> spawnerTargets = List.of();
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(KodariEspClient::onClientTick);
}
private static void onClientTick(MinecraftClient client) {
while (TOGGLE_PLAYERS.wasPressed()) {
playersEnabled = !playersEnabled;
notifyToggle(client.player, "Player", playersEnabled);
}
while (TOGGLE_CHESTS.wasPressed()) {
chestsEnabled = !chestsEnabled;
scanRequested = true;
notifyToggle(client.player, "Chest", chestsEnabled);
}
while (TOGGLE_SPAWNERS.wasPressed()) {
spawnersEnabled = !spawnersEnabled;
scanRequested = true;
notifyToggle(client.player, "Spawner", spawnersEnabled);
}
while (TOGGLE_PERFORMANCE.wasPressed()) {
setPerformanceMode(client, !performanceMode);
notifyToggle(client.player, "Performance mode", performanceMode);
}
if (client.world == null || client.player == null) {
scannedWorld = null;
chestTargets = List.of();
spawnerTargets = List.of();
scanRequested = true;
ticksSinceScan = 0;
return;
}
if (client.world != scannedWorld) {
scannedWorld = client.world;
scanRequested = true;
}
if (!chestsEnabled && !spawnersEnabled) {
chestTargets = List.of();
spawnerTargets = List.of();
ticksSinceScan = 0;
scanRequested = true;
} else {
ticksSinceScan++;
if (scanRequested || ticksSinceScan >= SCAN_INTERVAL_TICKS) {
refreshTargets(client.world, client.player);
scanRequested = false;
ticksSinceScan = 0;
}
}
try (GizmoDrawing.CollectorScope ignored = client.worldRenderer.startDrawingGizmos()) {
drawEsp();
}
}
private static void refreshTargets(ClientWorld world, ClientPlayerEntity player) {
List<BlockPos> chests = new ArrayList<>();
List<BlockPos> spawners = new ArrayList<>();
int centerChunkX = ((int) Math.floor(player.getX())) >> 4;
int centerChunkZ = ((int) Math.floor(player.getZ())) >> 4;
for (int chunkX = centerChunkX - BLOCK_SCAN_CHUNK_RADIUS; chunkX <= centerChunkX + BLOCK_SCAN_CHUNK_RADIUS; chunkX++) {
for (int chunkZ = centerChunkZ - BLOCK_SCAN_CHUNK_RADIUS; chunkZ <= centerChunkZ + BLOCK_SCAN_CHUNK_RADIUS; chunkZ++) {
WorldChunk chunk = world.getChunkManager().getChunk(chunkX, chunkZ, ChunkStatus.FULL, false);
if (chunk == null) {
continue;
}
for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
Identifier blockId = Registries.BLOCK.getId(blockEntity.getCachedState().getBlock());
Identifier typeId = BlockEntityType.getId(blockEntity.getType());
String blockPath = blockId.getPath().toLowerCase(Locale.ROOT);
String typePath = typeId == null ? "" : typeId.getPath().toLowerCase(Locale.ROOT);
String className = blockEntity.getClass().getSimpleName().toLowerCase(Locale.ROOT);
if (chestsEnabled && (blockEntity instanceof ChestBlockEntity
|| blockPath.contains("chest") || typePath.contains("chest") || className.contains("chest"))) {
chests.add(blockEntity.getPos());
}
if (spawnersEnabled && (blockEntity instanceof MobSpawnerBlockEntity
|| blockPath.contains("spawner") || typePath.contains("spawner") || className.contains("spawner"))) {
spawners.add(blockEntity.getPos());
}
}
}
}
chestTargets = chests;
spawnerTargets = spawners;
}
private static void drawEsp() {
MinecraftClient client = MinecraftClient.getInstance();
ClientWorld world = client.world;
ClientPlayerEntity localPlayer = client.player;
if (world == null || localPlayer == null) {
return;
}
double originX = localPlayer.getX();
double originY = localPlayer.getY();
double originZ = localPlayer.getZ();
double blockRangeSquared = BLOCK_RANGE * BLOCK_RANGE;
double playerRangeSquared = PLAYER_RANGE * PLAYER_RANGE;
if (playersEnabled) {
for (AbstractClientPlayerEntity target : world.getPlayers()) {
if (target == localPlayer || !withinRange(target.getX(), target.getY(), target.getZ(), originX, originY, originZ, playerRangeSquared)) {
continue;
}
GizmoDrawing.box(target.getBoundingBox(), DrawStyle.stroked(PLAYER_COLOR, 2.0F))
.ignoreOcclusion()
.withLifespan(100);
}
}
if (chestsEnabled) {
drawBlockTargets(chestTargets, CHEST_COLOR, originX, originY, originZ, blockRangeSquared);
}
if (spawnersEnabled) {
drawBlockTargets(spawnerTargets, SPAWNER_COLOR, originX, originY, originZ, blockRangeSquared);
}
}
private static void drawBlockTargets(List<BlockPos> targets, int color, double originX, double originY, double originZ, double rangeSquared) {
for (BlockPos pos : targets) {
double x = pos.getX() + 0.5;
double y = pos.getY() + 0.5;
double z = pos.getZ() + 0.5;
if (!withinRange(x, y, z, originX, originY, originZ, rangeSquared)) {
continue;
}
GizmoDrawing.box(new Box(pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1.0, pos.getY() + 1.0, pos.getZ() + 1.0),
DrawStyle.stroked(color, 2.0F))
.ignoreOcclusion()