-3

我试图让我的 MineFlayer 机器人在 5 格半径内检测到活塞表面时将沙块放在活塞表面。

我已经尝试过 bot.placeblock(33, vec(0,0,1), cb) 并且它给了我一个错误“偏移量”。

Bot.PlaceBlock(blockID, vec(1,0,0), cb)

这是 mineflayer github 源代码https://github.com/PrismarineJS/mineflayer

API 在文档中

假设在活塞的侧面放置一个方块,半径为 5 个方块。但我只是得到错误,无法让它工作。

4

1 回答 1

0

placeBlock 将块实例作为参考,而不是 ID。为了使用 placeBlock,您需要执行以下操作:

  1. 将要放置的方块放入快捷栏 ( bot.moveSlotItem)
  2. 为您的手选择该插槽 ( bot.equip)
  3. 获取块实例(可能来自blockAt或来自findBlock
  4. 现在你可以打电话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);

这段代码缺少一些东西:

  1. 确定要在活塞上放置哪个面(我认为您需要查看活塞的元数据值,它应该在pistonvar 中)

  2. 实际靠近活塞,或寻找沙子装备

  3. 等到机器人能够做事(即等待加入事件)

于 2019-04-09T02:05:34.417 回答