JustDoesGames

Tree Farm Program - 1.19.2 / 1.20

Oct 12th, 2023 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.55 KB | None | 0 0
  1. if not fs.exists("/cfg.lua") then local f = fs.open("cfg.lua", "w") f.close() end
  2. local f = fs.open("cfg.lua","r")
  3. if f then t = textutils.unserialize(f.readAll()) f.close() end
  4.  
  5. if not t then t = {} end
  6. if not t.active then t.active = false end
  7. if not t.state then t.state = 1 end
  8.  
  9. if not t.cycles then t.cycles = 0 end
  10.  
  11. local states = {
  12.     turtle.dig,
  13.     turtle.forward,
  14.     turtle.digUp,
  15.     turtle.up,
  16.     turtle.digUp,
  17.     turtle.up,
  18.     turtle.digUp,
  19.     turtle.up,
  20.     turtle.digUp,
  21.  
  22.     turtle.down,
  23.     turtle.down,
  24.     turtle.down,
  25.     turtle.back,
  26. }
  27.  
  28. local function refuel()
  29.     turtle.select(1)
  30.     term.setCursorPos(1,1) print("Refueling...")
  31.     while turtle.getFuelLevel() == 0 do print("Requires More Fuel.") sleep(1) turtle.refuel(1) end
  32. end
  33.  
  34. local function cleanInv()
  35.     term.clear() term.setCursorPos(1,1) print("Cleaning Inventory...") sleep(2)
  36.     for i=1, 16 do -- first 8 slots
  37.         local info = turtle.getItemDetail(i)
  38.         if info then
  39.             turtle.select(i)
  40.             if i == 1 then
  41.                 if info.name == "minecraft:oak_sapling" then
  42.                     local info = turtle.getItemDetail(2)
  43.                     if info then
  44.                         if info.name ~= "minecraft:oak_sapling" then
  45.                             turtle.select(2)
  46.                             turtle.dropDown()
  47.                         end
  48.                         turtle.select(i)
  49.                     end
  50.                     turtle.transferTo(2,64-turtle.getItemCount(2))
  51.                 end
  52.                 if info.name ~= "minecraft:charcoal" then
  53.                     turtle.dropDown()
  54.                 end
  55.             elseif i == 2 then
  56.                 if info.name == "minecraft:coal" or info.name == "minecraft:charcoal" then
  57.                     local info = turtle.getItemDetail(1)
  58.                     if info then
  59.                         if info.name ~= "minecraft:coal" or info.name ~= "minecraft:charcoal" then
  60.                             turtle.select(2)
  61.                             turtle.dropDown()
  62.                         end
  63.                         turtle.select(i)
  64.                     end
  65.                     turtle.transferTo(1,64-turtle.getItemCount(1))
  66.                 end
  67.                 if info.name ~= "minecraft:oak_sapling" then
  68.                     turtle.dropDown()
  69.                 end
  70.             elseif info.name == "minecraft:oak_sapling" and i ~= 2 then -- toss to slot 2
  71.                 if turtle.getItemCount(2) ~= 64 then
  72.                     turtle.transferTo(2,64-turtle.getItemCount(2))
  73.                 end
  74.                 turtle.select(i)
  75.                 turtle.dropDown()
  76.             elseif info.name == "minecraft:charcoal" and i ~= 1 then -- toss to slot 1
  77.                 if turtle.getItemCount(1) ~= 64 then
  78.                     turtle.transferTo(1,64-turtle.getItemCount(1))
  79.                 end
  80.                 turtle.select(i)
  81.                 turtle.dropDown()
  82.             else
  83.                 turtle.dropDown()
  84.             end
  85.         end
  86.     end
  87. end
  88.  
  89. local function doStates()
  90.     term.clear() term.setCursorPos(1,1)
  91.     print("Executing Program...")
  92.     t.active = true
  93.     while t.state < #states+1 do
  94.        
  95.         term.setCursorPos(1,2) print("Stage: ["..t.state.." / "..#states.."]")
  96.         if turtle.getFuelLevel() == 0 then refuel() end
  97.         states[t.state]()
  98.         t.state = t.state + 1
  99.  
  100.         local f = fs.open("cfg.lua", "w")
  101.         f.write(textutils.serialize(t)) f.close()
  102.  
  103.         sleep(.1)
  104.     end
  105.     t.active = false
  106.     t.state = 1 -- just to make sure
  107.     t.cycles = t.cycles+1
  108.  
  109.     local f = fs.open("cfg.lua", "w")
  110.     f.write(textutils.serialize(t)) f.close()
  111.  
  112.     cleanInv()
  113. end
  114.  
  115. term.clear()
  116.  
  117. if t.active then print("Picking up...") sleep(2) doStates() end
  118.  
  119. while true do
  120.    
  121.     term.clear()
  122.     term.setCursorPos(1,1)
  123.     print("Status: Idle")
  124.     print("Cycles: "..t.cycles)
  125.     print("Fuel Remaining: "..turtle.getFuelLevel().." ("..math.floor(turtle.getFuelLevel()/8).." Cycles)")
  126.     local s,d = turtle.inspect()
  127.  
  128.     if s then
  129.         if d.name == "minecraft:oak_log" then
  130.             doStates()
  131.         end
  132.     end
  133.  
  134.     turtle.suck(64)
  135.     turtle.suckUp(64)
  136.     local info = turtle.getItemDetail(2)
  137.     if info and not turtle.detect() then
  138.         if info.name == "minecraft:oak_sapling" then turtle.select(2) turtle.place() end
  139.     end
  140.  
  141.     sleep(1)
  142. end
  143.  
Add Comment
Please, Sign In to add comment