JustDoesGames

Furni Program (Modified)

Jan 1st, 2021
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.98 KB | None | 0 0
  1. --[[
  2. Use multiple furnaces at once to smelt items.
  3.  
  4. SETUP:
  5.     Place an introspection module into the turtles inventory.
  6.     Connect turtle to milo network with a wired modem.
  7.     Connect turtle to a second wired modem that is connected to furnaces ONLY.
  8.     Add as many furnaces as needed.
  9.  
  10. CONFIGURATION:
  11.     Set turtle as a "Generic Inventory"
  12.     export coal to slot 2
  13.     import from slot 3
  14.  
  15. Use this turtle for machine crafting.
  16. --]]
  17.  
  18. local Event      = require('opus.event')
  19. local Util       = require('opus.util')
  20.  
  21. local device     = _G.device
  22. local fs         = _G.fs
  23. local os         = _G.os
  24. local peripheral = _G.peripheral
  25. local turtle     = _G.turtle
  26.  
  27. local STARTUP_FILE = 'usr/autorun/miloFurni.lua'
  28. local SMELT_AMOUNT = 8
  29. local INPUT_SLOT   = 1
  30. local FUEL_SLOT    = 2
  31. local OUTPUT_SLOT  = 3
  32.  
  33. local function equip(side, item, rawName)
  34.     local equipped = peripheral.getType(side)
  35.  
  36.     if equipped == item then
  37.         return true
  38.     end
  39.  
  40.     if not turtle.equip(side, rawName or item) then
  41.         if not turtle.selectSlotWithQuantity(0) then
  42.             error('No slots available')
  43.         end
  44.         turtle.equip(side)
  45.         if not turtle.equip(side, item) then
  46.             error('Unable to equip ' .. item)
  47.         end
  48.     end
  49.  
  50.     turtle.select(1)
  51. end
  52.  
  53. equip('left', 'plethora:introspection', 'plethora:module:0')
  54. local intro = device['plethora:introspection']
  55. local inv = peripheral.wrap("front") -- CHANGE (Wrap chest in front of turtle)
  56.  
  57. if not fs.exists(STARTUP_FILE) then
  58.     Util.writeFile(STARTUP_FILE,
  59.         [[os.sleep(1)
  60. shell.openForegroundTab('packages/miloApps/apps/furni')]])
  61. end
  62.  
  63. local furnaces
  64. local localName
  65.  
  66. print('detecting wired modem connected to furnaces...')
  67. for _, dev in pairs(device) do
  68.     if dev.type == 'wired_modem' and dev.getNameLocal then
  69.         local list = dev.getNamesRemote()
  70.         furnaces = { }
  71.         localName = "minecraft:chest_0" -- CHANGE (Change this to the chest name)
  72.         for _, name in pairs(list) do
  73.             if device[name].type == 'minecraft:furnace' then -- CHANGE (Remove both lines and change ~= to ==)
  74.                 table.insert(furnaces, {
  75.                     dev = device[name],
  76.                     list = device[name].list(),
  77.                 })
  78.             end
  79.         end
  80.     end
  81.     if furnaces then
  82.         print('Using wired modem: '  .. dev.name)
  83.         print('Furnaces: ' .. #furnaces)
  84.         break
  85.     end
  86. end
  87.  
  88. if not furnaces then
  89.     error('Turtle must be connected to a second wired_modem connected to furnaces only')
  90. end
  91.  
  92. _G.printError([[Program must be restarted if new furnaces are added.]])
  93.  
  94. local function getSlot(furnace, slotNo)
  95.     if not furnace.list[slotNo] then
  96.         furnace.list[slotNo] = {
  97.             count = 0
  98.         }
  99.     end
  100.     return furnace.list[slotNo]
  101. end
  102.  
  103. local function process(list)
  104.     local inItem = list[INPUT_SLOT]
  105.     local inFuel = list[FUEL_SLOT]
  106.     local inReturn = list[OUTPUT_SLOT] or { count = 0 }
  107.  
  108.     for _, furnace in ipairs(Util.shallowCopy(furnaces)) do
  109.         local s, m = pcall(function()
  110.             if furnace.list[INPUT_SLOT] and furnace.list[INPUT_SLOT].count > 0 then
  111.                 furnace.list = furnace.dev.list()
  112.                 print('listing ' .. furnace.dev.name)
  113.             end
  114.  
  115.             -- items to cook
  116.             local cooking = getSlot(furnace, INPUT_SLOT)
  117.             if cooking.count < 64 and inItem and inItem.count > 0 then
  118.                 if cooking.count == 0 or cooking.name == inItem.name then
  119.                     print('cooking : ' .. furnace.dev.name)
  120.                     local count = furnace.dev.pullItems(localName, INPUT_SLOT, SMELT_AMOUNT, INPUT_SLOT)
  121.  
  122.                     if count > 0 then
  123.                         inItem.count = inItem.count - count
  124.  
  125.                         cooking.name = inItem.name
  126.                         cooking.count = cooking.count + count
  127.  
  128.                         -- push to end of queue
  129.                         Util.removeByValue(furnaces, furnace)
  130.                         table.insert(furnaces, furnace)
  131.                     end
  132.                 end
  133.             end
  134.  
  135.             -- fuel
  136.             local fuel = getSlot(furnace, FUEL_SLOT)
  137.             if fuel.count < 8 and inFuel and inFuel.count > 0 then
  138.                 if fuel.count == 0 or fuel.name == inFuel.name then
  139.                     print('fueling ' .. furnace.dev.name)
  140.                     local count = furnace.dev.pullItems(localName, FUEL_SLOT, 8 - fuel.count, FUEL_SLOT)
  141.                     if count > 0 then
  142.                         inFuel.count = inFuel.count - count
  143.  
  144.                         fuel.name = inFuel.name
  145.                         fuel.count = fuel.count + count
  146.                     end
  147.                 end
  148.             end
  149.  
  150.             local result = getSlot(furnace, OUTPUT_SLOT)
  151.             if result.count > 0 then
  152.                 if inReturn.count == 0 or result.name == inReturn.name then
  153.                     print('pulling from : ' .. furnace.dev.name)
  154.                     local count = furnace.dev.pushItems(localName, OUTPUT_SLOT, result.count, OUTPUT_SLOT)
  155.  
  156.                     if count > 0 then
  157.                         result.count = result.count - count
  158.                         if result.count == 0 then
  159.                             furnace.list[OUTPUT_SLOT] = nil
  160.                         end
  161.  
  162.                         inReturn.name = result.name
  163.                         inReturn.count = inReturn.count + count
  164.                     end
  165.                 end
  166.             end
  167.         end)
  168.         if not s and m then
  169.             _G.printError(m)
  170.         end
  171.     end
  172. end
  173.  
  174. Event.on('turtle_inventory', function()
  175.     while true do process(inv.list()) print('idle') sleep(1) end -- CHANGE (For some reason looping it like this pulls the items out of the furnace correctly)
  176. end)
  177.  
  178. Event.onInterval(3, function()
  179.     os.queueEvent('turtle_inventory')
  180. end)
  181.  
  182. os.queueEvent('turtle_inventory')
  183. Event.pullEvents()
Add Comment
Please, Sign In to add comment