Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为机器人编程以放置块_Javascript_Node.js - Fatal编程技术网

Javascript 为机器人编程以放置块

Javascript 为机器人编程以放置块,javascript,node.js,Javascript,Node.js,我正试图让我的MineFlayer机器人在活塞表面放置一个沙块,当它在5块半径范围内检测到它时 我已经试过bot.placeblock33,vec0,0,1,cb,它给了我一个错误“offset” Bot.PlaceBlock(blockID, vec(1,0,0), cb) 以下是mineflayer github源代码 API在文档中 假设在活塞的侧面放置一个块,半径为5块。 但我只是遇到了错误,无法让它工作。placeBlock将块实例作为引用,而不是ID。要使用placeBlock,您

我正试图让我的MineFlayer机器人在活塞表面放置一个沙块,当它在5块半径范围内检测到它时

我已经试过bot.placeblock33,vec0,0,1,cb,它给了我一个错误“offset”

Bot.PlaceBlock(blockID, vec(1,0,0), cb)
以下是mineflayer github源代码

API在文档中

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

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

将要放置的块放入hotbar bot.moveSlotItem中 为您的手动机器人选择该插槽 可能从blockAt或findBlock获取块实例 现在你可以调用placeBlockblock,face,cb 请参阅,以了解我假设无法读取未定义的属性“offset”的错误原因,因为未定义ID、数字和其他有关我所说内容的API信息的.position

编辑:下面是一些代码的摘录,可以大致实现这一点

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);
此代码缺少一些内容:

我想你需要研究活塞的元数据值,它应该在活塞变量中

实际上是接近活塞,或者找到沙子来装备

等待,直到机器人能够执行操作,即等待加入事件


欢迎来到堆栈溢出!为了帮助我们更好地理解您遇到的问题,您需要非常详细地说明您收到的错误消息,请确保将准确的错误文本复制粘贴到您的问题中。为上下文提供足够的代码也很重要,我们可以在必要时尝试重现问题。不清楚这里的错误是什么,也不清楚它在代码中实际发生的位置。请记住JavaScript是一种区分大小写的语言,所以不要只是随意翻阅大小写,而要阅读源代码中的示例和用例。我不太明白:/我对这一点很陌生,你能给我举个例子吗?谢谢!我已经在上面添加了一个代码片段,它应该可以帮助您解决这个问题。注释OK太长了,所以感谢您的帮助,但是const onSandPlace=error=>/。。。使const onSandPlace=error=>{if error{return;}部分不起作用。因此,如果我删除const onSandPlace=error=>/…即使在替换pistonBlockID和sandBlockId之后,它仍然不起作用,如果不替换sandBlockId,它仍然不起作用。我不太确定你的意思。你在onSandPlace方法中放了什么?不,第一个const onSandPlace确保第二个const不起作用k、 并得到一个意外的错误常量