gelatine87

Energized Orb Autocrafting

Jun 28th, 2025 (edited)
6,735
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 13.60 KB | Gaming | 0 0
  1. --[[
  2. ================================================================================
  3. Powah Energizing Orb Automation (V4 OOP Edition)
  4. ================================================================================
  5.  
  6. DESCRIPTION:
  7. This script fully automates the crafting process for Powah's Energizing Orbs
  8. using Applied Energistics 2 (AE2) or Refined Storage. It supports multiple orbs
  9. (auto-scaling), auto-recovery on failures, and strict recipe validation.
  10. Refactored into an Object-Oriented design.
  11.  
  12. HARDWARE SETUP:
  13. 1. Place an Advanced Computer.
  14. 2. Connect an ME Pattern Provider (or similar) to a Chest/Buffer.
  15. 3. Place the Chest directly next to the Computer (e.g., on the left side).
  16. 4. Connect one or multiple Powah Energizing Orbs to the Computer using
  17.    Networking Cables and Wired Modems.
  18. 5. (Optional but recommended) Set the Pattern Provider to "Blocking Mode".
  19.    This is only strictly necessary for extremely large bulk orders where
  20.    the input chest doesn't have enough slots to hold all ingredients at once.
  21. 6. Use an Import Bus on the Energizing Orbs to extract the finished items
  22.    back into your ME system.
  23.  
  24. HOW TO ADD RECIPES:
  25. Create or edit the `rezepte.json` file in the same directory.
  26. Add your recipes in the following format:
  27. [
  28.     {
  29.         "name": "Nitro Crystal",
  30.         "ingredients": {
  31.             "minecraft:redstone_block": 2,
  32.             "powah:blazing_crystal_block": 1,
  33.             "minecraft:nether_star": 1
  34.         }
  35.     }
  36. ]
  37. - "name": The display name on the dashboard.
  38. - "ingredients": The exact registry names of the items and their amounts.
  39. Note: The Energizing Orb can hold a maximum of 6 items!
  40.  
  41. HOTKEYS:
  42. - Press 'R' while the script is running to hot-reload recipes from JSON.
  43. ================================================================================
  44. ]] --
  45.  
  46. -- ==============================================
  47. -- Class: Dashboard
  48. -- ==============================================
  49. local Dashboard = {}
  50. Dashboard.__index = Dashboard
  51.  
  52. function Dashboard:new()
  53.     local instance = setmetatable({}, self)
  54.     instance.statusMsg = "Starting System..."
  55.     instance.errorMsg = ""
  56.     instance.lastCraft = "-"
  57.     instance.activeJobs = {}
  58.     instance.recipeCount = 0
  59.     return instance
  60. end
  61.  
  62. function Dashboard:updateJobs(jobs)
  63.     self.activeJobs = jobs
  64. end
  65.  
  66. function Dashboard:setRecipeCount(count)
  67.     self.recipeCount = count
  68. end
  69.  
  70. function Dashboard:setStatus(msg)
  71.     self.statusMsg = msg
  72.     self:draw()
  73. end
  74.  
  75. function Dashboard:setError(msg)
  76.     self.errorMsg = msg
  77.     self:draw()
  78. end
  79.  
  80. function Dashboard:setLastCraft(recipeName)
  81.     self.lastCraft = recipeName
  82. end
  83.  
  84. function Dashboard:draw()
  85.     term.clear()
  86.     term.setCursorPos(1, 1)
  87.     print("===================================")
  88.     print("   Powah System v4.0 (OOP)")
  89.     print("===================================")
  90.  
  91.     local orbCount = #{ peripheral.find("powah:energizing_orb") }
  92.     print("Connected Orbs: " .. orbCount)
  93.     print("Status:      " .. self.statusMsg)
  94.     print("Last Job:    " .. self.lastCraft)
  95.     if self.recipeCount > 0 then
  96.         print("Recipes:     " .. self.recipeCount)
  97.     end
  98.     print("-----------------------------------")
  99.  
  100.     local jobsFound = false
  101.     if self.activeJobs then
  102.         for name, job in pairs(self.activeJobs) do
  103.             print("-> " .. job.recipeName .. " (" .. name .. ")")
  104.             jobsFound = true
  105.         end
  106.     end
  107.     if not jobsFound then
  108.         print("No active crafting processes.")
  109.     end
  110.     print("-----------------------------------")
  111.  
  112.     if self.errorMsg ~= "" then
  113.         print("ERROR: " .. self.errorMsg)
  114.     else
  115.         print("[Press 'R' to reload JSON]")
  116.     end
  117. end
  118.  
  119. -- ==============================================
  120. -- Class: RecipeManager
  121. -- ==============================================
  122. local RecipeManager = {}
  123. RecipeManager.__index = RecipeManager
  124.  
  125. function RecipeManager:new(filename, dashboard)
  126.     local instance = setmetatable({}, self)
  127.     instance.filename = filename
  128.     instance.dashboard = dashboard
  129.     instance.recipes = {}
  130.     return instance
  131. end
  132.  
  133. function RecipeManager:load()
  134.     if not fs.exists(self.filename) then
  135.         self.dashboard:setError(self.filename .. " not found!")
  136.         return false
  137.     end
  138.  
  139.     local file = fs.open(self.filename, "r")
  140.     local content = file.readAll()
  141.     file.close()
  142.  
  143.     local rawRecipes = textutils.unserializeJSON(content)
  144.     if not rawRecipes then
  145.         self.dashboard:setError("Syntax Error in " .. self.filename .. "!")
  146.         return false
  147.     end
  148.  
  149.     local validated = self:validate(rawRecipes)
  150.     if validated then
  151.         self.recipes = validated
  152.         self.dashboard.errorMsg = ""
  153.         self.dashboard:setRecipeCount(#self.recipes)
  154.         return true
  155.     end
  156.     return false
  157. end
  158.  
  159. function RecipeManager:validate(rawRecipes)
  160.     local validRecipes = {}
  161.     for i, recipe in ipairs(rawRecipes) do
  162.         if type(recipe.name) ~= "string" or recipe.name == "" then
  163.             self.dashboard:setError("Recipe #" .. i .. ": Missing name!")
  164.             return nil
  165.         end
  166.         if type(recipe.ingredients) ~= "table" then
  167.             self.dashboard:setError("Recipe '" .. recipe.name .. "': Missing ingredients!")
  168.             return nil
  169.         end
  170.  
  171.         local totalItems = 0
  172.         for item, count in pairs(recipe.ingredients) do
  173.             if type(count) ~= "number" or count <= 0 then
  174.                 self.dashboard:setError("Recipe '" .. recipe.name .. "': Ingredient " .. item .. " has amount <= 0")
  175.                 return nil
  176.             end
  177.             totalItems = totalItems + count
  178.         end
  179.  
  180.         if totalItems > 6 then
  181.             self.dashboard:setError("Recipe '" .. recipe.name .. "': Too large! Max 6 items!")
  182.             return nil
  183.         end
  184.  
  185.         table.insert(validRecipes, recipe)
  186.     end
  187.     return validRecipes
  188. end
  189.  
  190. function RecipeManager:countItem(itemName, chestItems)
  191.     local count = 0
  192.     for _, item in pairs(chestItems) do
  193.         if item.name == itemName then
  194.             count = count + item.count
  195.         end
  196.     end
  197.     return count
  198. end
  199.  
  200. function RecipeManager:isRecipeComplete(recipe, chestItems)
  201.     for itemName, requiredAmount in pairs(recipe.ingredients) do
  202.         if self:countItem(itemName, chestItems) < requiredAmount then
  203.             return false
  204.         end
  205.     end
  206.     return true
  207. end
  208.  
  209. function RecipeManager:findReadyRecipe(chest)
  210.     local chestItems = chest:list()
  211.     if not next(chestItems) then return nil end
  212.  
  213.     for _, recipe in ipairs(self.recipes) do
  214.         if self:isRecipeComplete(recipe, chestItems) then
  215.             return recipe
  216.         end
  217.     end
  218.     return nil
  219. end
  220.  
  221. -- ==============================================
  222. -- Class: InventoryComponent (Base for Chest & Orb)
  223. -- ==============================================
  224. local InventoryComponent = {}
  225. InventoryComponent.__index = InventoryComponent
  226.  
  227. function InventoryComponent:new(name)
  228.     local instance = setmetatable({}, self)
  229.     instance.name = name
  230.     return instance
  231. end
  232.  
  233. function InventoryComponent:isPresent()
  234.     return peripheral.isPresent(self.name)
  235. end
  236.  
  237. function InventoryComponent:getPeripheral()
  238.     if self:isPresent() then
  239.         return peripheral.wrap(self.name)
  240.     end
  241.     return nil
  242. end
  243.  
  244. function InventoryComponent:list()
  245.     local p = self:getPeripheral()
  246.     if p then return p.list() end
  247.     return {}
  248. end
  249.  
  250. function InventoryComponent:isEmpty()
  251.     return not next(self:list())
  252. end
  253.  
  254. -- ==============================================
  255. -- Class: Orb
  256. -- Inherits from InventoryComponent
  257. -- ==============================================
  258. local Orb = setmetatable({}, { __index = InventoryComponent })
  259. Orb.__index = Orb
  260.  
  261. function Orb:new(name)
  262.     local instance = InventoryComponent:new(name)
  263.     setmetatable(instance, self)
  264.     return instance
  265. end
  266.  
  267. function Orb:recover(targetChestName)
  268.     local p = self:getPeripheral()
  269.     if not p then return end
  270.     for slot, item in pairs(p.list()) do
  271.         p.pushItems(targetChestName, slot)
  272.     end
  273. end
  274.  
  275. -- ==============================================
  276. -- Class: Chest
  277. -- Inherits from InventoryComponent
  278. -- ==============================================
  279. local Chest = setmetatable({}, { __index = InventoryComponent })
  280. Chest.__index = Chest
  281.  
  282. function Chest:new(name)
  283.     local instance = InventoryComponent:new(name)
  284.     setmetatable(instance, self)
  285.     return instance
  286. end
  287.  
  288. function Chest:transferRecipe(recipe, orbName)
  289.     local itemsToTransfer = {}
  290.     for k, v in pairs(recipe.ingredients) do
  291.         itemsToTransfer[k] = v
  292.     end
  293.  
  294.     local p = self:getPeripheral()
  295.     if not p then return false end
  296.  
  297.     local timeoutStart = os.epoch("utc")
  298.  
  299.     while next(itemsToTransfer) ~= nil do
  300.         if not self:isPresent() then return false end
  301.  
  302.         for slot, item in pairs(self:list()) do
  303.             if itemsToTransfer[item.name] ~= nil then
  304.                 local needed = itemsToTransfer[item.name]
  305.                 local transferred = p.pushItems(orbName, slot, needed)
  306.  
  307.                 local itemsLeft = needed - transferred
  308.                 if itemsLeft <= 0 then
  309.                     itemsToTransfer[item.name] = nil
  310.                 else
  311.                     itemsToTransfer[item.name] = itemsLeft
  312.                 end
  313.             end
  314.         end
  315.  
  316.         if (os.epoch("utc") - timeoutStart) > 5000 then
  317.             return false
  318.         end
  319.         os.sleep(0.1)
  320.     end
  321.     return true
  322. end
  323.  
  324. -- ==============================================
  325. -- Class: PowahSystem
  326. -- ==============================================
  327. local PowahSystem = {}
  328. PowahSystem.__index = PowahSystem
  329.  
  330. function PowahSystem:new(chestName, recipeFile)
  331.     local instance = setmetatable({}, self)
  332.     instance.chest = Chest:new(chestName)
  333.     instance.dashboard = Dashboard:new()
  334.     instance.recipeManager = RecipeManager:new(recipeFile, instance.dashboard)
  335.     instance.activeJobs = {}
  336.     return instance
  337. end
  338.  
  339. function PowahSystem:getFreeOrb()
  340.     local allOrbsNames = { peripheral.find("powah:energizing_orb") }
  341.     for _, orbPeripheral in ipairs(allOrbsNames) do
  342.         local name = peripheral.getName(orbPeripheral)
  343.         if not self.activeJobs[name] then
  344.             local orb = Orb:new(name)
  345.             if orb:isEmpty() then
  346.                 return orb
  347.             end
  348.         end
  349.     end
  350.     return nil
  351. end
  352.  
  353. function PowahSystem:checkActiveJobs()
  354.     for orbName, job in pairs(self.activeJobs) do
  355.         local orb = Orb:new(orbName)
  356.         if not orb:isPresent() then
  357.             self.activeJobs[orbName] = nil
  358.         else
  359.             if orb:isEmpty() then
  360.                 self.dashboard:setLastCraft(job.recipeName)
  361.                 self.activeJobs[orbName] = nil
  362.                 self.dashboard:draw()
  363.             else
  364.                 if (os.epoch("utc") - job.startTime) > 60000 then
  365.                     self.dashboard:setError("Timeout in " .. orbName .. ". Recovering...")
  366.                     orb:recover(self.chest.name)
  367.                     self.activeJobs[orbName] = nil
  368.                     os.sleep(1)
  369.                     self.dashboard:setError("")
  370.                 end
  371.             end
  372.         end
  373.     end
  374. end
  375.  
  376. function PowahSystem:process()
  377.     if not self.chest:isPresent() then
  378.         self.dashboard:setError("Chest missing!")
  379.         os.sleep(2)
  380.         return
  381.     end
  382.  
  383.     if self.dashboard.errorMsg == "Chest missing!" or self.dashboard.errorMsg == "No Orb found!" then
  384.         self.dashboard:setError("")
  385.     end
  386.  
  387.     self.dashboard:updateJobs(self.activeJobs)
  388.  
  389.     self:checkActiveJobs()
  390.  
  391.     local freeOrb = self:getFreeOrb()
  392.     if freeOrb then
  393.         local readyRecipe = self.recipeManager:findReadyRecipe(self.chest)
  394.         if readyRecipe then
  395.             self.dashboard:setStatus("Filling " .. freeOrb.name)
  396.             local success = self.chest:transferRecipe(readyRecipe, freeOrb.name)
  397.  
  398.             if success then
  399.                 self.activeJobs[freeOrb.name] = {
  400.                     startTime = os.epoch("utc"),
  401.                     recipeName = readyRecipe.name
  402.                 }
  403.                 self.dashboard:draw()
  404.             else
  405.                 self.dashboard:setError("Transfer Error! Starting Recovery...")
  406.                 freeOrb:recover(self.chest.name)
  407.                 os.sleep(1)
  408.             end
  409.         else
  410.             self.dashboard:setStatus("Waiting for items...")
  411.         end
  412.     else
  413.         local allOrbs = { peripheral.find("powah:energizing_orb") }
  414.         if #allOrbs > 0 then
  415.             self.dashboard:setStatus("All Orbs are busy...")
  416.         else
  417.             self.dashboard:setError("No Orb found!")
  418.         end
  419.     end
  420. end
  421.  
  422. function PowahSystem:mainLoop()
  423.     while true do
  424.         self:process()
  425.         os.sleep(0.1)
  426.     end
  427. end
  428.  
  429. function PowahSystem:keyListener()
  430.     while true do
  431.         local event, key = os.pullEvent("key")
  432.         if key == keys.r then
  433.             self.dashboard:setStatus("Reloading recipes...")
  434.             self.recipeManager:load()
  435.             os.sleep(0.5)
  436.             self.dashboard:setStatus("Waiting for items...")
  437.         end
  438.     end
  439. end
  440.  
  441. function PowahSystem:start()
  442.     if self.recipeManager:load() then
  443.         parallel.waitForAll(
  444.             function() self:mainLoop() end,
  445.             function() self:keyListener() end
  446.         )
  447.     end
  448. end
  449.  
  450. -- ==============================================
  451. -- Execution Setup
  452. -- ==============================================
  453. -- You can change "left" to whatever side or network name your chest is.
  454. local system = PowahSystem:new("left", "rezepte.json")
  455. system:start()
  456.  
Comments
  • Vinyuxor
    239 days
    Comment was deleted
  • User was banned
Add Comment
Please, Sign In to add comment