Files
src
build.gradle
gradle.properties
settings.gradle
Code
7 445
src/client/java/io/kodari/chunkhighlights/ChunkHighlightsClient.java
package io.kodari.chunkhighlights;
import com.mojang.blaze3d.platform.InputConstants;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientChunkEvents;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
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.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.gizmos.DrawableGizmoPrimitives;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import net.minecraft.world.level.entity.EntityTypeTest;
import net.minecraft.world.phys.AABB;
import net.minecraft.world.phys.Vec3;
import org.lwjgl.glfw.GLFW;
public final class ChunkHighlightsClient implements ClientModInitializer {
private static final int HIGHLIGHT_COLOR = 0xFFFFB000;
private static final float LINE_WIDTH = 2.5F;
private static final int[][] BOX_EDGES = {
{0, 1}, {0, 2}, {0, 4}, {1, 3}, {1, 5}, {2, 3},
{2, 6}, {3, 7}, {4, 5}, {4, 6}, {5, 7}, {6, 7}
};
private static final Set<Block> TARGET_BLOCKS = createTargetBlocks();
private static final KeyMapping TOGGLE_KEY = KeyMappingHelper.registerKeyMapping(
new KeyMapping(
"key.chunkhighlights.toggle",
InputConstants.Type.KEYSYM,
GLFW.GLFW_KEY_I,
KeyMapping.Category.MISC
)
);
private static final Map<ChunkKey, List<BlockPos>> blockTargets = new HashMap<>();
private static final Set<ChunkKey> dirtyChunks = new HashSet<>();
private static final Set<ArmorStand> armorStands = new HashSet<>();
private static Set<ChunkKey> activeChunks = Set.of();
private static ClientLevel trackedLevel;
private static boolean enabled;
@Override
public void onInitializeClient() {
ClientTickEvents.END_CLIENT_TICK.register(ChunkHighlightsClient::tick);
ClientChunkEvents.CHUNK_LOAD.register(ChunkHighlightsClient::onChunkLoad);
ClientChunkEvents.CHUNK_UNLOAD.register(ChunkHighlightsClient::onChunkUnload);
ClientEntityEvents.ENTITY_LOAD.register(ChunkHighlightsClient::onEntityLoad);
ClientEntityEvents.ENTITY_UNLOAD.register(ChunkHighlightsClient::onEntityUnload);
LevelRenderEvents.COLLECT_SUBMITS.register(ChunkHighlightsClient::renderHighlights);
}
private static void tick(Minecraft client) {
while (TOGGLE_KEY.consumeClick()) {
enabled = !enabled;
if (!enabled) {
clearTracking();
}
}
ClientLevel level = client.level;
if (level == null || client.player == null) {
trackedLevel = null;
clearTracking();
return;
}
if (trackedLevel != level) {
clearTracking();
trackedLevel = level;
}
if (!enabled) {
return;
}
ChunkPos playerChunk = client.player.chunkPosition();
List<ChunkKey> nearbyChunks = findNearbyChunks(playerChunk.x(), playerChunk.z());
Set<ChunkKey> nextActiveChunks = new LinkedHashSet<>(nearbyChunks);
if (!nextActiveChunks.equals(activeChunks)) {
activeChunks = nextActiveChunks;
blockTargets.keySet().retainAll(activeChunks);
dirtyChunks.retainAll(activeChunks);
for (ChunkKey key : activeChunks) {
if (!blockTargets.containsKey(key)) {
dirtyChunks.add(key);
}
}
refreshArmorStands(level);
}
rescanOneDirtyChunk(level);
armorStands.removeIf(stand -> !activeChunks.contains(keyOf(stand.chunkPosition())));
}
private static void renderHighlights(net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderContext context) {
if (!enabled || trackedLevel == null || Minecraft.getInstance().level != trackedLevel) {
return;
}
DrawableGizmoPrimitives gizmos = new DrawableGizmoPrimitives();
boolean hasHighlights = false;
for (ChunkKey key : activeChunks) {
List<BlockPos> positions = blockTargets.get(key);
if (positions == null) {
continue;
}
for (BlockPos pos : positions) {
addBox(gizmos, pos.getX(), pos.getY(), pos.getZ(), pos.getX() + 1.0, pos.getY() + 1.0, pos.getZ() + 1.0);
hasHighlights = true;
}
}
for (ArmorStand stand : armorStands) {
if (!activeChunks.contains(keyOf(stand.chunkPosition()))) {
continue;
}
AABB bounds = stand.getBoundingBox();
addBox(gizmos, bounds.minX, bounds.minY, bounds.minZ, bounds.maxX, bounds.maxY, bounds.maxZ);
hasHighlights = true;
}
if (hasHighlights) {
gizmos.submit(
context.submitNodeCollector(),
context.levelState().cameraRenderState,
true
);
}
}
private static void addBox(DrawableGizmoPrimitives gizmos, double minX, double minY, double minZ, double maxX, double maxY, double maxZ) {
Vec3[] corners = {
new Vec3(minX, minY, minZ),
new Vec3(maxX, minY, minZ),
new Vec3(minX, maxY, minZ),
new Vec3(maxX, maxY, minZ),
new Vec3(minX, minY, maxZ),
new Vec3(maxX, minY, maxZ),
new Vec3(minX, maxY, maxZ),
new Vec3(maxX, maxY, maxZ)
};
for (int[] edge : BOX_EDGES) {
gizmos.addLine(corners[edge[0]], corners[edge[1]], HIGHLIGHT_COLOR, LINE_WIDTH);
}
}
private static void onChunkLoad(ClientLevel level, LevelChunk chunk) {
if (!enabled || trackedLevel != level) {
return;
}
ChunkKey key = keyOf(chunk.getPos());
if (activeChunks.contains(key)) {
blockTargets.remove(key);
dirtyChunks.add(key);
addArmorStandsInChunk(level, key);
}
}
private static void onChunkUnload(ClientLevel level, LevelChunk chunk) {
if (trackedLevel != level) {
return;
}
ChunkKey key = keyOf(chunk.getPos());
blockTargets.remove(key);
dirtyChunks.remove(key);
armorStands.removeIf(stand -> keyOf(stand.chunkPosition()).equals(key));
}
private static void onEntityLoad(net.minecraft.world.entity.Entity entity, ClientLevel level) {
if (enabled && trackedLevel == level && entity instanceof ArmorStand stand && activeChunks.contains(keyOf(stand.chunkPosition()))) {
armorStands.add(stand);
}
}
private static void onEntityUnload(net.minecraft.world.entity.Entity entity, ClientLevel level) {
if (trackedLevel == level && entity instanceof ArmorStand stand) {
armorStands.remove(stand);
}
}
public static void onBlockChanged(ChunkPos pos) {
if (!enabled) {
return;
}