Files
pepa-pi-bot/runtime/skills/gather-stone.js
T
mayatnikovandClaude Opus 4.7 86f0c1799e fix(runtime): pin MC_VERSION=1.21.4 + ground-truth probe + close-loop dig
Two-pronged response to user-confirmed "bot stands still, doesn't actually
chop" on play.xmatic.team:

1. Pin protocol — .env now sets MC_VERSION=1.21.4. minecraft-data has
   wrong packet ID mappings for protocol 775 (server 26.1.2 via
   ViaBackwards 5.9.1) — see mineflayer#3888 and #3717. 1.21.5 also has
   an enchants decoder bug that breaks bot.dig. 1.21.4 is the last
   protocol mineflayer 4.37.1 can speak cleanly through VIA.

2. Don't trust dig success — runtime/actions.js chopNearestTree and
   runtime/skills/gather-stone.js now lookAt(face center)+forceLook,
   await collectBlock, then re-read the target block. If the log/stone
   is STILL there, return ok:false code:"silent_dig_failure" and
   blacklist the position. Prevents the curriculum from reporting
   "wood.16 in progress" while the world hasn't actually changed.

3. Defensive default — runtime/movement-profiles.js: canDig=false on
   every profile until dig is confirmed working live. Otherwise
   pathfinder schedules paths through must-dig blocks and the bot loops.

4. Ground-truth probe — runtime/skills/diagnose-physics.js dispatches
   forward/jump/dig probes and writes the result to the diary. New
   IPC command cmd:run-skill lets the operator (or a future curriculum
   trigger) fire any skill on demand; it waits for the current action
   to finish before dispatching. /tmp/pepa-runskill.mjs is a one-shot
   client.

Live probe on play.xmatic.team confirmed: forward Δ=0.003 over 2s
(BROKEN — server rejects movement packets), jump ΔY=0.42 (likely
physics jitter, not a real jump). Strongly suggests an anti-cheat
plugin gating bot-style movements server-side — beyond protocol pin.

npm test 124/124.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-26 11:51:43 +03:00

153 lines
4.9 KiB
JavaScript

// gather.stone — find a nearby stone/cobble/deepslate block, equip a
// pickaxe (best available), path to it and mine it. Stone-tier mining
// needs at least a wooden pickaxe — the preconditions enforce that.
import pathfinderPkg from "mineflayer-pathfinder";
const { pathfinder, goals, Movements } = pathfinderPkg;
import collectBlockPkg from "mineflayer-collectblock";
const collectBlockPlugin =
collectBlockPkg.plugin ??
collectBlockPkg.default?.plugin ??
collectBlockPkg.default ??
collectBlockPkg;
import { pickaxes } from "./groups.js";
import { info, warn } from "../log.js";
const STONE_NAMES = ["stone", "cobblestone", "deepslate", "cobbled_deepslate", "andesite", "diorite", "granite"];
let pluginLoaded = new WeakSet();
function ensurePathfinder(bot) {
if (pluginLoaded.has(bot)) return;
bot.loadPlugin(pathfinder);
pluginLoaded.add(bot);
}
let collectBlockLoaded = new WeakSet();
function ensureCollectBlock(bot) {
ensurePathfinder(bot);
if (collectBlockLoaded.has(bot)) return;
bot.loadPlugin(collectBlockPlugin);
collectBlockLoaded.add(bot);
}
function setMovementsForGather(bot) {
const m = new Movements(bot);
m.canDig = true;
m.allow1by1towers = false;
bot.pathfinder.setMovements(m);
}
const PICKAXE_PRIORITY = ["netherite_pickaxe", "diamond_pickaxe", "iron_pickaxe", "stone_pickaxe", "wooden_pickaxe"];
async function equipBestPickaxe(bot) {
for (const name of PICKAXE_PRIORITY) {
const item = bot.inventory.items().find((i) => i.name === name);
if (item) {
try {
await bot.equip(item, "hand");
return name;
} catch {}
}
}
return null;
}
function withTimeout(promise, ms, label) {
let timer;
const timeout = new Promise((_, reject) => {
timer = setTimeout(() => reject(new Error(`${label} timed out after ${ms / 1000}s`)), ms);
});
return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
const stoneBlacklist = new WeakMap();
const BLACKLIST_TTL_MS = 5 * 60_000;
function getBlacklist(bot) {
let m = stoneBlacklist.get(bot);
if (!m) {
m = new Map();
stoneBlacklist.set(bot, m);
}
const now = Date.now();
for (const [k, exp] of m) if (exp < now) m.delete(k);
return m;
}
export const skill = Object.freeze({
id: "gather.stone",
title: "Gather cobblestone",
timeoutMs: 90_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
const available = pickaxes(ctx.bot);
if (available.size === 0) {
return { ok: false, code: "unsupported_version", detail: "no pickaxes in registry" };
}
const owned = ctx.bot.inventory.items().some((i) => available.has(i.name));
if (!owned) {
return { ok: false, code: "missing_tool", detail: "no pickaxe in inventory" };
}
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
const blacklist = getBlacklist(bot);
const target = bot.findBlock({
matching: (b) => {
if (!b || !b.position || !STONE_NAMES.includes(b.name)) return false;
const key = `${b.position.x},${b.position.y},${b.position.z}`;
return !blacklist.has(key);
},
maxDistance: 32,
});
if (!target) {
return { ok: false, code: "no_target", detail: "no reachable stone within 32 blocks", worldDelta: null };
}
ensureCollectBlock(bot);
setMovementsForGather(bot);
const pickaxe = await equipBestPickaxe(bot);
info("action", `gather.stone: ${target.name} at ${target.position.x},${target.position.y},${target.position.z} (tool=${pickaxe ?? "fists"})`);
const targetPos = target.position.clone();
try {
try {
await withTimeout(bot.lookAt(targetPos.offset(0.5, 0.5, 0.5), true), 2_000, "lookAt(stone)");
} catch {}
await withTimeout(bot.collectBlock.collect(target), 60_000, "collectStone");
const after = bot.blockAt(targetPos);
if (after && STONE_NAMES.includes(after.name)) {
warn("action", `gather.stone reported ok but block still at ${targetPos.x},${targetPos.y},${targetPos.z} — silent dig failure`);
const key = `${targetPos.x},${targetPos.y},${targetPos.z}`;
blacklist.set(key, Date.now() + BLACKLIST_TTL_MS);
return {
ok: false,
code: "silent_dig_failure",
detail: "block still exists after collect — protocol/anti-cheat issue",
worldDelta: null,
};
}
return {
ok: true,
code: "done",
detail: { blockType: target.name, at: targetPos },
worldDelta: { minedAt: targetPos, blockType: target.name },
};
} catch (e) {
warn("action", `gather.stone failed: ${e.message}`);
const key = `${target.position.x},${target.position.y},${target.position.z}`;
blacklist.set(key, Date.now() + BLACKLIST_TTL_MS);
const msg = String(e?.message ?? "");
const code = msg.includes("timed out") ? "timeout" : "failed";
return { ok: false, code, detail: e.message, worldDelta: null };
}
},
validate(ctx, result) {
return result.ok && !!result.worldDelta?.blockType;
},
recover(ctx, result) {
if (result.code === "no_target") return { hint: "wander", reason: "no stone within 32 blocks" };
return null;
},
});