Advertisement
hornedcommando

Minecraft ComputerCraft Modem Stirling Dynamo TopUp Above and Beyond

Nov 15th, 2024
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.46 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A program which keeps the fuel on stirling dynamos topped up
  3. ---this configuration uses sticks because if your treefarm is setup you should have more than enough
  4. ---but other fuels can easily be added if needed
  5. ---Looks through all storage drawers and all stirling generator so no configuration needed there
  6. ---
  7.  
  8. --By: hornedcommando
  9.  
  10. -- IMPORTANT: change this if you want to cook more than just sticks
  11. local fuelItem = "minecraft:stick"
  12.  
  13. -- Function to dynamically find all connected storage drawers
  14. local function getConnectedDrawers()
  15.     local drawers = {}
  16.     for _, name in ipairs(peripheral.getNames()) do
  17.         if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
  18.             local drawer = peripheral.wrap(name)
  19.             table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
  20.         end
  21.     end
  22.     return drawers
  23. end
  24.  
  25. -- Function to dynamically find all connected Stirling Dynamos
  26. local function getConnectedDynamos()
  27.     local dynamos = {}
  28.     for _, name in ipairs(peripheral.getNames()) do
  29.         if peripheral.getType(name):find("thermal:dynamo_stirling") then
  30.             local dynamo = peripheral.wrap(name)
  31.             table.insert(dynamos, {name = name, dynamo = dynamo}) -- Store both name and wrapped dynamo
  32.         end
  33.     end
  34.     return dynamos
  35. end
  36.  
  37. -- Function to count items in drawers
  38. local function countItems(drawers, itemName)
  39.     local count = 0
  40.     for _, drawerInfo in ipairs(drawers) do
  41.         local drawerContents = drawerInfo.drawer.list()
  42.         for _, item in pairs(drawerContents) do
  43.             if item.name == itemName then
  44.                 count = count + item.count
  45.             end
  46.         end
  47.     end
  48.     return count
  49. end
  50.  
  51. -- Function to push items from drawers to a specific peripheral
  52. local function pushItems(drawers, itemName, targetPeripheral, amount)
  53.     for _, drawerInfo in ipairs(drawers) do
  54.         local drawerContents = drawerInfo.drawer.list()
  55.         for slot, item in pairs(drawerContents) do
  56.             if item.name == itemName then
  57.                 local success = drawerInfo.drawer.pushItems(peripheral.getName(targetPeripheral), slot, amount)
  58.                 if success > 0 then
  59.                     amount = amount - success
  60.                     if amount <= 0 then
  61.                         return true -- Successfully pushed the required amount
  62.                     end
  63.                 else
  64.                     print("Failed to push item: " .. item.name)
  65.                 end
  66.             end
  67.         end
  68.     end
  69.     return false -- Failed to push the required amount
  70. end
  71.  
  72. -- Main function
  73. local function main()
  74.     while true do
  75.         local drawers = getConnectedDrawers()
  76.         local dynamos = getConnectedDynamos()
  77.         local stickCount = countItems(drawers, fuelItem)
  78.  
  79.         if stickCount >= #dynamos * 64 then
  80.             for _, dynamoInfo in ipairs(dynamos) do
  81.                 print("Topping up dynamo: " .. dynamoInfo.name)
  82.                 local success = pushItems(drawers, fuelItem, dynamoInfo.dynamo, 64)
  83.                 if not success then
  84.                     print("Failed to top up dynamo: " .. dynamoInfo.name)
  85.                 end
  86.             end
  87.         else
  88.             print("Not enough sticks available to top up all dynamos.")
  89.         end
  90.  
  91.         os.sleep(60) -- This is about how fast 1 dynamo can burn through 64 sticks at max output
  92.     end
  93. end
  94.  
  95. -- Start the main function
  96. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement