Advertisement
JackGrey

AE2xMineColoniesXCC

Oct 16th, 2024 (edited)
842
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 1 0
  1. -- AE2Warehouse.lua
  2. -- Version 1.8
  3. -- Author: Modified by AshGrey
  4. -- Date: 2024-10-16
  5.  
  6. -- This script monitors MineColonies warehouse work requests and tries to fulfill them
  7. -- using the AE2 network. It automates requests through storage access and crafting patterns.
  8.  
  9. -------------------------------------------------------------------------------
  10. -- INITIALIZATION
  11. -------------------------------------------------------------------------------
  12.  
  13. -- Initialize the AE2 Bridge Realative to the computer
  14. local aeBridge = peripheral.find("meBridge")
  15. if not aeBridge then error("AE2 Bridge not found.") end
  16. print("AE2 Bridge initialized.")
  17.  
  18. -- Initialize Colony Integrator
  19. local colony = peripheral.find("colonyIntegrator")
  20. if not colony then error("Colony Integrator not found.") end
  21. if not colony.isInColony() then error("Colony Integrator is not in a colony.") end
  22. print("Colony Integrator initialized.")
  23.  
  24. -- Initialize Ender Modem
  25. local modem = peripheral.wrap("left") -- Wrap the Ender Modem on the left side
  26. if not modem then error("Ender Modem not found.") end
  27. print("Ender Modem initialized.")
  28.  
  29. -- Point to storage container relative to the ME Bridge (connected via ME Export Bus or Storage Bus)
  30. local storage = "up"
  31. print("Storage initialized.")
  32.  
  33. -------------------------------------------------------------------------------
  34. -- FUNCTIONS
  35. -------------------------------------------------------------------------------
  36.  
  37. -- Function to strip NBT data from item names
  38. local function stripNBT(itemName)
  39. return itemName:match("([^%{]+)") -- Match only the name before any NBT data
  40. end
  41.  
  42. -- Export items from AE2 network to storage container
  43. function exportItem(name, count)
  44. local itemData = {name = name, count = count}
  45. local success, err = aeBridge.exportItem(itemData, storage)
  46.  
  47. -- Debugging output
  48. if success then
  49. print("Successfully exported:", name, "Count:", count)
  50. return count -- Return the count requested
  51. else
  52. print("Failed to export:", name, "Error:", err)
  53. return 0 -- No items exported
  54. end
  55. end
  56.  
  57. -- Start crafting job for missing items
  58. function craftItem(name, count)
  59. local success = aeBridge.craftItem({name = name, count = count})
  60. if success then
  61. print("Crafting job scheduled:", count, "x", name)
  62. else
  63. print("Crafting failed:", name)
  64. end
  65. return success
  66. end
  67.  
  68. -- Check all open work requests from MineColonies Warehouse
  69. function handleWorkRequests()
  70. local requests = colony.getRequests()
  71. local items = aeBridge.listItems() -- Fetch all AE2 items
  72. local itemMap = {}
  73. local missingItems = {}
  74.  
  75. -- Map item amounts for quick access
  76. for _, item in ipairs(items) do
  77. local strippedName = stripNBT(item.name) -- Strip NBT data
  78. itemMap[strippedName] = item.amount
  79. end
  80.  
  81. for _, req in pairs(requests) do
  82. local itemName = stripNBT(req.items[1].name) -- Strip NBT from requested item
  83. local needed = req.count
  84. local provided = 0
  85.  
  86. print("Processing request:", itemName, "x", needed)
  87.  
  88. -- Try to export items from AE2 to storage
  89. if itemMap[itemName] and itemMap[itemName] > 0 then
  90. provided = exportItem(itemName, needed)
  91. else
  92. print("Requested item not available in AE2:", itemName)
  93. end
  94.  
  95. -- If not enough items, schedule crafting job
  96. if provided < needed then
  97. local toCraft = needed - provided
  98. local crafting = craftItem(itemName, toCraft)
  99.  
  100. if crafting then
  101. print("Crafting initiated:", toCraft, "x", itemName)
  102. else
  103. print("Unable to craft:", itemName)
  104. end
  105.  
  106. -- Track missing items for wireless monitor
  107. if missingItems[itemName] then
  108. missingItems[itemName] = missingItems[itemName] + toCraft -- Increment if already exists
  109. else
  110. missingItems[itemName] = toCraft -- Set initial value if new
  111. end
  112. end
  113. end
  114.  
  115. -- Check if there are missing items to transmit
  116. if next(missingItems) then -- Only transmit if there are missing items
  117. local serializedData = ""
  118. for itemName, count in pairs(missingItems) do
  119. -- Prepare data in a way that avoids serialization errors
  120. serializedData = serializedData .. itemName .. ":" .. count .. ";"
  121. end
  122.  
  123. -- Transmit the serialized data
  124. modem.transmit(1, 1, serializedData) -- Transmit to channel 1
  125. print("Missing items transmitted:", serializedData)
  126. end
  127. end
  128.  
  129. -------------------------------------------------------------------------------
  130. -- MAIN LOOP
  131. -------------------------------------------------------------------------------
  132.  
  133. print("Starting AE2 Warehouse Monitor...")
  134. while true do
  135. -- Only scan requests during daytime (MineColonies logic)
  136. local time = os.time()
  137. if time >= 6 and time < 18 then
  138. handleWorkRequests()
  139. else
  140. print("Nighttime detected. Pausing...")
  141. end
  142.  
  143. -- Wait 30 seconds before next scan
  144. sleep(30)
  145. end
  146.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement