yumetodo

auto_craft_concrete.lua

Jun 9th, 2021 (edited)
499
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local function sync()
  2.     -- print("sync")
  3.     local init_slot = turtle.getSelectedSlot() -- 退避
  4.     local found_slot = 0
  5.     for i = 1, 16 do
  6.         -- print("sync:: slot:"..i.." found slot:"..found_slot)
  7.         local info = turtle.getItemDetail(i)
  8.         if info then
  9.             if info.name == "minecraft:concrete" then
  10.                 turtle.select(i)
  11.                 turtle.dropUp()
  12.             elseif (found_slot == 0) and (info.name == "minecraft:concrete_powder") then
  13.                 found_slot = i
  14.                 -- print("sync:: found")
  15.             end
  16.         end
  17.     end
  18.     turtle.select(init_slot)
  19.     return found_slot
  20. end
  21.  
  22. local function place_and_dig_impl()
  23.     -- print("place_and_dig_impl")
  24.     turtle.dig()
  25.     while 0 < turtle.getItemCount() do
  26.         local info = turtle.getItemDetail(i)
  27.         if (not info) or (info.name ~= "minecraft:concrete_powder") then break end
  28.         local re, message = turtle.place()
  29.         if not re then return re, message end
  30.         os.sleep(0.2)
  31.         while true do
  32.             local re, info = turtle.inspect()
  33.             if re and info ~= nil and info.name == "minecraft:concrete" then
  34.                 break
  35.             end
  36.             os.sleep(0.1)
  37.         end
  38.         re, message = turtle.dig()
  39.         if not re then return re, message end
  40.     end
  41.     return true
  42. end
  43.  
  44. ---@param slot integer
  45. local function place_and_dig(slot)
  46.     local init_slot = turtle.getSelectedSlot() -- 退避
  47.     turtle.select(slot)
  48.     local re, message = place_and_dig_impl()
  49.     turtle.select(init_slot)
  50.     return re, message
  51. end
  52. local function main()
  53.     while true do
  54.         local slot = sync()
  55.         if slot ~= 0 then
  56.             local re, message = place_and_dig(slot)
  57.             if not re then return re, message end
  58.         else
  59.             os.sleep(2)
  60.         end
  61.     end
  62.     return true
  63. end
  64. local re, message = main()
  65. if not re then print(message) end
RAW Paste Data