CC: Tweaked

CC: Tweaked

57M Downloads

turtle.getEquipped method

kepler155c opened this issue ยท 1 comments

commented

Determining what the turtle has equipped is quite a pain. In writing many programs, I would require a item to be equipped. To determine if the turtle has a non-peripheral item equipped (suck as a pickaxe or sword), I need to un-equip it, check the inventory, then re-equip the item.

It would be very helpful to have a method that returns the item equipped on a side. The method could be:
turtle.getEquipped('left')
or
turtle.getEquippedLeft().

Note that I have many programs that swap out items while running (i.e. swap sensor for scanner). Here is what I currently use (and this does not cover all cases):

local function equip(side, item, rawName)
  -- is it already equipped on the correct side?
  local equipped = peripheral.getType(side)
  if equipped == item then
    return true
  end

  -- is it equipped on the opposite side?
  local osides = { left = 'right', right = 'left' }
  if peripheral.getType(osides[side]) == item then
    if not turtle.selectSlotWithQuantity(0) then
      error('No slots available')
    end
    turtle.equip(osides[side])
  elseif not turtle.has(rawName or item) then
    -- don't have the item - unequip that side to see if it's the correct item
    if not turtle.selectSlotWithQuantity(0) then
      error('No slots available')
    end
    turtle.equip(side)
  end

  -- TODO: if the non-peripheral item was equipped on the other side, then this will not work

  if not turtle.has(rawName or item) then
    error('Missing ' .. (rawName or item))
  end

  if not turtle.equip(side, rawName or item) then
    error('Unable to equip ' .. (rawName or item))
  end

  turtle.select(1)
end

equip('left', 'minecraft:diamond_pickaxe')
equip('right', 'modem', ''computercraft:peripheral:1'')
commented

See dan200/ComputerCraft#462.

Also, as the issue template says, please try to report non-CC:T-specific issues on the main ComputerCraft tracker! It's an awful lot more people watching it, so means more people are likely to contribute.