RhodanBull

Untitled

Jul 11th, 2024 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.60 KB | None | 0 0
  1. -- Peripherien erkennen
  2. local meBridge = peripheral.wrap("right") -- meBridge ist rechts vom Computer
  3. local colonyIntegrator = peripheral.wrap("top") -- Colony Integrator ist oben auf dem Computer
  4. local monitor = peripheral.find("monitor")
  5. local chest = peripheral.wrap("minecraft:chest_0") -- Truhe ist mit dem Namen minecraft:chest_0 verbunden
  6.  
  7. -- Überprüfen, ob die Peripherien erkannt wurden
  8. if not meBridge then
  9. print("ME Bridge nicht gefunden!")
  10. return
  11. end
  12.  
  13. if not colonyIntegrator then
  14. print("Colony Integrator nicht gefunden!")
  15. return
  16. end
  17.  
  18. if not monitor then
  19. print("Monitor nicht gefunden!")
  20. return
  21. end
  22.  
  23. if not chest then
  24. print("Truhe nicht gefunden!")
  25. return
  26. end
  27.  
  28. -- Monitor konfigurieren
  29. monitor.setTextScale(0.5)
  30. monitor.clear()
  31. monitor.setCursorPos(1, 1)
  32.  
  33. -- Funktion zum Bereinigen der Item-Namen und Umwandeln in Lowercase
  34. function cleanItemName(name)
  35. -- Entfernt führende Leerzeichen, Ziffern, Bindestriche und weitere Zahlen
  36. -- Ersetzt Unterstriche durch Leerzeichen
  37. return string.lower(name:gsub("^%s*%d*%s*-?%d*%s*", ""):gsub("_", " "))
  38. end
  39.  
  40. -- Funktion zum Überprüfen, ob ein Item von der Liste ausgeschlossen werden soll
  41. function isExcludedItem(itemName)
  42. local excludedItems = {"helmet", "chestplate", "leggings", "boots", "smeltableOre", "compostable"}
  43. for _, excluded in ipairs(excludedItems) do
  44. if string.find(itemName, excluded) then
  45. return true
  46. end
  47. end
  48. return false
  49. end
  50.  
  51. -- Funktion zum Abrufen und Bereinigen der Item-Namen
  52. function getItemsTable()
  53. local items = meBridge.listItems()
  54. local itemsTable = {}
  55.  
  56. for _, item in ipairs(items) do
  57. local fullName = item["name"]
  58. local simpleName = cleanItemName(fullName:match(":(.*)"))
  59. itemsTable[simpleName] = {fullName = fullName, amount = item["amount"]}
  60. end
  61.  
  62. return itemsTable
  63. end
  64.  
  65.  
  66. -- Funktion zum Speichern der Hilfstabelle in einer Datei und deren Erweiterung mit neuen Items
  67. function saveItemsTableToFile(filename, itemsTable)
  68. local existingItems = {}
  69.  
  70. -- Hilfstabelle einlesen, falls sie existiert
  71. if fs.exists(filename) then
  72. local file = fs.open(filename, "r")
  73. if file then
  74. local line = file.readLine()
  75. while line do
  76. local fullName, simpleName = string.match(line, "([^,]+), ([^,]+)")
  77. if fullName and simpleName then
  78. existingItems[simpleName] = {fullName = fullName}
  79. end
  80. line = file.readLine()
  81. end
  82. file.close()
  83. end
  84. end
  85.  
  86. -- Neue Items zur Hilfstabelle hinzufügen
  87. for simpleName, data in pairs(itemsTable) do
  88. if not existingItems[simpleName] then
  89. existingItems[simpleName] = {fullName = data.fullName}
  90. end
  91. end
  92.  
  93. -- Hilfstabelle in die Datei schreiben
  94. local file = fs.open(filename, "w")
  95. if not file then
  96. print("Fehler beim Öffnen der Datei: " .. filename)
  97. return
  98. end
  99.  
  100. file.writeLine("Full Name, Simple Name")
  101. for simpleName, data in pairs(existingItems) do
  102. file.writeLine(data.fullName .. ", " .. simpleName)
  103. end
  104.  
  105. file.close()
  106. print("Hilfstabelle gespeichert in Datei: " .. filename)
  107. end
  108.  
  109. -- Funktion zum Laden der Hilfstabelle aus einer Datei
  110. function loadItemsTableFromFile(filename)
  111. local itemsTable = {}
  112.  
  113. if fs.exists(filename) then
  114. local file = fs.open(filename, "r")
  115. if file then
  116. local line = file.readLine() -- skip header line
  117. line = file.readLine()
  118. while line do
  119. local fullName, simpleName = string.match(line, "([^,]+), ([^,]+)")
  120. if fullName and simpleName then
  121. itemsTable[simpleName] = {fullName = fullName}
  122. end
  123. line = file.readLine()
  124. end
  125. file.close()
  126. end
  127. end
  128.  
  129. return itemsTable
  130. end
  131.  
  132. -- Funktion zum Überprüfen der Autocrafting-Fähigkeit im ME-System
  133. function isAutocraftable(itemName)
  134. local craftables = meBridge.listCraftableItems()
  135. for _, item in ipairs(craftables) do
  136. -- print (item["name"])
  137. if item["name"] == itemName then
  138. return true
  139. end
  140. end
  141. return false
  142. end
  143.  
  144. -- Funktion zum Abrufen der Crafting-CPUs
  145. function getCraftingCPUs()
  146. local cpus = meBridge.getCraftingCPUs()
  147. local freeCPUs = {}
  148. local busyCPUs = {}
  149. for i, cpu in ipairs(cpus) do
  150. local cpuID = "CPU #" .. i
  151. if cpu.isBusy == false then
  152. table.insert(freeCPUs, {id = cpuID, storage = cpu.storage})
  153. else
  154. table.insert(busyCPUs, {id = cpuID, storage = cpu.storage})
  155. end
  156. end
  157. return freeCPUs, busyCPUs
  158. end
  159.  
  160. -- Funktion zum Starten des Autocrafting
  161. function startAutocrafting(itemName, count)
  162. print("Starte Autocrafting für Item: " .. itemName .. " Menge: " .. count)
  163. local craftedItem = {name = itemName, count = count}
  164. local success, err = pcall(function()
  165. meBridge.craftItem(craftedItem)
  166. end)
  167. if not success then
  168. print("Fehler beim Starten des Autocraftings: " .. err)
  169. end
  170. end
  171.  
  172. -- Funktion zum Überprüfen, ob ein Item bereits gecraftet wird
  173. function isItemBeingCrafted(itemName)
  174. local success, isCrafting = pcall(function()
  175. local craftingItem = {name = itemName}
  176. return meBridge.isItemCrafting(craftingItem)
  177. end)
  178. if success then
  179. print("Crafting-Status für " .. itemName .. ": " .. tostring(isCrafting))
  180. return isCrafting
  181. else
  182. print("Fehler beim Überprüfen des Crafting-Status für " .. itemName)
  183. return false
  184. end
  185. end
  186.  
  187. -- Funktion zum Exportieren von Items in die Kiste
  188. function exportItemToChest(itemName, count)
  189. print("Exportiere Item:", itemName, "Menge:", count)
  190. local success, err = pcall(function()
  191. meBridge.exportItem({name = itemName, count = count}, "back")
  192. end)
  193. if not success then
  194. print("Fehler beim Exportieren des Items: " .. err)
  195. end
  196. end
  197.  
  198. -- Funktion zum Überprüfen der Anfragen im Colony Integrator
  199. function checkRequests()
  200. local requests
  201. local status, err = pcall(function()
  202. requests = colonyIntegrator.getRequests()
  203. end)
  204. if not status then
  205. print("Fehler beim Abrufen der Anfragen: " .. err)
  206. return {}
  207. end
  208. if not requests then
  209. print("Keine Anfragen gefunden oder Fehler beim Abrufen der Anfragen.")
  210. return {}
  211. end
  212. return requests
  213. end
  214.  
  215. -- Funktion zum Überprüfen der Anfragen und Verfügbarkeit der Items
  216. function displayRequestsAndAvailability()
  217. monitor.clear()
  218. monitor.setCursorPos(1, 1)
  219. monitor.write("Warten auf neue Abfrage")
  220. sleep(2)
  221. monitor.clear()
  222. monitor.setCursorPos(1, 1)
  223.  
  224. -- Hilfstabelle aktualisieren
  225. local itemsTableNew = getItemsTable()
  226. saveItemsTableToFile("/temp/items_table.csv", itemsTableNew)
  227.  
  228. local loadedItemsTable = loadItemsTableFromFile("/temp/items_table.csv")
  229.  
  230. local requestsList = checkRequests()
  231. if #requestsList == 0 then
  232. monitor.write("Keine offenen Anfragen")
  233. else
  234. local lines = {}
  235. local freeCPUs, busyCPUs = getCraftingCPUs()
  236. for _, request in ipairs(requestsList) do
  237. local cleanedItemName = cleanItemName(request.name)
  238. if not isExcludedItem(cleanedItemName) then
  239. print("Überprüfe Item: " .. cleanedItemName)
  240. local itemDataHelp = loadedItemsTable[cleanedItemName]
  241. local itemDataME = getItemsTable()
  242.  
  243. local fullItemName = itemDataHelp and itemDataHelp.fullName
  244. local available = false
  245. if fullItemName then
  246. local meItemData = itemDataME[cleanedItemName]
  247. available = meItemData and meItemData.amount >= request.count
  248. end
  249.  
  250. local color
  251. -- print(fullItemName)
  252. if available then
  253. color = colors.green
  254. exportItemToChest(fullItemName, request.count)
  255. elseif isAutocraftable(fullItemName) then
  256. print("Autocrafting für " .. fullItemName)
  257. local neededAmount = request.count - (meItemData and meItemData.amount or 0)
  258. if neededAmount > 0 and #freeCPUs > 0 and not isItemBeingCrafted(fullItemName) then
  259. print("meItemName: " .. (fullItemName or "nil") .. ", count: " .. neededAmount)
  260. startAutocrafting(fullItemName or cleanedItemName, neededAmount)
  261. color = colors.blue
  262. else
  263. color = colors.red
  264. end
  265. else
  266. color = colors.red
  267. end
  268. table.insert(lines, {text = cleanedItemName .. " (" .. request.count .. ")", color = color})
  269. -- else print("Item " .. cleanedItemName .. " is excluded")
  270. end
  271. end
  272.  
  273. -- Aktualisiere die Listen der freien und beschäftigten Autocrafting-CPUs
  274. freeCPUs, busyCPUs = getCraftingCPUs()
  275. table.insert(lines, {text = "Freie Autocrafting-CPUs:", color = colors.yellow})
  276. for _, cpu in ipairs(freeCPUs) do
  277. table.insert(lines, {text = "CPU " .. cpu.id .. " (Speicher: " .. cpu.storage .. ")", color = colors.white})
  278. end
  279. table.insert(lines, {text = "Beschäftigte Autocrafting-CPUs:", color = colors.yellow})
  280. for _, cpu in ipairs(busyCPUs) do
  281. table.insert(lines, {text = "CPU " .. cpu.id .. " (Speicher: " .. cpu.storage .. ")", color = colors.white})
  282. end
  283.  
  284. for _, line in ipairs(lines) do
  285. monitor.setTextColor(line.color)
  286. monitor.write(line.text)
  287. monitor.setCursorPos(1, select(2, monitor.getCursorPos()) + 1)
  288. end
  289. monitor.setTextColor(colors.white)
  290. end
  291. end
  292.  
  293. -- Countdown-Funktion
  294. function countdown(seconds)
  295. local width, height = monitor.getSize()
  296. local y = height
  297. for i = seconds, 1, -1 do
  298. monitor.setCursorPos(1, y)
  299. monitor.clearLine()
  300. monitor.setTextColor(colors.blue)
  301. monitor.write("Nächste Abfrage in: " .. i .. " Sekunden")
  302. monitor.setTextColor(colors.white)
  303. sleep(1)
  304. end
  305. end
  306.  
  307. -- Hauptprogramm
  308. while true do
  309. displayRequestsAndAvailability()
  310. countdown(60) -- Countdown bis zur nächsten Überprüfung
  311. end
Advertisement
Add Comment
Please, Sign In to add comment