placeBlock 将块实例作为参考,而不是 ID。为了使用 placeBlock,您需要执行以下操作:
- 将要放置的方块放入快捷栏 (
bot.moveSlotItem
)
- 为您的手选择该插槽 (
bot.equip
)
- 获取块实例(可能来自
blockAt
或来自findBlock
)
- 现在你可以打电话
placeBlock(block, face, cb)
请参阅https://github.com/PrismarineJS/mineflayer/blob/6ae68d3bc754ac165e658cd8c64ce32e22a1706f/lib/plugins/inventory.js#L319,因为您收到错误的原因(我假设“无法读取未定义的属性'偏移'” ,因为 .position 未定义 ID,一个数字),以及https://github.com/PrismarineJS/mineflayer/blob/master/docs/api.md有关我所说的其他 API 信息。
编辑:这是一些可以大致完成此任务的代码的摘录
const bot = mineflayer.createBot({ /* ... */ });
// ... do some action that would get you within range of a piston ...
// This .findBlock method will find the nearest block to the point, which is the position of the bot's entity. The block it finds must match the piston block ID, and will be returned.
const piston = bot.findBlock({ point: bot.entity.position, matching: pistonBlockId });
if (piston == null) {
return;
}
// the null is required for some reason, but doesn't do anything
const sand = bot.inventory.findInventoryItem(sandBlockId, null);
if (sand == null) {
return;
}
const onSandPlace = (error) => // ...
const onSandEquip = (error) => {
if (error) {
return;
}
// this vec should probably be different based on the face you want to place it on
bot.placeBlock(piston, new Vec3(0, 1, 0), onSandPlace);
}
bot.equip(sand, 'hand', onSandEquip);
这段代码缺少一些东西:
确定要在活塞上放置哪个面(我认为您需要查看活塞的元数据值,它应该在piston
var 中)
实际靠近活塞,或寻找沙子装备
等到机器人能够做事(即等待加入事件)