Guest User

xpShopFurnace.lua

a guest
Jul 19th, 2022
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.18 KB | None | 0 0
  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.             if furnace.xp >= xpRequested then
  168.                 refuel()
  169.                 goto(furnace.x, furnace.y, function()
  170.                     turtle.digUp()
  171.                     if furnace.y > 1 then
  172.                         turtle.back()
  173.                         sleep(0.5)
  174.                         turtle.forward()
  175.                     else
  176.                         turtle.forward()
  177.                         sleep(0.5)
  178.                         turtle.back()
  179.                     end
  180.                     for i = 1, 16 do
  181.                         turtle.select(i)
  182.                         local itemDetail = turtle.getItemDetail()
  183.                         if itemDetail ~= nil and itemDetail.name == "minecraft:furnace" then
  184.                             turtle.placeUp()
  185.                             break
  186.                         else
  187.                             turtle.dropDown(turtle.getItemCount())
  188.                         end
  189.                     end
  190.                 end)
  191.                
  192.                 xpRequested = xpRequested - furnace.xp
  193.                 furnaces[furnace.name].xp = 0
  194.                 tableFile.save(furnaces, "#furnaces.lua")
  195.                 rednet.broadcast(furnaces, "bbXpShop-r2s")
  196.                
  197.                 if xpRequested <= 0 then
  198.                     xpRequested = 0
  199.                     break
  200.                 end
  201.             end
  202.         end
  203.     end
  204. end
  205.  
  206. local function checkFurnaces()
  207.     local checked = 0
  208.     for _, furnace in pairs(furnaces) do
  209.         if furnace.timesSinceLastCheck == nil then
  210.             furnace.timesSinceLastCheck = 0
  211.         end
  212.         if furnace.timesSinceLastCheck >= checkFurnaceEach and checked < checkFurnaceCount and furnaces[furnace.name].xp < xpFor30levels then
  213.             furnace.timesSinceLastCheck = 0
  214.             checked = checked + 1
  215.             local f = peripheral.wrap(furnace.name)
  216.             if f == nil then
  217.                 local furnacePeripherals = { peripheral.find("minecraft:furnace") }
  218.                 for _, furnacePeripheral in pairs(furnacePeripherals) do
  219.                     local fName = peripheral.getName(furnacePeripheral)
  220.                     local inList = false
  221.                     for _, furnace2 in pairs(furnaces) do
  222.                         if furnace2.name == fName then
  223.                             inList = true
  224.                         end
  225.                     end
  226.                     if inList == false then
  227.                         furnace.name = fName
  228.                         f = peripheral.wrap(furnace.name)
  229.                         break
  230.                     end
  231.                 end
  232.             end
  233.            
  234.             if f == nil then
  235.                 error("Furnace not found")
  236.             end
  237.            
  238.             local fList = f.list()
  239.            
  240.             --take items out
  241.             if fList[3] ~= nil and fList[3].count >= 8 then
  242.                 f.pushItems(smeltedChestName, 3, fList[3].count)
  243.                 furnaces[furnace.name].xp = furnaces[furnace.name].xp + fList[3].count
  244.                 tableFile.save(furnaces, "#furnaces.lua")
  245.                 rednet.broadcast(furnaces, "bbXpShop-r2s")
  246.             end
  247.            
  248.             --restock
  249.             if fList[1] == nil or fList[1].count == 0 and furnaces[furnace.name].xp < xpFor30levels then
  250.                 local toSmeltPulled = 0
  251.                 for i = 1, 27 do
  252.                     if toSmeltPulled < 8 then
  253.                         toSmeltPulled = toSmeltPulled + f.pullItems(toSmeltChestName, i, 8 - toSmeltPulled, 1)
  254.                     end
  255.                 end
  256.                
  257.                 local botChest = peripheral.wrap(botChestName)
  258.                 while f.list()[2] == nil or f.list()[2].count == 0 do
  259.                     local botChestList = botChest.list()
  260.                     if botChestList[1] ~= nil and botChestList[1].count > 32 then
  261.                         if f.pullItems(botChestName, 1, 1, 2) > 0 then
  262.                             break
  263.                         end
  264.                     else
  265.                         invClient.get("minecraft:charcoal.0.", 16, "enderstorage:ender_chest_2", 1)
  266.                     end
  267.                 end
  268.             end
  269.            
  270.             getXp()
  271.         else
  272.             furnace.timesSinceLastCheck = furnace.timesSinceLastCheck + 1
  273.         end
  274.     end
  275. end
  276.  
  277. local function actionLoop()
  278.     while true do
  279.         checkFurnaces()
  280.         getXp()
  281.     end
  282. end
  283.  
  284. parallel.waitForAny(networkLoop, actionLoop)
Advertisement
Add Comment
Please, Sign In to add comment