Files
src
BlockESP
build.gradle
gradle.properties
settings.gradle
Code
7 194
src/client/java/com/kodari/blockvision/client/BlockVisionClient.java
package com.kodari.blockvision.client;
import com.mojang.blaze3d.platform.InputConstants;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keymapping.v1.KeyMappingHelper;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;
import net.minecraft.client.KeyMapping;
import net.minecraft.client.Minecraft;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.gizmos.GizmoStyle;
import net.minecraft.gizmos.Gizmos;
import net.minecraft.network.chat.Component;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import java.util.ArrayList;
import java.util.List;
public class BlockVisionClient implements ClientModInitializer {
private static final int SCAN_RADIUS = 24;
private static final int SCAN_INTERVAL_TICKS = 10;
private static final KeyMapping TOGGLE_KEY = KeyMappingHelper.registerKeyMapping(
new KeyMapping("key.blockvision.toggle", InputConstants.Type.KEYSYM, 88, KeyMapping.Category.MISC));
private static final KeyMapping SELECT_KEY = KeyMappingHelper.registerKeyMapping(
new KeyMapping("key.blockvision.select", InputConstants.Type.KEYSYM, 66, KeyMapping.Category.MISC));
private static boolean enabled;
private static Block selectedBlock;
private static List<BlockPos> matchingBlocks = List.of();
private static Object scannedLevel;
private static int ticksSinceScan = SCAN_INTERVAL_TICKS;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(BlockVisionClient::onClientTick);
LevelRenderEvents.BEFORE_GIZMOS.register(context -> {
if (!enabled || Minecraft.getInstance().level != scannedLevel) {
return;
}
for (BlockPos pos : matchingBlocks) {
Gizmos.cuboid(pos, GizmoStyle.fill(0x66FF9A00)).setAlwaysOnTop();
}
});
}
private static void onClientTick(Minecraft client) {
while (TOGGLE_KEY.consumeClick()) {
enabled = !enabled;
ticksSinceScan = SCAN_INTERVAL_TICKS;
if (!enabled) {
matchingBlocks = List.of();
}
if (client.player != null) {
String state = enabled ? "enabled" : "disabled";
String selection = selectedBlock == null ? " Select a block with B first." : "";
client.player.sendSystemMessage(Component.literal("Block Vision " + state + "." + selection));
}
}
while (SELECT_KEY.consumeClick()) {
if (client.player == null || client.level == null) {
continue;
}
if (client.hitResult instanceof BlockHitResult hit && hit.getType() == HitResult.Type.BLOCK) {
selectedBlock = client.level.getBlockState(hit.getBlockPos()).getBlock();
matchingBlocks = List.of();
ticksSinceScan = SCAN_INTERVAL_TICKS;
client.player.sendSystemMessage(Component.literal(
"Selected " + BuiltInRegistries.BLOCK.getKey(selectedBlock) + ". Press X to toggle X-ray."));
} else {
client.player.sendSystemMessage(Component.literal("Aim at a block and press B to select it."));
}
}
if (!enabled || client.level == null || client.player == null || selectedBlock == null) {
matchingBlocks = List.of();
scannedLevel = client.level;
ticksSinceScan = 0;
return;
}
if (client.level != scannedLevel || ticksSinceScan >= SCAN_INTERVAL_TICKS) {
scanForSelectedBlock(client);
ticksSinceScan = 0;
} else {
ticksSinceScan++;
}
}
private static void scanForSelectedBlock(Minecraft client) {
BlockPos center = client.player.blockPosition();
int radiusSquared = SCAN_RADIUS * SCAN_RADIUS;
List<BlockPos> found = new ArrayList<>();
for (int x = center.getX() - SCAN_RADIUS; x <= center.getX() + SCAN_RADIUS; x++) {
for (int y = center.getY() - SCAN_RADIUS; y <= center.getY() + SCAN_RADIUS; y++) {
for (int z = center.getZ() - SCAN_RADIUS; z <= center.getZ() + SCAN_RADIUS; z++) {
int dx = x - center.getX();
int dy = y - center.getY();
int dz = z - center.getZ();
if (dx * dx + dy * dy + dz * dz > radiusSquared) {
continue;
}
BlockPos pos = new BlockPos(x, y, z);
if (client.level.getBlockState(pos).getBlock() == selectedBlock) {
found.add(pos);
}
}
}
}
matchingBlocks = found;
scannedLevel = client.level;
}
}