From 3d1ee74f0fc5694eb645065c6eae31e2d8b56638 Mon Sep 17 00:00:00 2001 From: kepler155c Date: Thu, 29 Mar 2018 07:04:56 -0400 Subject: [PATCH] plethora import/export support + introspection module --- apis/inventoryAdapter.lua | 1 + apis/meAdapter18.lua | 100 ++++++++++++++++++++++++++++++++++++++ apis/refinedAdapter.lua | 6 +-- 3 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 apis/meAdapter18.lua diff --git a/apis/inventoryAdapter.lua b/apis/inventoryAdapter.lua index c57ff49..7aa9afd 100644 --- a/apis/inventoryAdapter.lua +++ b/apis/inventoryAdapter.lua @@ -3,6 +3,7 @@ local Adapter = { } function Adapter.wrap(args) local adapters = { 'refinedAdapter', + 'meAdapter18', 'chestAdapter18', -- adapters for version 1.7 diff --git a/apis/meAdapter18.lua b/apis/meAdapter18.lua new file mode 100644 index 0000000..858a5b6 --- /dev/null +++ b/apis/meAdapter18.lua @@ -0,0 +1,100 @@ +local class = require('class') +local RSAdapter = require('refinedAdapter') +local Peripheral = require('peripheral') +local Util = require('util') + +local MEAdapter = class(RSAdapter) + +function MEAdapter:init(args) + local defaults = { + name = 'appliedEnergistics', + jobList = { }, + } + Util.merge(self, defaults) + Util.merge(self, args) + + local controller + if not self.side then + controller = Peripheral.getByMethod('getCraftingCPUs') + else + controller = Peripheral.getBySide(self.side) + if controller and not controller.getCraftingCPUs then + controller = nil + end + end + + if controller then + Util.merge(self, controller) + end +end + +function MEAdapter:isValid() + return not not self.getCraftingCPUs +end + +function MEAdapter:isCPUAvailable() + local cpus = self.getCraftingCPUs() or { } + local available = false + + for cpu,v in pairs(cpus) do + if not v.busy then + available = true + elseif not self.jobList[cpu] then -- something else is crafting something (don't know what) + return false -- return false since we are in an unknown state + end + end + return available +end + +function MEAdapter:craft(item, qty) + if not self:isCPUAvailable() then + return false + end + + local detail = self.findItem(item) + if detail and detail.craft then + + local cpus = self.getCraftingCPUs() or { } + for cpu,v in pairs(cpus) do + if not v.busy then + self.requestCrafting({ + id = item.name, + dmg = item.damage, + nbt_hash = item.nbtHash, + }, + count or 1, + v.name -- CPUs must be named ! use anvil + ) + + os.sleep(0) -- needed ? + cpus = self.getCraftingCPUs() or { } + + if cpus[cpu].busy then + self.jobList[cpu] = { + name = item.name, + damage = item.damage, + nbtHash = item.nbtHash, + count = count, + } + return true + end + break -- only need to try the first available cpu + end + end + return false + end +end + +function MEAdapter:isCrafting(item) + for _,task in pairs(self.getCraftingTasks()) do + local output = task.getPattern().outputs[1] + if output.name == item.name and + output.damage == item.damage and + output.nbtHash == item.nbtHash then + return true + end + end + return false +end + +return MEAdapter diff --git a/apis/refinedAdapter.lua b/apis/refinedAdapter.lua index b80249d..6c00c53 100644 --- a/apis/refinedAdapter.lua +++ b/apis/refinedAdapter.lua @@ -14,10 +14,10 @@ function RefinedAdapter:init(args) local controller if not self.side then - controller = Peripheral.getByMethod('listAvailableItems') + controller = Peripheral.getByMethod('getCraftingTasks') else controller = Peripheral.getBySide(self.side) - if controller and not controller.listAvailableItems then + if controller and not controller.getCraftingTasks then controller = nil end end @@ -28,7 +28,7 @@ function RefinedAdapter:init(args) end function RefinedAdapter:isValid() - return not not self.listAvailableItems + return not not self.getCraftingTasks end function RefinedAdapter:getItemDetails(item)