Files
src
build.gradle
gradle.properties
manifest.kod
settings.gradle
Code
18 983
src/client/java/com/kodari/chestfinder/client/ChestFinderClient.java
package com.kodari.chestfinder.client;
import io.wispforest.owo.ui.component.LabelComponent;
import io.wispforest.owo.ui.component.UIComponents;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.container.UIContainers;
import io.wispforest.owo.ui.core.Insets;
import io.wispforest.owo.ui.core.Positioning;
import io.wispforest.owo.ui.core.Sizing;
import io.wispforest.owo.ui.core.Surface;
import io.wispforest.owo.ui.hud.Hud;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommands;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.rendering.v1.level.LevelRenderEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.renderer.blockentity.BeaconRenderer;
import net.minecraft.core.BlockPos;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraft.world.level.chunk.status.ChunkStatus;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class ChestFinderClient implements ClientModInitializer {
static final Map<BlockPos, Waypoint> WAYPOINTS = new LinkedHashMap<>();
private static ClientLevel waypointLevel;
private static LabelComponent hudLabel;
private static String lastHud = "";
private static int ticks;
static BlockPos navigationTarget;
static final int[] COLORS = {0xff55ffff, 0xffff5555, 0xff55ff55, 0xffffff55, 0xffff55ff, 0xff5599ff};
static final String[] COLOR_NAMES = {"Cyan", "Red", "Green", "Yellow", "Pink", "Blue"};
static boolean showBeams = true;
record Chest(BlockPos pos, String type, double distance) { }
record Waypoint(String type, boolean enabled, int color) {
Waypoint(String type, boolean enabled) { this(type, enabled, 0); }
}
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, context) ->
dispatcher.register(ClientCommands.literal("chestfinder").executes(command -> {
Minecraft.getInstance().gui.setScreen(new ChestFinderScreen());
return 1;
})));
ClientCommandRegistrationCallback.EVENT.register((dispatcher, context) ->
dispatcher.register(ClientCommands.literal("freecam").executes(command -> {
boolean enabled = Freecam.toggle(Minecraft.getInstance());
command.getSource().sendFeedback(Component.literal(enabled
? "Freecam enabled: movement keys fly, jump rises, sneak descends. /freecam to return."
: "Freecam disabled."));
return 1;
})));
ClientCommandRegistrationCallback.EVENT.register((dispatcher, context) ->
dispatcher.register(ClientCommands.literal("combat").executes(command -> {
Minecraft.getInstance().gui.setScreen(new CombatScreen());
return 1;
})));
Hud.add(Identifier.fromNamespaceAndPath("chestfinder", "waypoints"), () -> {
hudLabel = UIComponents.label(Component.literal(""));
FlowLayout panel = UIContainers.verticalFlow(Sizing.content(), Sizing.content());
panel.child(hudLabel);
panel.padding(Insets.of(4));
panel.surface(Surface.DARK_PANEL);
panel.margins(Insets.of(5));
panel.positioning(Positioning.relative(100, 0));
return panel;
});
LevelRenderEvents.COLLECT_SUBMITS.register(context -> {
if (!showBeams) return;
var camera = context.levelState().cameraRenderState.pos;
var pose = context.poseStack();
for (var entry : WAYPOINTS.entrySet()) {
if (!entry.getValue().enabled()) continue;
BlockPos pos = entry.getKey();
pose.pushPose();
pose.translate(pos.getX() - camera.x, pos.getY() - camera.y, pos.getZ() - camera.z);
BeaconRenderer.submitBeaconBeam(pose, context.submitNodeCollector(), BeaconRenderer.BEAM_LOCATION,
1.0F, ticks, 0, 2048, COLORS[entry.getValue().color()], 0.5F, 0.7F);
pose.popPose();
}
});
ClientTickEvents.END_CLIENT_TICK.register(client -> {
Freecam.tick(client);
CombatHelpers.tick(client);
if (client.level != waypointLevel) {
WAYPOINTS.clear();
navigationTarget = null;
waypointLevel = client.level;
}
if (++ticks % 10 != 0 || hudLabel == null) return;
String text = waypointText(client);
if (!text.equals(lastHud)) {
hudLabel.text(Component.literal(text));
lastHud = text;
}
});
}
static List<Chest> scan(Minecraft client) {
List<Chest> found = new ArrayList<>();
if (client.level == null || client.player == null) return found;
BlockPos player = client.player.blockPosition();
int cx = player.getX() >> 4;
int cz = player.getZ() >> 4;
int radius = client.options.getEffectiveRenderDistance() + 3;
for (int x = cx - radius; x <= cx + radius; x++) {
for (int z = cz - radius; z <= cz + radius; z++) {
LevelChunk chunk = client.level.getChunkSource().getChunk(x, z, ChunkStatus.FULL, false);
if (chunk == null) continue;
for (BlockPos pos : chunk.getBlockEntities().keySet()) {
var block = chunk.getBlockState(pos).getBlock();
String type = block == Blocks.CHEST ? "Chest" : block == Blocks.TRAPPED_CHEST ? "Trapped chest" : block == Blocks.ENDER_CHEST ? "Ender chest" : null;
if (type != null) found.add(new Chest(pos, type, distance(player, pos)));
}
}
}
found.sort(Comparator.comparingDouble(Chest::distance));
return found;
}
static final class BlockScan {
final List<Chest> results = new ArrayList<>();
final Map<Block, String> matchingNames = new HashMap<>();
final String query;
final ClientLevel level;
final BlockPos origin;
final int minX, maxX, minZ, maxZ;
int x, z, section;
boolean done;
boolean truncated;
BlockScan(Minecraft client, String query) {
this.query = query.toLowerCase(java.util.Locale.ROOT).trim();
this.level = client.level;
this.origin = client.player == null ? BlockPos.ZERO : client.player.blockPosition();
int radius = client.options.getEffectiveRenderDistance() + 3;
minX = (origin.getX() >> 4) - radius;
maxX = (origin.getX() >> 4) + radius;
minZ = (origin.getZ() >> 4) - radius;
maxZ = (origin.getZ() >> 4) + radius;
x = minX;
z = minZ;
done = level == null || client.player == null || this.query.isEmpty();
}
void tick(Minecraft client) {
if (done) return;
if (client.level != level) { done = true; return; }
long deadline = System.nanoTime() + 4_000_000L;
for (int budget = 0; budget < 64 && !done && (budget == 0 || System.nanoTime() < deadline); budget++) {
LevelChunk chunk = level.getChunkSource().getChunk(x, z, ChunkStatus.FULL, false);
if (chunk != null && section < chunk.getSections().length) {
var part = chunk.getSections()[section];
if (part.maybeHas(state -> !matchingName(state.getBlock()).isEmpty())) {
for (int ly = 0; ly < 16 && !done; ly++) for (int lz = 0; lz < 16 && !done; lz++) for (int lx = 0; lx < 16; lx++) {
var block = part.getBlockState(lx, ly, lz).getBlock();
String name = matchingName(block);
if (name.isEmpty()) continue;
BlockPos pos = new BlockPos((x << 4) + lx, chunk.getMinY() + section * 16 + ly, (z << 4) + lz);
results.add(new Chest(pos, name, distance(origin, pos)));
if (results.size() >= 1000) { truncated = true; done = true; break; }
}
}
section++;
}
if (chunk == null || section >= chunk.getSections().length) {
section = 0;
if (++z > maxZ) { z = minZ; if (++x > maxX) done = true; }
}
}
if (done) results.sort(Comparator.comparingDouble(Chest::distance));
}
private String matchingName(Block block) {
return matchingNames.computeIfAbsent(block, value -> {
String id = BuiltInRegistries.BLOCK.getKey(value).toString();
String name = value.getName().getString();
return id.toLowerCase(java.util.Locale.ROOT).contains(query)
|| name.toLowerCase(java.util.Locale.ROOT).contains(query)
? name + " (" + id + ")" : "";
});
}
}