**This is an old revision of the document!**

组件:机器人

此组件由机器人提供。
下列函数可在Lua解释器中使用=component.robot.function()的写法调用。想要以更直观的方式来调用机器人函数的话,可以使用Robot(机器人) API。它是组件函数的封装版本,还提供了额外的功能(在API页面中总结)。

组件名:robot

基础方法

  • durability():number
    获取当前装备工具的耐久度。

  • move(direction:number):boolean
    向指定方向移动。可用的移动方向有:frontbacktopbottom(参见sides(方向) API)。

  • turn(clockwise:boolean):boolean
    向指定方向旋转。
  • name():string
    获取机器人的名称。

  • swing(side:number):boolean[, string]
    让机器人使用当前工具槽位装备的物品与正前方的方块或空间交互。如果成功则返回 true(可能需要时间,具取决于与其交互的方块,例如黑曜石需要一些时间来挖掘)。如果执行失败则返回false以及失败原因。

  • use(side: number[, sneaky: boolean[, duration: number]]): boolean[, string]
    尝试以与玩家右键单击相同的方式使用当前装备在工具槽位的物品。
    side - 若给定,则机器人仅尝试在指定面上“右键单击”,否则机器人会尝试所有可能的面。请查阅sides(方向) API以获取可能的方向列表。
    sneaky - 若设置为true,机器人将会模拟玩家的潜行右键单击(如同玩家在右键单击时按住shift键一样)。若设置为true,某些物品(例如桶)的行为会有所不同。
    duration - 使用该物品的时间。这在使用弓等需要蓄力的物品时很有用。
    返回值:若机器人可以与其前面的方块或实体交互,则返回 true,否则返回 false。若成功,会有第二个返回值,代表机器人交互的内容,可能取值为下列之一:block_activated(激活方块)、item_placed(放置物品)、item_used(使用物品)、item_interacted(与物品互动)、air(空气)。
    此函数用途非常广泛,因为机器人可以模拟右键单击大多数物品。在这方面机器人与玩家的唯一区别是机器人不能使用明确要求用户是实体的物品,因为机器人是方块。所以喝药水、吃食物或扔末影珍珠都会失败。 此函数的第二个返回值可用来判断右键单击的结果。有时候不一定能明显看出一个物品会返回哪个值,因此需要事先测试。还需注意,虽然机器人不受有害药水的影响,但它们可能会被爆炸摧毁,因此在使用此函数放置、投掷或激活任何形式的炸药时要小心。
    第二个返回值的可能值有:
    • block_activated - 某个方块被激活(例如拉杆、开关以及门)
    • item_interacted - 装备的工具与世界进行了互动,例如在羊身上使用剪刀。
    • item_placed - 某个物品被放置到了世界中。此返回值不一定由可放置的方块触发,其他会导致方块或实体出现在世界上的物品也会触发(例如燧石或刷怪蛋)。
    • item_used - 装备的物品被激活,例如喷溅式药水。
    • air - 当前装备的物品需要目标才能使用,但并不存在。请注意如果你的机器人有天使升级,此返回值将永远不会出现,但一些操作可能仍然无效果。

  • place(side: number[, sneaky: boolean]): boolean[, string]
    尝试将物品栏中前选定槽位中的方块放置在机器人的指定方向(若支持此方向)。若sneaky设置为true,那么机器人将会模拟潜行放置(按住shift点击)。若成功放置则返回 true,若操作失败则返回 false。若使用不支持的值作为参数,则操作将会失败,并返回失败原因。

  • getLightColor():number
    获取当前活动指示灯(机器人)或机翼灯(无人机)设定的颜色,形式为整数编码RGB值(0xRRGGBB)。

  • setLightColor(value:number):number
    将活动指示灯(机器人)或机翼灯(无人机)的颜色设定为给定的整数编码RGB值(0xRRGGBB)。

Internal Inventory Methods

  • inventorySize():number
    Returns the size of the device's internal inventory.
  • select([slot:number]):number
    Get the currently selected slot; set the selected slot if specified.
  • count([slot:number]):number
    Get the number of items in the specified slot, otherwise in the selected slot.
  • space([slot:number]):number
    Get the remaining space in the specified slot, otherwise in the selected slot.
  • compareTo(otherSlot:number):boolean
    Compare the contents of the selected slot (in the robot inventory) to the contents of the specified slot (also in the robot inventory).
  • transferTo(toSlot:number[, amount:number]):boolean
    Move the specified amount of items from the selected slot into the specified slot. If no amount is specified, the entire stack is moved to the target slot.
  • tankCount():number
    Returns the number of tanks installed in the robot.
  • selectTank(tank:number)
    Selects the specified tank (if robot contains more than one Tank upgrade). Any tank operations will use this tank.
  • tankLevel([tank:number]):number
    Returns the fluid level in the specified tank. If no tank specified, returns the fluid level in the selected tank (using selectTank()).
  • tankSpace([tank:number]):number
    Returns the remaining fluid capacity (empty space) in the specified tank. If not tank specified, returns the remaining capacity in the selected tank.
  • compareFluidTo(tank:number):boolean
    Tests whether the fluid in the selected tank (in the robot inventory) is the same as in the specified tank (requires Tank upgrade).
  • transferFluidTo(tank:number[, count:number]):boolean
    Transfer the specified amount of fluid in the selected tank into the specified tank. If no volume is specified, the robot will attempt to transfer 1000mB.

External Inventory Methods

  • detect(side:number):boolean
    Detects the block on the given side, relative to the robot, and returns whether or not the robot can move into the block, as well as a general description of the block. Returns true if the block will prevent the robot from moving forward, false otherwise. Drones return true even if the block is passable.

    side - See the Sides API for a list of possible sides.

  • compareFluid(side:number):boolean Compares fluid in the selected tank (requires a Tank upgrade) to fluid in the world or in an external tank on the specified side of the robot.
  • drain(side:number[, count:number]):boolean
    Extracts the specified amount of fluid from the world or a tank on the specified side of the robot. If no amount is specified, the robot will try to drain 1000mB. If the tank is unable to store the specified amount of fluid, the operation will fail (no fluid is lost in the process).
  • fill(side:number[, count:number]):boolean
    Fills the specified amount of fluid from the selected tank (requires a Tank upgrade) into the world or a tank in front of the robot. If no amount is specified, the robot will try to fill the target tank with 1000mB of fluid. If there is not enough fluid to fill a block, or not enough space in the target tank, the operation will fail with no fluids lost.
  • compare(side:number[, fuzzy:boolean=false]):boolean Compares the block on the specified side of the robot with the item in the currently selected slot and returns whether they are the same or not. Blocks are considered identical if the type and metadata match; additional ItemStack information is not checked. Empty blocks are considered as air blocks, which cannot be compared to an empty inventory slot; the detect() function can be used to determine if there is a block in front of the robot. For blocks that drop a different item, the compare() method won't work (eg: Diamond block dropping diamond items); for these cases, use silk-touch to obtain a block for comparison.
  • drop(side:number[, count:number]):boolean
    Drops the specified number of items from the currently selected slot. Returns true if at least one item is dropped, false otherwise. If the block in front of the robot is an inventory, the robot will try to place the item into the inventory. If the inventory doesn't accept the item, the item is not dropped into the world (the operation will fail and return false). Robots themselves are considered blocks with an inventory, and items can be moved into them using the drop() function. The drop() function will not work on non-item inventories, such as fluid tanks (the use() function works for these cases).
  • suck(side:number[, count:number]):boolean
    Tries to pick up the specified number of items and place it in the selected slot. If the selected slot is occupied, items will be placed in the first available slot. Returns the number of items sucked else false.

目录