Advertisement
cing5000

place item

Apr 26th, 2015
460
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.64 KB | None | 0 0
  1. local succes = false
  2. local oldSlot = turtle.getSelectedSlot()
  3.  
  4. --Checks for a specific item in current slot and returns succes
  5. local function checkItem(name)
  6.         if turtle.getItemDetail() ~= nil then
  7.                 local data = turtle.getItemDetail()
  8.                 if data.name == name then
  9.                         return true
  10.                 else
  11.                         return false
  12.                 end
  13.         end
  14.         return false
  15. end
  16.  
  17. --check if item is in inventory and returns succes
  18. function checkInvetory(name)
  19.         oldSlot = turtle.getSelectedSlot()
  20.         for i = 1,16 do
  21.             turtle.select(i)
  22.             if checkItem(name) then
  23.                 turtle.select(oldSlot)
  24.                 return true
  25.             end
  26.         end
  27.         turtle.select(oldSlot)
  28.         return false
  29. end
  30.  
  31. --check if item is in inventory and returns numberSlot
  32. function checkInventorySlot(name)
  33.         oldSlot = turtle.getSelectedSlot()
  34.         for i = 1,16 do
  35.             turtle.select(i)
  36.             if checkItem(name) then
  37.                 turtle.select(oldSlot)
  38.                 return i
  39.             end
  40.         end
  41.         turtle.select(oldSlot)
  42.         return nil
  43. end
  44.  
  45. --move "amount(int)" of "name(string)" items to slot "slot(int)" and returns succes
  46. function placeItemInSlot(name, slot, amount)
  47.     if type(name) ~= string or type(slot) ~= number or type(amount) ~= number then
  48.         return false
  49.     else
  50.         numberSlot = placer.checkInventorySlot(name)
  51.         if numberSlot == nil then
  52.             return false
  53.         else
  54.             turtle.select(numberSlot)
  55.             if turtle.transferTo(slot, amount) then
  56.                 local succeeded = true
  57.             else
  58.                 local succeeded = false
  59.             end
  60.             turtle.select(oldSlot)
  61.             return succeeded
  62.         end
  63.     end
  64. end
  65.  
  66. --places any item in any direction and returns succes
  67. function place(name ,dir)
  68.         succes = false
  69.         for i = 1,16 do
  70.             turtle.select(i)
  71.             if checkItem(name) then
  72.                 succes = true
  73.             end
  74.         end
  75.         if succes then
  76.                 if dir == "up" then
  77.                         return turtle.placeUp()
  78.                 elseif dir == "down" then
  79.                         return turtle.placeDown()
  80.                 else
  81.                         return turtle.place()
  82.                 end
  83.         else      
  84.             if checkInvetory then
  85.                 --print("Could not place any "..name.." "..dir)
  86.             else
  87.                 --print("There is no "..name.." in inventory")
  88.             end
  89.         end
  90.         return false
  91. end
  92.  
  93. --just a little example
  94. for i = 1,10 do
  95.     if place("minecraft:sand","down") then
  96.         sleep(0.5)
  97.     end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement