Advertisement
superjaja05

xpShopFurnace2.lua

Jul 19th, 2022
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local wRequire = require("wRequire")
  2. local tableFile = wRequire("tableFile")
  3. local invClient = wRequire("invClient")
  4.  
  5. rednet.open("right")
  6.  
  7. invClient.storageComputerID = 12
  8.  
  9. local width = 6
  10. local depth = 11
  11.  
  12. local botChestName = "enderstorage:ender_chest_14"
  13. local toSmeltChestName = "enderstorage:ender_chest_13"
  14. local smeltedChestName = "storagedrawers:standard_drawers_1_0"
  15.  
  16. local xpFor30levels = 1395
  17. local checkFurnaceEach = 1
  18. local checkFurnaceCount = 10000
  19.  
  20. local furnaces = tableFile.load("#furnaces.lua", {})
  21.  
  22. local function refuel()
  23.     if turtle.getFuelLevel() > 2000 then
  24.         return
  25.     end
  26.    
  27.     local networkName = peripheral.wrap("top").getNameLocal()
  28.     local botChest = peripheral.wrap(botChestName)
  29.    
  30.     turtle.select(1)
  31.     turtle.dropDown(turtle.getItemCount())
  32.    
  33.     while turtle.getFuelLevel() < 5000 do
  34.         print("Fuel: " .. tostring(turtle.getFuelLevel()) .. "/5000")
  35.         local botChestList = botChest.list()
  36.         if botChestList[1] ~= nil and botChestList[1].count > 32 then
  37.             print("Refueling")
  38.             botChest.pushItems(networkName, 1, 10, 1)
  39.             turtle.refuel(turtle.getItemCount())
  40.         else
  41.             print("Fetching fuel from storage network")
  42.             invClient.get("minecraft:charcoal.0.", 16, "enderstorage:ender_chest_2", 1)
  43.         end
  44.     end
  45. end
  46.  
  47. local function goto(x, y, action)
  48.     turtle.turnRight()
  49.     for x2 = 1, x do
  50.         turtle.forward()
  51.     end
  52.     turtle.turnLeft()
  53.    
  54.     for y2 = 1, y do
  55.         turtle.forward()
  56.     end
  57.    
  58.     action()
  59.    
  60.     for y2 = 1, y do
  61.         turtle.back()
  62.     end
  63.    
  64.     turtle.turnRight()
  65.     for x2 = 1, x do
  66.         turtle.back()
  67.     end
  68.     turtle.turnLeft()
  69. end
  70.  
  71. local function mapFurnaces()
  72.     refuel()
  73.     local toSmeltChest = peripheral.wrap(toSmeltChestName)
  74.    
  75.     turtle.select(1)
  76.     turtle.dropDown(turtle.getItemCount())
  77.    
  78.     local furnacePeripherals = { peripheral.find("minecraft:furnace") }
  79.    
  80.     for _, furnacePeripheral in pairs(furnacePeripherals) do
  81.         furnacePeripheral.pushItems(peripheral.wrap("top").getNameLocal(), 1, 64)
  82.     end
  83.    
  84.     while turtle.getItemCount() < 1 do
  85.         print("Fetching filler items")
  86.         toSmeltChest.pushItems(peripheral.wrap("top").getNameLocal(), 1, 1)
  87.     end
  88.    
  89.     for _, furnacePeripheral in pairs(furnacePeripherals) do
  90.         local furnaceName = peripheral.getName(furnacePeripheral)
  91.         if furnaces[furnaceName] == nil then
  92.             furnacePeripheral.pullItems(peripheral.wrap("top").getNameLocal(), 1, 1)
  93.             print("Searching for position of furnace: " .. furnaceName)
  94.             local found = false
  95.            
  96.             local function positionAlreadyFound(x, y)
  97.                 for _, furnace in pairs(furnaces) do
  98.                     if furnace.x == x and furnace.y == y then
  99.                         return true
  100.                     end
  101.                 end
  102.                 return false
  103.             end
  104.            
  105.             local function find()
  106.                 for x = 0, (width - 1) do
  107.                     for y = 0, (depth - 1) do
  108.                         if (x == 0 and y == 0) or positionAlreadyFound(x, y) then
  109.                         else
  110.                             goto(x, y, function()
  111.                                 local above = peripheral.wrap("top")
  112.                                 if above ~= nil then
  113.                                     local aboveList = above.list()
  114.                                     if aboveList[1] ~= nil and aboveList[1].count == 1 then
  115.                                         found = true
  116.                                     end
  117.                                 end
  118.                             end)
  119.                            
  120.                             if found then
  121.                                 print("Found position of furnace: " .. furnaceName .. " " .. tostring(x) .. ", " .. tostring(y))
  122.                                 furnaces[furnaceName] = {
  123.                                     ["name"] = furnaceName,
  124.                                     ["x"] = x,
  125.                                     ["y"] = y,
  126.                                     ["xp"] = 0
  127.                                 }
  128.                                 tableFile.save(furnaces, "#furnaces.lua")
  129.                                 rednet.broadcast(furnaces, "bbXpShop-r2s")
  130.                                 furnacePeripheral.pushItems(peripheral.wrap("top").getNameLocal(), 1, 64)
  131.                                 return
  132.                             end
  133.                         end
  134.                     end
  135.                 end
  136.             end
  137.            
  138.             find()
  139.        
  140.             if found == false then
  141.                 error("Not found")
  142.             end
  143.         end
  144.     end
  145.    
  146.     turtle.dropDown(64)
  147. end
  148.  
  149. mapFurnaces()
  150.  
  151. local xpRequested = 0
  152.  
  153. local function networkLoop()
  154.     --rednet.broadcast("ready", "bbXpShop-r2s")
  155.     while true do
  156.         local id, message = rednet.receive("bbXpShop-s2r", 10)
  157.         if message ~= nil then
  158.             xpRequested = xpRequested + message
  159.             print(xpRequested)
  160.         end
  161.     end
  162. end
  163.  
  164. local function getXp()
  165.     if xpRequested > 0 then
  166.         for _, furnace in pairs(furnaces) do
  167.             os.sleep()
  168.             if furnace.xp >= xpRequested then
  169.                 refuel()
  170.                 goto(furnace.x, furnace.y, function()
  171.                     turtle.digUp()
  172.                     if furnace.y > 1 then
  173.                         turtle.back()
  174.                         sleep(0.5)
  175.                         turtle.forward()
  176.                     else
  177.                         turtle.forward()
  178.                         sleep(0.5)
  179.                         turtle.back()
  180.                     end
  181.                     for i = 1, 16 do
  182.                         turtle.select(i)
  183.                         local itemDetail = turtle.getItemDetail()
  184.                         if itemDetail ~= nil and itemDetail.name == "minecraft:furnace" then
  185.                             turtle.placeUp()
  186.                             break
  187.                         else
  188.                             turtle.dropDown(turtle.getItemCount())
  189.                         end
  190.                     end
  191.                 end)
  192.                
  193.                 xpRequested = xpRequested - furnace.xp
  194.                 furnaces[furnace.name].xp = 0
  195.                 tableFile.save(furnaces, "#furnaces.lua")
  196.                 rednet.broadcast(furnaces, "bbXpShop-r2s")
  197.                
  198.                 if xpRequested <= 0 then
  199.                     xpRequested = 0
  200.                     break
  201.                 end
  202.             end
  203.         end
  204.     end
  205. end
  206.  
  207. local function checkFurnaces()
  208.     local checked = 0
  209.     for _, furnace in pairs(furnaces) do
  210.         os.sleep()
  211.         if furnace.timesSinceLastCheck == nil then
  212.             furnace.timesSinceLastCheck = 0
  213.         end
  214.         if furnace.timesSinceLastCheck >= checkFurnaceEach and checked < checkFurnaceCount and furnaces[furnace.name].xp < xpFor30levels then
  215.             furnace.timesSinceLastCheck = 0
  216.             checked = checked + 1
  217.             local f = peripheral.wrap(furnace.name)
  218.             if f == nil then
  219.                 local furnacePeripherals = { peripheral.find("minecraft:furnace") }
  220.                 for _, furnacePeripheral in pairs(furnacePeripherals) do
  221.                     local fName = peripheral.getName(furnacePeripheral)
  222.                     local inList = false
  223.                     for _, furnace2 in pairs(furnaces) do
  224.                         if furnace2.name == fName then
  225.                             inList = true
  226.                         end
  227.                     end
  228.                     if inList == false then
  229.                         furnace.name = fName
  230.                         f = peripheral.wrap(furnace.name)
  231.                         break
  232.                     end
  233.                 end
  234.             end
  235.            
  236.             if f == nil then
  237.                 error("Furnace not found")
  238.             end
  239.            
  240.             local fList = f.list()
  241.            
  242.             --take items out
  243.             if fList[3] ~= nil and fList[3].count >= 8 then
  244.                 f.pushItems(smeltedChestName, 3, fList[3].count)
  245.                 furnaces[furnace.name].xp = furnaces[furnace.name].xp + fList[3].count
  246.                 tableFile.save(furnaces, "#furnaces.lua")
  247.                 rednet.broadcast(furnaces, "bbXpShop-r2s")
  248.             end
  249.            
  250.             --restock
  251.             if fList[1] == nil or fList[1].count == 0 and furnaces[furnace.name].xp < xpFor30levels then
  252.                 local toSmeltPulled = 0
  253.                 for i = 1, 27 do
  254.                     if toSmeltPulled < 8 then
  255.                         toSmeltPulled = toSmeltPulled + f.pullItems(toSmeltChestName, i, 8 - toSmeltPulled, 1)
  256.                     end
  257.                 end
  258.                
  259.                 local botChest = peripheral.wrap(botChestName)
  260.                 while f.list()[2] == nil or f.list()[2].count == 0 do
  261.                     local botChestList = botChest.list()
  262.                     if botChestList[1] ~= nil and botChestList[1].count > 32 then
  263.                         if f.pullItems(botChestName, 1, 1, 2) > 0 then
  264.                             break
  265.                         end
  266.                     else
  267.                         invClient.get("minecraft:charcoal.0.", 16, "enderstorage:ender_chest_2", 1)
  268.                     end
  269.                 end
  270.             end
  271.            
  272.             getXp()
  273.         else
  274.             furnace.timesSinceLastCheck = furnace.timesSinceLastCheck + 1
  275.         end
  276.     end
  277. end
  278.  
  279. local function actionLoop()
  280.     while true do
  281.         checkFurnaces()
  282.         getXp()
  283.     end
  284. end
  285.  
  286. parallel.waitForAny(networkLoop, actionLoop)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement