Advertisement
hornedcommando

Minecraft ComputerCraft Modem Furnace Above and Beyond

Nov 22nd, 2024
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.06 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A Furnace Manager program which automatically searches Occultism storage
  3. ---and moves fuel and cookable items to connected furnaces.
  4. ---Extracts cooked items back into Occultism storage.
  5. ---Uses inventory balancing to decide which fuel and cookable items to use.
  6. ---Due to furnaces being slow and holding a lot, everything is pulled back into inventory each loop
  7. ---to improve balancing accuracy
  8. ---
  9.  
  10. --By: hornedcommando
  11.  
  12. -- Define peripherals
  13. local occultism = peripheral.wrap("occultism:storage_controller_0") -- IMPORTANT: this has to match your id
  14.  
  15. -- Check if the Occultism storage controller is wrapped correctly
  16. if not occultism then
  17.     error("Occultism storage controller peripheral not found!")
  18. end
  19.  
  20. -- List of fuel items in order of preference.  IMPORTANT: change this if you have different fuel requirements
  21. local fuelWhitelist = {
  22.     "minecraft:stick",
  23.     "minecraft:charcoal",
  24.     "thermal:charcoal_block",
  25.     "minecraft:coal",
  26.     "minecraft:coal_block",
  27.     "minecraft:lava_bucket"
  28. }
  29.  
  30. -- List of always cookable items IMPORTANT: these are items that have limited use beyond cooking, change as needed
  31. local alwaysCookableItems = {
  32.     "farmersdelight:wheat_dough",
  33.     "tconstruct:grout",
  34.     "thermal:rubber",
  35.     "forbidden_arcanus:tentacle",
  36.     "occultism:iesnium_dust",
  37.     "thermal:enderium_dust",
  38.     "buddycards:deep_luminis_crystal",
  39.     "buddycards:luminis_crystal",
  40.     "improvedbackpacks:bound_leather",
  41.     "architects_palatte:algal_blend"
  42. }
  43.  
  44. -- List of inventory balanced items IMPORTANT: these items will not process if Ingredient < Output, change as needed
  45. local balancedItems = {
  46.     ["aquaculture:fish_fillet_raw"] = "aquaculture:fish_fillet_cooked",
  47.     ["minecraft:rabbit"] = "minecraft:cooked_rabbit",
  48.     ["farmersdelight:minced_beef"] = "farmersdelight:beef_patty",
  49.     ["farmersdelight:salmon_slice"] = "farmersdelight:cooked_salmon_slice",
  50.     ["create:diorite_cobblestone"] = "minecraft:diorite",
  51.     ["create:gabbro_cobblestone"] = "create:gabbro",
  52.     ["minecraft:cod"] = "minecraft:cooked_cod",
  53.     ["minecraft:porkchop"] = "minecraft:cooked_porkchop",
  54.     ["minecraft:cobblestone"] = "minecraft:stone",
  55.     ["minecraft:potato"] = "minecraft:baked_potato",
  56.     ["minecraft:egg"] = "farmersdelight:fried_egg",
  57.     ["minecraft:salmon"] = "minecraft:cooked_salmon",
  58.     ["minecraft:mutton"] = "minecraft:cooked_mutton",
  59.     ["tconstruct:nether_grout"] = "tconstruct:scorched_brick",
  60.     ["create:andesite_cobblestone"] = "minecraft:andesite",
  61.     ["minecraft:kelp"] = "minecraft:dried_kelp",
  62.     ["occultism:otherstone"] = "occultism:burnt_otherstone",
  63.     ["minecraft:beef"] = "minecraft:cooked_beef",
  64.     ["minecraft:chicken"] = "minecraft:cooked_chicken",
  65.     ["minecraft:sand"] = "minecraft:glass",
  66.     ["biomesoplenty:white_sand"] = "minecraft:glass",
  67.     ["biomesoplenty:orange_sand"] = "minecraft:glass",
  68.     ["biomesoplenty:fir_log"] = "minecraft:charcoal"
  69. }
  70.  
  71. -- Function to count specific items in the Occultism storage
  72. local function countItems(storage, itemName)
  73.     local count = 0
  74.     for slot, item in pairs(storage.list()) do
  75.         if item.name == itemName then
  76.             count = count + item.count
  77.         end
  78.     end
  79.     return count
  80. end
  81.  
  82. -- Function to find the best fuel item based on inventory balancing
  83. local function findBestFuel(storage)
  84.     for i = 1, #fuelWhitelist - 1 do
  85.         local currentFuel = fuelWhitelist[i]
  86.         local nextFuel = fuelWhitelist[i + 1]
  87.         if countItems(storage, currentFuel) < countItems(storage, nextFuel) then
  88.             return nextFuel
  89.         end
  90.     end
  91.     return fuelWhitelist[1] -- Default to the first fuel item if no better option is found
  92. end
  93.  
  94. -- Function to find the best cookable item based on inventory balancing
  95. local function findBestCookable(storage)
  96.     -- Check always cookable items first
  97.     for _, cookable in ipairs(alwaysCookableItems) do
  98.         if countItems(storage, cookable) > 0 then
  99.             return cookable
  100.         end
  101.     end
  102.  
  103.     -- Check inventory balanced items
  104.     for rawItem, cookedItem in pairs(balancedItems) do
  105.         if countItems(storage, rawItem) > countItems(storage, cookedItem) then
  106.             return rawItem
  107.         end
  108.     end
  109.  
  110.     return nil -- No cookable item found
  111. end
  112.  
  113. -- Function to push items from Occultism storage to the furnace
  114. local function pushItemsToFurnace(storage, furnace, itemName, slot)
  115.     for storageSlot, item in pairs(storage.list()) do
  116.         if item.name == itemName then
  117.             local success = storage.pushItems(peripheral.getName(furnace), storageSlot, item.count, slot)
  118.             if success == 0 then
  119.                 print("Failed to push item: " .. item.name)
  120.                 return false -- Failed push, continue checking other items
  121.             else
  122.                 print("Pushed " .. item.count .. " of " .. item.name .. " to furnace slot " .. slot)
  123.                 return true -- Successfully pushed the item
  124.             end
  125.         end
  126.     end
  127.     return false -- No item found to push
  128. end
  129.  
  130. -- Function to move all items from the furnace to the Occultism storage
  131. local function moveAllItemsToStorage(storage, furnace)
  132.     local furnaceItems = furnace.list()
  133.     for slot, item in pairs(furnaceItems) do
  134.         local success = furnace.pushItems(peripheral.getName(storage), slot, item.count)
  135.         if success == 0 then
  136.             print("Cannot push item from slot " .. slot .. " to storage! Check storage capacity.")
  137.             return false -- Failed push, continue checking other items
  138.         else
  139.             print("Moved " .. item.count .. " of " .. item.name .. " from slot " .. slot .. " to storage.")
  140.         end
  141.     end
  142.     return true -- Successfully processed all items
  143. end
  144.  
  145. -- Function to clear all items from furnaces
  146. local function clearFurnaces(storage, furnaces)
  147.     for _, furnace in ipairs(furnaces) do
  148.         moveAllItemsToStorage(storage, furnace)
  149.     end
  150. end
  151.  
  152. -- Main loop function
  153. local function main()
  154.     while true do
  155.         -- Detect all connected furnaces
  156.         local furnaces = {}
  157.         for _, name in ipairs(peripheral.getNames()) do
  158.             if peripheral.getType(name) == "minecraft:furnace" then
  159.                 table.insert(furnaces, peripheral.wrap(name))
  160.             end
  161.         end
  162.  
  163.         -- Clear all items from furnaces to get accurate counts
  164.         clearFurnaces(occultism, furnaces)
  165.  
  166.         -- Process each furnace
  167.         for _, furnace in ipairs(furnaces) do
  168.             -- Find the best fuel and cookable item
  169.             local bestFuel = findBestFuel(occultism)
  170.             local bestCookable = findBestCookable(occultism)
  171.  
  172.             -- Push fuel to slot 2
  173.             if bestFuel then
  174.                 pushItemsToFurnace(occultism, furnace, bestFuel, 2)
  175.             end
  176.  
  177.             -- Push cookable item to slot 1
  178.             if bestCookable then
  179.                 pushItemsToFurnace(occultism, furnace, bestCookable, 1)
  180.             end
  181.         end
  182.  
  183.         os.sleep(200) -- Wait 200 seconds before the next iteration
  184.     end
  185. end
  186.  
  187. -- Start the main function
  188. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement