Files
src
build.gradle
gradle.properties
settings.gradle
Code
5 612
src/client/java/com/kodari/oreminer/SafeOreMiner.java
package com.kodari.oreminer;
import com.mojang.brigadier.arguments.StringArgumentType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
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.command.v2.FabricClientCommandSource;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.Minecraft;
import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.commands.SharedSuggestionProvider;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.inventory.ContainerInput;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.FallingBlock;
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.phys.BlockHitResult;
import net.minecraft.world.phys.HitResult;
import net.minecraft.world.phys.Vec3;
public final class SafeOreMiner implements ClientModInitializer {
private static final Direction[] HORIZONTAL = {
Direction.NORTH, Direction.SOUTH, Direction.WEST, Direction.EAST
};
private static final int MAX_CANDIDATES = 64;
private static final int MAX_ROUTE_NODES = 30000;
private Block desired;
private FabricClientCommandSource source;
private ClientLevel activeLevel;
private BlockPos target;
private List<BlockPos> path = List.of();
private int pathIndex;
private BlockPos miningBlock;
private int mineTicks;
private int stuckTicks;
private int placementTicks;
private double lastX, lastY, lastZ;
private boolean minerSprinting;
private BlockPos scanOrigin;
private int scanRadius, scanCursor;
private List<ChunkOffset> scanChunks;
private List<BlockPos> candidates;
private PriorityQueue<BlockPos> nearestCandidates;
private int candidateIndex;
private PriorityQueue<RouteNode> routeQueue;
private Map<BlockPos, Double> routeCosts;
private Map<BlockPos, BlockPos> routePrevious;
private BlockPos routeStart;
private BlockPos routeGoal;
private record RouteNode(BlockPos pos, double cost, double score) { }
private record ChunkOffset(int x, int z) { }
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) -> dispatcher.register(
ClientCommands.literal("automine")
.then(ClientCommands.literal("start")
.then(ClientCommands.argument("block", StringArgumentType.word())
.suggests((context, builder) -> SharedSuggestionProvider.suggestResource(BuiltInRegistries.BLOCK.keySet(), builder))
.executes(context -> start(context.getSource(), StringArgumentType.getString(context, "block")))))
.then(ClientCommands.literal("stop").executes(context -> {
stop(Minecraft.getInstance(), "Auto-miner stopped.");
return 1;
}))
));
ClientTickEvents.START_CLIENT_TICK.register(this::tick);
}
private int start(FabricClientCommandSource commandSource, String name) {
Minecraft client = Minecraft.getInstance();
Identifier id = Identifier.tryParse(name);
Block block = id == null ? null : BuiltInRegistries.BLOCK.getOptional(id).orElse(null);
if (client.player == null || client.level == null || client.gameMode == null) {
commandSource.sendError(Component.literal("Join a world before starting the auto-miner."));
return 0;
}
if (block == null || block == Blocks.AIR) {
commandSource.sendError(Component.literal("Unknown block ID: " + name));
return 0;
}
if (desired != null) stop(client, null);
desired = block;
source = commandSource;
activeLevel = client.level;
resetSearch();
commandSource.sendFeedback(Component.literal("Searching loaded chunks for " + id + ". Use /automine stop to cancel."));
return 1;
}
private void stop(Minecraft client, String message) {
releaseMovement(client);
if (miningBlock != null && client.gameMode != null) client.gameMode.stopDestroyBlock();
if (message != null && source != null) source.sendFeedback(Component.literal(message));
desired = null;
source = null;
activeLevel = null;
miningBlock = null;
resetSearch();
}
private void resetSearch() {
target = null;
path = List.of();
pathIndex = 0;
scanOrigin = null;
scanChunks = null;
candidates = null;
nearestCandidates = null;
routeQueue = null;
routeCosts = null;
routePrevious = null;
routeGoal = null;
mineTicks = 0;
stuckTicks = 0;
placementTicks = 0;
}
private void releaseMovement(Minecraft client) {
client.options.keyUp.setDown(false);
client.options.keyJump.setDown(false);
if (minerSprinting && client.player != null) client.player.setSprinting(false);
minerSprinting = false;
}
private void tick(Minecraft client) {
if (desired == null) return;
if (client.player == null || client.level != activeLevel || client.gameMode == null) {
stop(client, null);
return;
}
if (client.gui.screen() != null || client.gui.overlay() != null) {
releaseMovement(client);
if (miningBlock != null) client.gameMode.stopDestroyBlock();
miningBlock = null;
mineTicks = 0;
return;
}
ClientLevel level = client.level;
if (target == null) {
releaseMovement(client);
if (candidates == null || candidateIndex >= candidates.size()) {
if (scanChunks != null && scanCursor >= scanChunks.size()) {
stop(client, "No reachable matching block found in loaded chunks. Try moving closer or supplying building blocks.");
return;
}
scan(client);
return;
}
findRoute(client);
return;
}
if (!loaded(level, target) || level.getBlockState(target).getBlock() != desired) {
if (miningBlock != null) client.gameMode.stopDestroyBlock();
miningBlock = null;
releaseMovement(client);
resetSearch();
return;
}
if (pathIndex < path.size()) {
followPath(client);
return;
}
releaseMovement(client);
mineBlock(client, target);
}
private void scan(Minecraft client) {
ClientLevel level = client.level;
if (scanChunks == null) {
scanOrigin = client.player.blockPosition();
scanRadius = Math.max(0, level.getServerSimulationDistance());
scanChunks = new ArrayList<>();
for (int x = -scanRadius; x <= scanRadius; x++) {
for (int z = -scanRadius; z <= scanRadius; z++) {
scanChunks.add(new ChunkOffset(x, z));
}
}