Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local minecoloniesBridgeName = " [Minecolonies Bridge]"
- local mainNetworkBridgeName = " [Main Network Bridge]"
- local mainToMinecoloniesDirection = "bottom"
- local minecoloniesToMainDirection = "top"
- local loopInterval = 5
- local minecoloniesMaxAllowedItemCount = 256
- local minecoloniesNetworkCleanupIterations = 10
- local minecoloniesBridge = nil
- local mainNetworkBridge = nil
- local colonyIntegrator = nil
- local next = next
- local function InitalizeComponents()
- local sucessfulInitialization = true
- repeat
- if sucessfulInitialization == false then
- sleep(0.1)
- term.clear()
- term.setCursorPos(1,1)
- end
- sucessfulInitialization = true
- local deviceList = peripheral.getNames()
- local bridges = {}
- for i = 1, #deviceList do
- local device = peripheral.getType(deviceList[i])
- if device == "colonyIntegrator" then
- colonyIntegrator = peripheral.wrap(deviceList[i])
- elseif device == "rsBridge" then
- bridges[#bridges + 1] = peripheral.wrap(deviceList[i])
- end
- end
- if #bridges ~= 2 then
- print("You need exactly 2 RS Bridges")
- sucessfulInitialization = false
- else
- for i = 1, #bridges do
- local bridge = bridges[i]
- if bridge.getName() == minecoloniesBridgeName then
- minecoloniesBridge = bridge
- elseif bridge.getName() == mainNetworkBridgeName then
- mainNetworkBridge = bridge
- end
- end
- end
- if minecoloniesBridge == nil or mainNetworkBridge == nil then
- print("At least one RS Bridge is missing.");
- sucessfulInitialization = false;
- end
- if colonyIntegrator == nil then
- print("Colony Integrator is missing");
- sucessfulInitialization = false;
- end
- until (sucessfulInitialization)
- end
- local function RequestAddOrIncreaseCount(flatRequests,request)
- local itemname = request.items[1].name
- for i=1,#flatRequests do
- if flatRequests[i].name == itemname then
- flatRequests[i].count = flatRequests[i].count + request.count
- return
- end
- end
- flatRequests[#flatRequests + 1] = {name = itemname,count = request.count,displayName = request.items[1].displayName}
- end
- local function GetFlattenRequests()
- local requests = colonyIntegrator.getRequests()
- if requests == nil or next(requests) == nil then
- return nil
- end
- local flatRequests = {}
- for i=1,#requests do
- RequestAddOrIncreaseCount(flatRequests,requests[i])
- end
- return flatRequests
- end
- local function PrintFlatRequests(flatRequests)
- term.clear()
- term.setCursorPos(1,1)
- for i=1,#flatRequests do
- print(flatRequests[i].count .. flatRequests[i].displayName)
- end
- end
- local function ArrayRemove(t, fnKeep)
- local j, n = 1, #t;
- for i=1,n do
- if (fnKeep(t, i, j)) then
- if (i ~= j) then
- t[j] = t[i];
- t[i] = nil;
- end
- j = j + 1;
- else
- t[i] = nil;
- end
- end
- return t;
- end
- local function CheckRequestsInMinecolonies(flatRequests)
- for i=1,#flatRequests do
- local req = flatRequests[i]
- local mineItem = minecoloniesBridge.getItem(req)
- if mineItem ~= nil then
- req.count = math.max(req.count-mineItem.amount,0)
- end
- end
- return ArrayRemove(flatRequests,function(t, i, j)
- local v = t[i];
- return (v.count > 0);
- end)
- end
- local function MoveOrCraftRequestedItemsFromMain(flatRequests)
- for i=1,#flatRequests do
- local req = flatRequests[i]
- local mnitem = mainNetworkBridge.getItem(req)
- local pattern = mainNetworkBridge.getPattern(req)
- if mnitem == nil and pattern == nil then
- --nothing we can do
- elseif (mnitem ~= nil and pattern ~= nil and mnitem.amount == pattern.outputs[1].amount) or (mnitem == nil and pattern ~= nil) then
- --network reports something, but its likely bug https://github.com/Seniorendi/AdvancedPeripherals/issues/270 or we dont have any but we can craft it
- if not mainNetworkBridge.isItemCrafting(req.name) then
- mainNetworkBridge.craftItem(req)
- end
- else
- local missing = 0
- local inNet = 0
- if mnitem == nil then
- missing = req.count
- else
- inNet = mnitem.amount
- missing = math.max(req.count-inNet,0)
- end
- if inNet > 0 then
- mainNetworkBridge.exportItem(req,mainToMinecoloniesDirection)
- end
- if pattern ~= nil and missing > 0 then
- if not mainNetworkBridge.isItemCrafting(req.name) then
- req.count = missing
- mainNetworkBridge.craftItem(req)
- end
- end
- end
- end
- end
- local function CheckItemExistsInBlacklist(blacklist,item)
- for i=1,#blacklist do
- if blacklist[i].name == item.name then
- return true
- end
- end
- return false
- end
- local function MoveItemsToMainNetworkWhenOverLimit(blacklist)
- local minecoloniesItems = minecoloniesBridge.listItems()
- for i=1,#minecoloniesItems do
- local mineitem = minecoloniesItems[i]
- if mineitem.amount > minecoloniesMaxAllowedItemCount then
- if not CheckItemExistsInBlacklist(blacklist,mineitem) then
- minecoloniesBridge.exportItem({name = mineitem.name,count = mineitem.amount - minecoloniesMaxAllowedItemCount},minecoloniesToMainDirection)
- end
- end
- end
- end
- InitalizeComponents()
- local cycle = minecoloniesNetworkCleanupIterations
- repeat
- local flatRequests = GetFlattenRequests()
- if cycle >= minecoloniesNetworkCleanupIterations then
- MoveItemsToMainNetworkWhenOverLimit(flatRequests or {})
- cycle = 0
- end
- if flatRequests == nil or next(flatRequests) == nil then
- term.clear()
- --term.setCursorPos(1,1)
- else
- flatRequests = CheckRequestsInMinecolonies(flatRequests)
- PrintFlatRequests(flatRequests);
- MoveOrCraftRequestedItemsFromMain(flatRequests)
- end
- cycle = cycle + 1
- sleep(loopInterval)
- until (false)
Add Comment
Please, Sign In to add comment