GrooveypenguinX

AE2Warehouse

Jan 4th, 2025 (edited)
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. -- AE2Warehouse.lua
  2. -- Improved Version by AshGrey
  3.  
  4. -------------------------------------------------------------------------------
  5. -- CONFIGURATION
  6. -------------------------------------------------------------------------------
  7.  
  8. local config = {
  9. WarehouseDirection = "up",
  10. aeBridgeSide = "top",
  11. transmissionChannel = 1,
  12. daytimeStart = 6,
  13. daytimeEnd = 18,
  14. scanInterval = 30,
  15. }
  16.  
  17. -------------------------------------------------------------------------------
  18. -- INITIALIZATION
  19. -------------------------------------------------------------------------------
  20.  
  21. -- Initialize peripherals with error handling
  22. local aeBridge = peripheral.find("meBridge") or error("AE2 Bridge not found.")
  23. local colony = peripheral.find("colonyIntegrator") or error("Colony Integrator not found.")
  24. assert(colony.isInColony(), "Colony Integrator is not in a colony.")
  25. local modem = peripheral.find("modem") or error("Modem not found")
  26.  
  27. -- Initialize monitor (optional)
  28. local monitor = peripheral.find("monitor")
  29. if monitor then
  30. monitor.clear()
  31. monitor.setTextScale(0.5)
  32. end
  33.  
  34. -------------------------------------------------------------------------------
  35. -- FUNCTIONS
  36. -------------------------------------------------------------------------------
  37.  
  38. -- Function to split a string by a separator (e.g., space, newline)
  39. local function split(inputStr, separator)
  40. local result = {}
  41. for match in (inputStr .. separator):gmatch("(.-)" .. separator) do
  42. table.insert(result, match)
  43. end
  44. return result
  45. end
  46.  
  47. -- Improved Log function with monitor support (optional)
  48. local function log(level, message)
  49. local formattedMessage = string.format("[%s] [%s] %s", textutils.formatTime(os.time()), level, message)
  50.  
  51. -- Print to the console
  52. print(formattedMessage)
  53.  
  54. -- Display message on the monitor (if available)
  55. if monitor then
  56. local cursorX, cursorY = monitor.getCursorPos()
  57. local monitorWidth, monitorHeight = monitor.getSize()
  58.  
  59. if cursorY >= monitorHeight then
  60. monitor.scroll(1)
  61. cursorY = monitorHeight - 1
  62. end
  63.  
  64. local wrappedMessage = ""
  65. local startIndex = 1
  66. while startIndex <= #formattedMessage do
  67. local cutIndex = startIndex + monitorWidth - 1
  68. if cutIndex > #formattedMessage then
  69. cutIndex = #formattedMessage
  70. else
  71. while cutIndex > startIndex and formattedMessage:sub(cutIndex, cutIndex) ~= " " do
  72. cutIndex = cutIndex - 1
  73. end
  74. end
  75. wrappedMessage = wrappedMessage .. formattedMessage:sub(startIndex, cutIndex) .. "\n"
  76. startIndex = cutIndex + 1
  77. end
  78.  
  79. monitor.setCursorPos(1, cursorY + 1)
  80. monitor.write(wrappedMessage)
  81. end
  82. end
  83.  
  84. -- Function to strip NBT data
  85. local function stripNBT(itemName)
  86. return itemName:match("([^%{]+)")
  87. end
  88.  
  89. -- Export items to storage
  90. local function exportItem(name, count)
  91. local itemData = {name = name, count = count}
  92. local success, err = aeBridge.exportItem(itemData, config.WarehouseDirection)
  93.  
  94. if success then
  95. log("INFO", "Exported: " .. name .. " x " .. count)
  96. return count
  97. else
  98. log("ERROR", "Failed to export: " .. name .. " - " .. err)
  99. return 0
  100. end
  101. end
  102.  
  103. -- Craft items using AE2
  104. local function craftItem(name, count)
  105. local success = aeBridge.craftItem({name = name, count = count})
  106. if success then
  107. log("INFO", "Crafting initiated: " .. name .. " x " .. count)
  108. else
  109. log("ERROR", "Crafting failed for: " .. name)
  110. end
  111. return success
  112. end
  113.  
  114. -- Enhanced block request handler with more logging
  115. function handleWorkRequests()
  116. local requests = colony.getRequests()
  117. local items = aeBridge.listItems() -- Fetch all AE2 items
  118.  
  119. -- Check if items were successfully fetched
  120. if not items then
  121. log("ERROR", "Failed to fetch items from AE2. Retrying in the next cycle.")
  122. return -- Skip this cycle and try again later
  123. end
  124.  
  125. local itemMap = {}
  126. local missingItems = {}
  127. local domumData = {} -- Store domum_ornamentum item data
  128. local normalItemsDisplayName = {} -- Store display names for normal items
  129. local consolidatedMessage = "" -- Collect all messages
  130.  
  131. -- Map item amounts for quick access
  132. for _, item in ipairs(items) do
  133. local strippedName = stripNBT(item.name) -- Strip NBT data
  134. itemMap[strippedName] = item.amount
  135. end
  136.  
  137. for _, req in pairs(requests) do
  138. local itemName = stripNBT(req.items[1].name) -- Strip NBT from requested item
  139. local needed = req.count
  140. local provided = 0
  141.  
  142. -- Handle 'domum_ornamentum' items
  143. if itemName:match("^domum_ornamentum") then
  144.  
  145. local textureData = {}
  146. if req.items[1].nbt and req.items[1].nbt.textureData then
  147. for _, textureValues in pairs(req.items[1].nbt.textureData) do
  148. table.insert(textureData, textureValues)
  149. end
  150. end
  151. local blockData = {}
  152. local blockCount = 1
  153. for _, block in ipairs(textureData) do
  154. table.insert(blockData, " Block" .. blockCount .. ": " .. block)
  155. blockCount = blockCount + 1
  156. end
  157. domumData[itemName] = {
  158. displayName = req.items[1].displayName,
  159. textureData = blockData
  160. }
  161. else
  162. -- Handle normal items (non-"domum_ornamentum")
  163. normalItemsDisplayName[itemName] = req.items[1].displayName
  164. end
  165.  
  166. -- Export items
  167. if itemMap[itemName] and itemMap[itemName] > 0 then
  168. provided = exportItem(itemName, needed)
  169. else
  170. log("WARNING", "Requested item not available in AE2: " .. itemName)
  171. end
  172.  
  173. -- Craft missing items
  174. if provided < needed then
  175. local toCraft = needed - provided
  176. local crafting = craftItem(itemName, toCraft)
  177.  
  178. if crafting then
  179. log("INFO", "Crafting initiated: " .. toCraft .. " x " .. itemName)
  180. else
  181. log("ERROR", "Unable to craft: " .. itemName)
  182. end
  183.  
  184. -- Track missing items
  185. missingItems[itemName] = (missingItems[itemName] or 0) + toCraft
  186. end
  187. end
  188.  
  189. -- Append missing items to the consolidated message
  190. if next(missingItems) then
  191. for itemName, count in pairs(missingItems) do
  192. if itemName:match("^domum_ornamentum") then
  193. local itemData = domumData[itemName]
  194. consolidatedMessage = consolidatedMessage .. string.format("%s:\nDisplay Name: %s\nBlocks Needed: %s\nMissing Count: %d\n",
  195. itemName, itemData.displayName, table.concat(itemData.textureData, ", "), count)
  196. else
  197. local displayName = normalItemsDisplayName[itemName] or "Unknown Display Name"
  198. consolidatedMessage = consolidatedMessage .. string.format("%s:\nDisplay Name: %s\nMissing Count: %d\n", itemName, displayName, count)
  199. end
  200. end
  201. end
  202.  
  203. -- Transmit the consolidated message
  204. modem.transmit(config.transmissionChannel, config.transmissionChannel, consolidatedMessage)
  205. end
  206.  
  207.  
  208. -------------------------------------------------------------------------------
  209. -- MAIN LOOP
  210. -------------------------------------------------------------------------------
  211.  
  212. log("INFO", "Starting AE2 Warehouse Monitor...")
  213. while true do
  214. local time = os.time()
  215. if time >= config.daytimeStart and time < config.daytimeEnd then
  216. handleWorkRequests()
  217. else
  218. log("INFO", "Nighttime detected. Pausing...")
  219. end
  220. sleep(config.scanInterval)
  221. end
Advertisement
Add Comment
Please, Sign In to add comment