largeNumberGoeshere

byeTree

Apr 24th, 2021
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.87 KB | None | 0 0
  1.         -- IDS
  2.         sapplingId = "environmental:willow_sapling"
  3.         logId = "environmental:willow_log"
  4.         bonemealId = "minecraft:bone_meal"
  5.  
  6.         -- other settings
  7.         count = 10        --max height turtle will go
  8.         fuelBuffer =  50
  9.         idleTime = 0.2
  10.         maxSlotsFull = 15
  11.  
  12.         sappingSlot = 1
  13.         logSlot = 2
  14.         bonemealSlot = 12
  15.         ---
  16.  
  17.  
  18.            
  19.         print("Make sure to put sappings in slot "..tostring(sappingSlot).." and logs in slot " .. tostring(logSlot) .. " and bonemal in slot "..tostring(bonemealSlot))
  20.          
  21.         function isCorrectItem(slot,name)
  22.             turtle.select(slot)
  23.            
  24.             local id = turtle.getItemDetail() or "nothing"
  25.             if id == "nothing" then
  26.                     return "nothing"
  27.             end
  28.            
  29.             i1 = id.name
  30.            
  31.             if i1 == name then
  32.                 return true
  33.             else
  34.                 return false
  35.             end
  36.         end
  37.  
  38.         function ensureCorrectItem(slot,name,isOptional)
  39.             local isCorrect = isCorrectItem(slot,name)
  40.            
  41.             function repond(itemName, slot)
  42.                 print("Incorrect items")
  43.                 print("Please insert"..itemName.." into "..tostring(slot) )
  44.             end
  45.                
  46.             if isCorrect == true then
  47.                 return true -- tell caller all went well
  48.             end
  49.            
  50.             if isOptional == true then
  51.                 -- check if slot has nothing
  52.                 if isCorrect == "nothing" then
  53.                     return true --- tell caller all went well!
  54.                 end
  55.                
  56.                 -- if it has something , check that it has bonemeal
  57.                 if isCorrect == false then
  58.                     repond(name,slot)
  59.                     write(" or empty it")
  60.  
  61.                     --- wait until something is in slot
  62.                     while isCorrectItem(slot,name) == false do
  63.                         sleep(2)
  64.                         write(".")
  65.                     end
  66.                         return false -- tell caller to retry check
  67.                 end
  68.                
  69.  
  70.                
  71.                 error("Something weird happened @ensureCorrectItem")
  72.                
  73.             else
  74.                 --check if slot has nothing
  75.                 if isCorrect == "nothing" then
  76.                     respond(name,slot)
  77.                     while isCorrectItem(slot,name) == false do
  78.                         sleep(2)
  79.                         write(".")
  80.                     end
  81.                     return false
  82.                 end
  83.                
  84.                 --check if slot has the wrong item
  85.                 if isCorrect == false then
  86.                     respond(name,slot)
  87.                    
  88.                     while isCorrectItem(slot,name) == false do
  89.                         sleep(2)
  90.                         write(".")
  91.                     end
  92.                     return false
  93.                 end
  94.                
  95.                 --return
  96.                 error("this should not have happened")
  97.                 return
  98.            
  99.             end
  100.  
  101.  
  102.            
  103.             error("Uknown error @ensureCorrectItem")   
  104.            
  105.         end
  106.          
  107.          
  108.          
  109.          
  110.          
  111.         -- make sure a slot is as full as possible
  112.         function combine(slot)    
  113.                 ---Make sure slot is not already full
  114.                 turtle.select(slot)
  115.                 if turtle.getItemCount() == 64 then return end
  116.                
  117.                 if turtle.getItemCount() > 0 then
  118.                      for i=1,16 do
  119.                             turtle.select(i)
  120.                             if turtle.getItemCount() >0 then
  121.                                 if turtle.compareTo(i) then
  122.                                         turtle.transferTo(slot)
  123.                                 end    
  124.                             end
  125.                      end  
  126.                 else
  127.                     return "slot empty"
  128.                 end
  129.                
  130.            
  131.         end
  132.          
  133.         -- see if there is tree left to dig up
  134.         function isBlock()
  135.             local fwd = turtle.detect()
  136.             local up turtle.detectUp()
  137.             if fwd == true then return true end
  138.             if up == true then return true end
  139.             return false
  140.         end
  141.          
  142.          
  143.         function main()
  144.                 --- do something repeatedly
  145.                 function rpt(func,count)
  146.                                 for i=1, count do
  147.                                                 func()
  148.                                 end
  149.                 end
  150.                
  151.                 turtle.select(1)
  152.                 --- dig log then go up
  153.                 for i=1, count do
  154.                                 turtle.dig()
  155.                                 turtle.suck()
  156.                                 turtle.digUp()
  157.                                 turtle.suck()
  158.                                 turtle.up()
  159.                         -- if no logs left, go back
  160.                         if not isBlock() then
  161.                             rpt(
  162.                                 function()
  163.                                     turtle.down()
  164.                                 end, i
  165.                             )
  166.                             return
  167.                         end
  168.                        
  169.                 end
  170.                
  171.                 -- go back down if haven't already
  172.                 rpt(
  173.                         turtle.down,
  174.                         count
  175.                 )
  176.         end
  177.  
  178.  
  179.  
  180.  
  181.         isFullCounterMax = 10
  182.         isFullCounter = 0   -- allows running only every n-th call
  183.         function isFull(maxFull,force)
  184.             isFullCounter = isFullCounter + 1
  185.            
  186.             if isFullCounter < isFullCounterMax and not force then return end
  187.            
  188.             isFullCounter = 0
  189.             local c1 = combine(1)
  190.             local c2 = combine(2)
  191.             if c1=="slot empty" or c2 =="slot empty" then
  192.                 print("Please refill slots 1 and 2")
  193.                 while combine(1) == "slot empty" do
  194.                     sleep(2)
  195.                     write(".")
  196.                 end
  197.                 while combine(2) == "slot empty" do
  198.                     sleep(2)
  199.                     write(".")
  200.                 end
  201.                
  202.             end
  203.  
  204.  
  205.            
  206.             function returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  207.                 --print("slots empty / max empty: "..tostring(slotsEmpty).."/"..tostring(maxEmpty))
  208.                
  209.                 if slotsEmpty>=maxEmpty then
  210.                     return false
  211.                
  212.                 elseif slotsFull>=maxFull then
  213.                     return true
  214.                
  215.                 else
  216.                     return nil
  217.                
  218.                 end
  219.             end
  220.            
  221.             --- --maxFull    = n
  222.             local slotsEmpty = 0
  223.             local slotsFull  = 0
  224.            
  225.             local maxEmpty = 17-maxFull
  226.             for slot=1,16 do
  227.                 turtle.select(slot)
  228.                 local qtyInSlot = turtle.getItemCount()
  229.                 local isEmpty = qtyInSlot < 1
  230.                
  231.                 if isEmpty then
  232.                     slotsEmpty = slotsEmpty+1
  233.                 else
  234.                     slotsFull = slotsFull+1
  235.                 end
  236.                
  237.                 local enough = returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  238.                 if enough ~= nil then return enough end
  239.             end
  240.            
  241.            
  242.             local enough = returnIfEnough(slotsEmpty,slotsFull,maxEmpty,maxFull)
  243.             if enough ~= nil then return enough end
  244.            
  245.             error("unknown error @isFull")
  246.            
  247.            
  248.         end
  249.          
  250.         function isTree()
  251.                 --is there a tree?
  252.                 local i1,i2 = turtle.inspect()
  253.                 local block = i2.name or "nothing"
  254.                 local affix = string.sub(block,-3,-1)
  255.          
  256.                 if turtle.detect() and affix == "log" then
  257.                         main()
  258.                         turtle.select(1)
  259.                         turtle.place()
  260.                     --organise slotss
  261.  
  262.                 end
  263.         end
  264.                
  265.         while true do
  266.                 --make sure slots have correct items
  267.                 ensureCorrectItem(1,sapplingId,false)
  268.                 ensureCorrectItem(2,logId,false)
  269.                 ensureCorrectItem(bonemealSlot,bonemealId,true)
  270.  
  271.                 -- if there's a tree, then dig it up
  272.                 isTree()
  273.  
  274.                 --idle some
  275.                 sleep(idleTime )
  276.                
  277.                 --refuel if neccesary and show fuel level
  278.                 local fuelSlot = 2
  279.                 local fuel = turtle.getFuelLevel()
  280.                 while turtle.getFuelLevel() < fuelBuffer do
  281.                         turtle.select(fuelSlot)
  282.                         turtle.refuel(1)
  283.                         if turtle.getItemCount() <1 then
  284.                                 print("Please put more fuel into slot "..tostring(fuelSlot))
  285.                                 sleep(2)
  286.                                 while turtle.getItemCount() <1 do
  287.                                     sleep(2)
  288.                                     write(" .")
  289.                                 end
  290.                         end
  291.                 end
  292.                 print(turtle.getFuelLevel())
  293.            
  294.              --Place a new sappling  
  295.              turtle.select(1)
  296.              turtle.place()
  297.              
  298.              -- pickup left over items
  299.              turtle.suck() 
  300.              turtle.suckUp()
  301.            
  302.             --Allow for bonemeal being used
  303.             turtle.select(bonemealSlot)
  304.             if turtle.getItemCount() > 0 then
  305.                 turtle.place()
  306.             else
  307.             end
  308.            
  309.             --Stop if full
  310.             if isFull(maxSlotsFull) then
  311.                 print("Overfilled - please empty turtle to continue")
  312.                
  313.                 while isFull(maxSlotsFull,true) do
  314.                     sleep(2)
  315.                     write(".")
  316.                 end
  317.                
  318.                
  319.                 print("resuming")
  320.             end
  321.            
  322.  
  323.            
  324.         end
Add Comment
Please, Sign In to add comment