Files
pepa-pi-bot/runtime/skills/gather-wool.js
T
mayatnikovandClaude Opus 4.7 28f5d9e483 fix(perception): use numeric block ids — callback matchers silently fail under ViaBackwards
Root cause of "bot just stands still": every gather.* skill was using
bot.findBlock({ matching: (b) => names.includes(b.name) }), and under
mineflayer 1.21.4 + ViaBackwards the Block objects fed into the
callback have a wrong .name field (Block.type / numeric id is still
correct — this is mineflayer issue #2347). Every search returned null,
every skill reported "no_target", reflex looped wander → tunnel-out
forever. The bot's logs said "dispatch ok" while the operator watched
it pace in circles.

Proven live with a new diag.match skill on play.xmatic.team:
  findBlocks({matching: numericIds})              → 50 hits
  findBlocks({matching: (b) => b.name === ...})   →  0 hits  ← the bug
  findBlock({matching: (b) => b.name === ...})    → null     ← the bug
  findBlock({matching: numericIds})               → dark_oak_log @ (606,62,110)

After this fix the same bot from the same spawn dispatches gather.logs
and reaches the chop loop ("chop: dark_oak_log at 606,62,110 (tool=fists)")
instead of returning "no reachable log within 64 blocks".

Changes:
- runtime/perception.js (new): findBlocksByName / findNearestBlockByName
  centralise the numeric-id workaround for any future skill.
- runtime/actions.js: chopNearestTree, sleepInBed, placeCraftingTable now
  use perception. Also load mineflayer-tool plugin alongside collectblock
  (collectblock 1.6 hard-requires bot.tool to dispatch a dig).
- gather-stone, gather-wool, deposit-surplus rewritten to numeric-id
  search. gather-wool also loads mineflayer-tool.
- diagnose-scan.js (new): two diagnostic skills — diag.scan reports
  findBlocks counts per radius for common blocks; diag.match cross-tests
  the four matcher styles so this regression can be re-proven on demand.
- runtime/skills/index.js: registers diag.scan + diag.match.

Memory: project_findblock_callback_broken_under_viabackwards.md.

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

173 lines
5.6 KiB
JavaScript

// gather.wool — get one block of wool, any colour. Three paths in order
// of preference:
// 1. Mine a placed wool block within 32 blocks (someone left one).
// 2. Shear a nearby sheep if we carry shears.
// 3. Attack a nearby sheep to drop wool (last resort; gives 1 wool).
//
// Wool is the only ingredient missing for a bed once we have planks, so
// this skill is the first real "go find an animal" task the bot has.
import collectBlockPkg from "mineflayer-collectblock";
import pathfinderPkg from "mineflayer-pathfinder";
import toolPkg from "mineflayer-tool";
const toolPlugin = toolPkg.plugin ?? toolPkg.default?.plugin ?? toolPkg.default ?? toolPkg;
import { info, warn } from "../log.js";
import { findNearestBlockByName } from "../perception.js";
const { pathfinder, goals, Movements } = pathfinderPkg;
const WOOL_NAMES = [
"white_wool", "orange_wool", "magenta_wool", "light_blue_wool", "yellow_wool",
"lime_wool", "pink_wool", "gray_wool", "light_gray_wool", "cyan_wool",
"purple_wool", "blue_wool", "brown_wool", "green_wool", "red_wool", "black_wool",
];
const collectBlockPlugin =
collectBlockPkg.plugin ??
collectBlockPkg.default?.plugin ??
collectBlockPkg.default ??
collectBlockPkg;
const WOOL_BLOCK_RE = /(?:^|_)wool$/;
let pluginLoaded = new WeakSet();
function ensurePlugins(bot) {
if (pluginLoaded.has(bot)) return;
bot.loadPlugin(pathfinder);
bot.loadPlugin(toolPlugin); // collectblock needs bot.tool
bot.loadPlugin(collectBlockPlugin);
pluginLoaded.add(bot);
}
function setMovementsForGather(bot) {
const m = new Movements(bot);
m.canDig = true;
m.allow1by1towers = false;
bot.pathfinder.setMovements(m);
}
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));
}
function woolCount(bot) {
return bot.inventory.items().reduce(
(sum, i) => (WOOL_BLOCK_RE.test(i.name) || i.name.endsWith("_wool") ? sum + i.count : sum),
0,
);
}
function nearestSheep(bot) {
let best = null;
for (const e of Object.values(bot.entities)) {
if (e?.name !== "sheep" || !e.position) continue;
const d = e.position.distanceTo(bot.entity.position);
if (!best || d < best.d) best = { e, d };
}
return best;
}
export const skill = Object.freeze({
id: "gather.wool",
title: "Gather one wool",
timeoutMs: 90_000,
preconditions(ctx) {
if (!ctx?.bot) return { ok: false, code: "no_bot", detail: "bot missing" };
if (woolCount(ctx.bot) >= 3) {
return { ok: false, code: "already_have", detail: "already have ≥3 wool" };
}
return { ok: true };
},
async execute(ctx) {
const bot = ctx.bot;
ensurePlugins(bot);
setMovementsForGather(bot);
// 1. Placed wool block? Numeric-id search — see runtime/perception.js.
const woolBlock = findNearestBlockByName(bot, WOOL_NAMES, { maxDistance: 32 });
if (woolBlock) {
info("action", `gather.wool: mining ${woolBlock.name} at ${woolBlock.position}`);
try {
await withTimeout(bot.collectBlock.collect(woolBlock), 45_000, "collectWool");
return {
ok: true,
code: "done",
detail: { from: "block", name: woolBlock.name },
worldDelta: { gotWool: 1, source: "block" },
};
} catch (e) {
warn("action", `gather.wool block-mine failed: ${e.message}`);
// fall through to sheep
}
}
// 2/3. Sheep — shear if we have shears, otherwise attack.
const sheep = nearestSheep(bot);
if (!sheep) {
return { ok: false, code: "no_target", detail: "no wool block and no sheep within view", worldDelta: null };
}
try {
await withTimeout(
bot.pathfinder.goto(new goals.GoalFollow(sheep.e, 2)),
30_000,
"pathToSheep",
);
} catch (e) {
return { ok: false, code: "no_path", detail: e.message, worldDelta: null };
}
const shears = bot.inventory.items().find((i) => i.name === "shears");
if (shears) {
try {
await withTimeout(bot.equip(shears, "hand"), 3000, "equip shears");
bot.activateEntity(sheep.e); // shears interaction
await new Promise((r) => setTimeout(r, 600));
// Wait for the drop entity to spawn near the sheep, then pick it up
// by walking to it. Simplest: a brief wait — the bot is already next
// to the sheep, drops are auto-collected.
await new Promise((r) => setTimeout(r, 1200));
return {
ok: true,
code: "done",
detail: { from: "shear", entityId: sheep.e.id },
worldDelta: { gotWool: 1, source: "shear" },
};
} catch (e) {
warn("action", `gather.wool shear failed: ${e.message}`);
// fall through to attack
}
}
try {
bot.attack(sheep.e);
await new Promise((r) => setTimeout(r, 800));
// Mineflayer doesn't auto-loop attacks; re-fire until dead or out
// of reach. Up to 6 swings.
for (let i = 0; i < 6; i++) {
const still = Object.values(bot.entities).find((e) => e.id === sheep.e.id);
if (!still) break;
if (still.position.distanceTo(bot.entity.position) > 4) break;
bot.attack(still);
await new Promise((r) => setTimeout(r, 700));
}
await new Promise((r) => setTimeout(r, 1200));
return {
ok: true,
code: "done",
detail: { from: "kill", entityId: sheep.e.id },
worldDelta: { gotWool: 1, source: "kill" },
};
} catch (e) {
warn("action", `gather.wool attack failed: ${e.message}`);
return { ok: false, code: "failed", detail: e.message, worldDelta: null };
}
},
recover(ctx, result) {
if (result.code === "no_target") return { hint: "wander", reason: "no sheep or wool block visible" };
return null;
},
});