Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- ================================================================================
- Powah Energizing Orb Automation (V4 OOP Edition)
- ================================================================================
- DESCRIPTION:
- This script fully automates the crafting process for Powah's Energizing Orbs
- using Applied Energistics 2 (AE2) or Refined Storage. It supports multiple orbs
- (auto-scaling), auto-recovery on failures, and strict recipe validation.
- Refactored into an Object-Oriented design.
- HARDWARE SETUP:
- 1. Place an Advanced Computer.
- 2. Connect an ME Pattern Provider (or similar) to a Chest/Buffer.
- 3. Place the Chest directly next to the Computer (e.g., on the left side).
- 4. Connect one or multiple Powah Energizing Orbs to the Computer using
- Networking Cables and Wired Modems.
- 5. (Optional but recommended) Set the Pattern Provider to "Blocking Mode".
- This is only strictly necessary for extremely large bulk orders where
- the input chest doesn't have enough slots to hold all ingredients at once.
- 6. Use an Import Bus on the Energizing Orbs to extract the finished items
- back into your ME system.
- HOW TO ADD RECIPES:
- Create or edit the `rezepte.json` file in the same directory.
- Add your recipes in the following format:
- [
- {
- "name": "Nitro Crystal",
- "ingredients": {
- "minecraft:redstone_block": 2,
- "powah:blazing_crystal_block": 1,
- "minecraft:nether_star": 1
- }
- }
- ]
- - "name": The display name on the dashboard.
- - "ingredients": The exact registry names of the items and their amounts.
- Note: The Energizing Orb can hold a maximum of 6 items!
- HOTKEYS:
- - Press 'R' while the script is running to hot-reload recipes from JSON.
- ================================================================================
- ]] --
- -- ==============================================
- -- Class: Dashboard
- -- ==============================================
- local Dashboard = {}
- Dashboard.__index = Dashboard
- function Dashboard:new()
- local instance = setmetatable({}, self)
- instance.statusMsg = "Starting System..."
- instance.errorMsg = ""
- instance.lastCraft = "-"
- instance.activeJobs = {}
- instance.recipeCount = 0
- return instance
- end
- function Dashboard:updateJobs(jobs)
- self.activeJobs = jobs
- end
- function Dashboard:setRecipeCount(count)
- self.recipeCount = count
- end
- function Dashboard:setStatus(msg)
- self.statusMsg = msg
- self:draw()
- end
- function Dashboard:setError(msg)
- self.errorMsg = msg
- self:draw()
- end
- function Dashboard:setLastCraft(recipeName)
- self.lastCraft = recipeName
- end
- function Dashboard:draw()
- term.clear()
- term.setCursorPos(1, 1)
- print("===================================")
- print(" Powah System v4.0 (OOP)")
- print("===================================")
- local orbCount = #{ peripheral.find("powah:energizing_orb") }
- print("Connected Orbs: " .. orbCount)
- print("Status: " .. self.statusMsg)
- print("Last Job: " .. self.lastCraft)
- if self.recipeCount > 0 then
- print("Recipes: " .. self.recipeCount)
- end
- print("-----------------------------------")
- local jobsFound = false
- if self.activeJobs then
- for name, job in pairs(self.activeJobs) do
- print("-> " .. job.recipeName .. " (" .. name .. ")")
- jobsFound = true
- end
- end
- if not jobsFound then
- print("No active crafting processes.")
- end
- print("-----------------------------------")
- if self.errorMsg ~= "" then
- print("ERROR: " .. self.errorMsg)
- else
- print("[Press 'R' to reload JSON]")
- end
- end
- -- ==============================================
- -- Class: RecipeManager
- -- ==============================================
- local RecipeManager = {}
- RecipeManager.__index = RecipeManager
- function RecipeManager:new(filename, dashboard)
- local instance = setmetatable({}, self)
- instance.filename = filename
- instance.dashboard = dashboard
- instance.recipes = {}
- return instance
- end
- function RecipeManager:load()
- if not fs.exists(self.filename) then
- self.dashboard:setError(self.filename .. " not found!")
- return false
- end
- local file = fs.open(self.filename, "r")
- local content = file.readAll()
- file.close()
- local rawRecipes = textutils.unserializeJSON(content)
- if not rawRecipes then
- self.dashboard:setError("Syntax Error in " .. self.filename .. "!")
- return false
- end
- local validated = self:validate(rawRecipes)
- if validated then
- self.recipes = validated
- self.dashboard.errorMsg = ""
- self.dashboard:setRecipeCount(#self.recipes)
- return true
- end
- return false
- end
- function RecipeManager:validate(rawRecipes)
- local validRecipes = {}
- for i, recipe in ipairs(rawRecipes) do
- if type(recipe.name) ~= "string" or recipe.name == "" then
- self.dashboard:setError("Recipe #" .. i .. ": Missing name!")
- return nil
- end
- if type(recipe.ingredients) ~= "table" then
- self.dashboard:setError("Recipe '" .. recipe.name .. "': Missing ingredients!")
- return nil
- end
- local totalItems = 0
- for item, count in pairs(recipe.ingredients) do
- if type(count) ~= "number" or count <= 0 then
- self.dashboard:setError("Recipe '" .. recipe.name .. "': Ingredient " .. item .. " has amount <= 0")
- return nil
- end
- totalItems = totalItems + count
- end
- if totalItems > 6 then
- self.dashboard:setError("Recipe '" .. recipe.name .. "': Too large! Max 6 items!")
- return nil
- end
- table.insert(validRecipes, recipe)
- end
- return validRecipes
- end
- function RecipeManager:countItem(itemName, chestItems)
- local count = 0
- for _, item in pairs(chestItems) do
- if item.name == itemName then
- count = count + item.count
- end
- end
- return count
- end
- function RecipeManager:isRecipeComplete(recipe, chestItems)
- for itemName, requiredAmount in pairs(recipe.ingredients) do
- if self:countItem(itemName, chestItems) < requiredAmount then
- return false
- end
- end
- return true
- end
- function RecipeManager:findReadyRecipe(chest)
- local chestItems = chest:list()
- if not next(chestItems) then return nil end
- for _, recipe in ipairs(self.recipes) do
- if self:isRecipeComplete(recipe, chestItems) then
- return recipe
- end
- end
- return nil
- end
- -- ==============================================
- -- Class: InventoryComponent (Base for Chest & Orb)
- -- ==============================================
- local InventoryComponent = {}
- InventoryComponent.__index = InventoryComponent
- function InventoryComponent:new(name)
- local instance = setmetatable({}, self)
- instance.name = name
- return instance
- end
- function InventoryComponent:isPresent()
- return peripheral.isPresent(self.name)
- end
- function InventoryComponent:getPeripheral()
- if self:isPresent() then
- return peripheral.wrap(self.name)
- end
- return nil
- end
- function InventoryComponent:list()
- local p = self:getPeripheral()
- if p then return p.list() end
- return {}
- end
- function InventoryComponent:isEmpty()
- return not next(self:list())
- end
- -- ==============================================
- -- Class: Orb
- -- Inherits from InventoryComponent
- -- ==============================================
- local Orb = setmetatable({}, { __index = InventoryComponent })
- Orb.__index = Orb
- function Orb:new(name)
- local instance = InventoryComponent:new(name)
- setmetatable(instance, self)
- return instance
- end
- function Orb:recover(targetChestName)
- local p = self:getPeripheral()
- if not p then return end
- for slot, item in pairs(p.list()) do
- p.pushItems(targetChestName, slot)
- end
- end
- -- ==============================================
- -- Class: Chest
- -- Inherits from InventoryComponent
- -- ==============================================
- local Chest = setmetatable({}, { __index = InventoryComponent })
- Chest.__index = Chest
- function Chest:new(name)
- local instance = InventoryComponent:new(name)
- setmetatable(instance, self)
- return instance
- end
- function Chest:transferRecipe(recipe, orbName)
- local itemsToTransfer = {}
- for k, v in pairs(recipe.ingredients) do
- itemsToTransfer[k] = v
- end
- local p = self:getPeripheral()
- if not p then return false end
- local timeoutStart = os.epoch("utc")
- while next(itemsToTransfer) ~= nil do
- if not self:isPresent() then return false end
- for slot, item in pairs(self:list()) do
- if itemsToTransfer[item.name] ~= nil then
- local needed = itemsToTransfer[item.name]
- local transferred = p.pushItems(orbName, slot, needed)
- local itemsLeft = needed - transferred
- if itemsLeft <= 0 then
- itemsToTransfer[item.name] = nil
- else
- itemsToTransfer[item.name] = itemsLeft
- end
- end
- end
- if (os.epoch("utc") - timeoutStart) > 5000 then
- return false
- end
- os.sleep(0.1)
- end
- return true
- end
- -- ==============================================
- -- Class: PowahSystem
- -- ==============================================
- local PowahSystem = {}
- PowahSystem.__index = PowahSystem
- function PowahSystem:new(chestName, recipeFile)
- local instance = setmetatable({}, self)
- instance.chest = Chest:new(chestName)
- instance.dashboard = Dashboard:new()
- instance.recipeManager = RecipeManager:new(recipeFile, instance.dashboard)
- instance.activeJobs = {}
- return instance
- end
- function PowahSystem:getFreeOrb()
- local allOrbsNames = { peripheral.find("powah:energizing_orb") }
- for _, orbPeripheral in ipairs(allOrbsNames) do
- local name = peripheral.getName(orbPeripheral)
- if not self.activeJobs[name] then
- local orb = Orb:new(name)
- if orb:isEmpty() then
- return orb
- end
- end
- end
- return nil
- end
- function PowahSystem:checkActiveJobs()
- for orbName, job in pairs(self.activeJobs) do
- local orb = Orb:new(orbName)
- if not orb:isPresent() then
- self.activeJobs[orbName] = nil
- else
- if orb:isEmpty() then
- self.dashboard:setLastCraft(job.recipeName)
- self.activeJobs[orbName] = nil
- self.dashboard:draw()
- else
- if (os.epoch("utc") - job.startTime) > 60000 then
- self.dashboard:setError("Timeout in " .. orbName .. ". Recovering...")
- orb:recover(self.chest.name)
- self.activeJobs[orbName] = nil
- os.sleep(1)
- self.dashboard:setError("")
- end
- end
- end
- end
- end
- function PowahSystem:process()
- if not self.chest:isPresent() then
- self.dashboard:setError("Chest missing!")
- os.sleep(2)
- return
- end
- if self.dashboard.errorMsg == "Chest missing!" or self.dashboard.errorMsg == "No Orb found!" then
- self.dashboard:setError("")
- end
- self.dashboard:updateJobs(self.activeJobs)
- self:checkActiveJobs()
- local freeOrb = self:getFreeOrb()
- if freeOrb then
- local readyRecipe = self.recipeManager:findReadyRecipe(self.chest)
- if readyRecipe then
- self.dashboard:setStatus("Filling " .. freeOrb.name)
- local success = self.chest:transferRecipe(readyRecipe, freeOrb.name)
- if success then
- self.activeJobs[freeOrb.name] = {
- startTime = os.epoch("utc"),
- recipeName = readyRecipe.name
- }
- self.dashboard:draw()
- else
- self.dashboard:setError("Transfer Error! Starting Recovery...")
- freeOrb:recover(self.chest.name)
- os.sleep(1)
- end
- else
- self.dashboard:setStatus("Waiting for items...")
- end
- else
- local allOrbs = { peripheral.find("powah:energizing_orb") }
- if #allOrbs > 0 then
- self.dashboard:setStatus("All Orbs are busy...")
- else
- self.dashboard:setError("No Orb found!")
- end
- end
- end
- function PowahSystem:mainLoop()
- while true do
- self:process()
- os.sleep(0.1)
- end
- end
- function PowahSystem:keyListener()
- while true do
- local event, key = os.pullEvent("key")
- if key == keys.r then
- self.dashboard:setStatus("Reloading recipes...")
- self.recipeManager:load()
- os.sleep(0.5)
- self.dashboard:setStatus("Waiting for items...")
- end
- end
- end
- function PowahSystem:start()
- if self.recipeManager:load() then
- parallel.waitForAll(
- function() self:mainLoop() end,
- function() self:keyListener() end
- )
- end
- end
- -- ==============================================
- -- Execution Setup
- -- ==============================================
- -- You can change "left" to whatever side or network name your chest is.
- local system = PowahSystem:new("left", "rezepte.json")
- system:start()