Advertisement
hornedcommando

Minecraft ComputerCraft Modem DrawerController Above and Beyond

Nov 16th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.10 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A storage control program which acts as a drawer controller
  3. ---Barrels can be defined below, and their contents will be added to drawers
  4. ---if no drawer is found with the contents then it's placed randomly
  5. ---
  6.  
  7. --By: Spank
  8.  
  9. -- Define peripherals IMPORTANT: change these based on the outputs for your farms
  10. local barrels = {
  11.     peripheral.wrap("minecraft:barrel_0"), -- First barrel
  12.     peripheral.wrap("minecraft:barrel_3"), -- Third barrel
  13.     peripheral.wrap("minecraft:barrel_2"),  -- Second barrel
  14.     peripheral.wrap("minecraft:barrel_5"),  -- Fifth barrel
  15.     peripheral.wrap("minecraft:hopper_0")  -- First hopper
  16. }
  17.  
  18. -- Function to dynamically find all connected storage drawers
  19. local function getConnectedDrawers()
  20.     local drawers = {}
  21.     for _, name in ipairs(peripheral.getNames()) do
  22.         if peripheral.getType(name):find("storagedrawers:standard_drawers_1") then
  23.             local drawer = peripheral.wrap(name)
  24.             table.insert(drawers, {name = name, drawer = drawer}) -- Store both name and wrapped drawer
  25.         end
  26.     end
  27.     return drawers
  28. end
  29.  
  30. -- Function to check if a drawer contains a specific item
  31. local function findDrawerForItem(drawers, itemName)
  32.     for _, drawerInfo in ipairs(drawers) do
  33.         local drawerContents = drawerInfo.drawer.list()
  34.         if drawerContents[2] and drawerContents[2].name == itemName then
  35.             return drawerInfo -- Return the drawerInfo which includes the name
  36.         end
  37.     end
  38.     return nil -- Return nil if no drawer currently contains the item
  39. end
  40.  
  41. -- Function to push items from a single barrel to drawers
  42. local function pushItemsFromBarrel(barrel)
  43.     local barrelContents = barrel.list()
  44.     if not barrelContents or next(barrelContents) == nil then
  45.         return true -- No items in barrel, nothing to process
  46.     end
  47.  
  48.     -- Get the updated list of connected drawers
  49.     local drawers = getConnectedDrawers()
  50.  
  51.     for slot, item in pairs(barrelContents) do
  52.         local targetDrawerInfo = findDrawerForItem(drawers, item.name)
  53.  
  54.         -- If no specific drawer for the item, find an empty one
  55.         if not targetDrawerInfo then
  56.             for _, drawerInfo in ipairs(drawers) do
  57.                 if not drawerInfo.drawer.list()[2] then
  58.                     targetDrawerInfo = drawerInfo
  59.                     break
  60.                 end
  61.             end
  62.         end
  63.  
  64.         -- Attempt to push items to the drawer
  65.         if targetDrawerInfo then
  66.             local drawerName = peripheral.getName(targetDrawerInfo.drawer) -- Get the peripheral name of the drawer
  67.             print("Attempting to push to drawer: " .. drawerName)
  68.  
  69.             -- Push items to the drawer and check success
  70.             local success = barrel.pushItems(drawerName, slot, item.count, 2)
  71.             if success == 0 then
  72.                 print("Cannot push item to drawer: " .. drawerName .. " from barrel " .. peripheral.getName(barrel) .. "! Check storage capacity.")
  73.                 return false -- Failed push, continue checking other barrels
  74.             end
  75.         else
  76.             print("No drawer available for item " .. item.name .. " in barrel " .. peripheral.getName(barrel) .. "! Add or upgrade a drawer.")
  77.             return false -- No drawer available for this item
  78.         end
  79.     end
  80.     return true -- Successfully processed all items in this barrel
  81. end
  82.  
  83. -- Main loop function
  84. local function main()
  85.     while true do
  86.         local allSuccess = true
  87.  
  88.         for _, barrel in ipairs(barrels) do
  89.             -- Try pushing items from the current barrel
  90.             local success = pushItemsFromBarrel(barrel)
  91.             if not success then
  92.                 allSuccess = false -- Mark as false if any barrel fails
  93.             end
  94.         end
  95.  
  96.         -- Wait before rechecking if all barrels were successful, or retry immediately if there was an issue
  97.         if allSuccess then
  98.             os.sleep(10) -- Wait 10 seconds before checking again
  99.         else
  100.             os.sleep(2) -- Short delay to allow immediate retry if a barrel fails
  101.         end
  102.     end
  103. end
  104.  
  105. -- Start the main function
  106. main()
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement