Hiranus

Minecolonies RS integration

Mar 5th, 2022 (edited)
2,936
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.51 KB | None | 0 0
  1.  
  2. local minecoloniesBridgeName = "   [Minecolonies Bridge]"
  3. local mainNetworkBridgeName = "   [Main Network Bridge]"
  4. local mainToMinecoloniesDirection = "bottom"
  5. local minecoloniesToMainDirection = "top"
  6. local loopInterval = 5
  7. local minecoloniesMaxAllowedItemCount = 256
  8. local minecoloniesNetworkCleanupIterations = 10
  9.  
  10. local minecoloniesBridge = nil
  11. local mainNetworkBridge = nil
  12. local colonyIntegrator = nil
  13.  
  14. local next = next
  15.  
  16. local function InitalizeComponents()
  17.     local sucessfulInitialization = true
  18.     repeat
  19.         if sucessfulInitialization == false then
  20.             sleep(0.1)
  21.             term.clear()
  22.             term.setCursorPos(1,1)
  23.         end
  24.         sucessfulInitialization = true
  25.         local deviceList = peripheral.getNames()
  26.         local bridges = {}
  27.         for i = 1, #deviceList do
  28.             local device = peripheral.getType(deviceList[i])
  29.             if device == "colonyIntegrator" then
  30.                 colonyIntegrator = peripheral.wrap(deviceList[i])
  31.             elseif device == "rsBridge" then
  32.                 bridges[#bridges + 1] = peripheral.wrap(deviceList[i])
  33.             end
  34.         end
  35.         if #bridges ~= 2 then
  36.             print("You need exactly 2 RS Bridges")
  37.             sucessfulInitialization = false
  38.         else
  39.             for i = 1, #bridges do
  40.                 local bridge = bridges[i]
  41.                 if bridge.getName() == minecoloniesBridgeName then
  42.                     minecoloniesBridge = bridge
  43.                 elseif bridge.getName() == mainNetworkBridgeName then
  44.                     mainNetworkBridge = bridge
  45.                 end
  46.             end
  47.         end
  48.         if minecoloniesBridge == nil or mainNetworkBridge == nil then
  49.             print("At least one RS Bridge is missing.");
  50.             sucessfulInitialization = false;
  51.         end
  52.         if colonyIntegrator == nil then
  53.             print("Colony Integrator is missing");
  54.             sucessfulInitialization = false;
  55.         end
  56.     until (sucessfulInitialization)
  57. end
  58.  
  59. local function RequestAddOrIncreaseCount(flatRequests,request)
  60.     local itemname = request.items[1].name
  61.     for i=1,#flatRequests do
  62.         if flatRequests[i].name == itemname then
  63.             flatRequests[i].count = flatRequests[i].count + request.count
  64.             return
  65.         end
  66.     end
  67.  
  68.     flatRequests[#flatRequests + 1] = {name = itemname,count = request.count,displayName = request.items[1].displayName}
  69. end
  70.  
  71. local function GetFlattenRequests()
  72.     local requests = colonyIntegrator.getRequests()
  73.     if requests == nil or next(requests) == nil then
  74.         return nil
  75.     end
  76.     local flatRequests = {}
  77.     for i=1,#requests do
  78.         RequestAddOrIncreaseCount(flatRequests,requests[i])
  79.     end
  80.     return flatRequests
  81. end
  82.  
  83. local function PrintFlatRequests(flatRequests)
  84.     term.clear()
  85.     term.setCursorPos(1,1)
  86.     for i=1,#flatRequests do
  87.         print(flatRequests[i].count .. flatRequests[i].displayName)
  88.     end
  89. end
  90.  
  91. local function ArrayRemove(t, fnKeep)
  92.     local j, n = 1, #t;
  93.  
  94.     for i=1,n do
  95.         if (fnKeep(t, i, j)) then
  96.             if (i ~= j) then
  97.                 t[j] = t[i];
  98.                 t[i] = nil;
  99.             end
  100.             j = j + 1;
  101.         else
  102.             t[i] = nil;
  103.         end
  104.     end
  105.  
  106.     return t;
  107. end
  108.  
  109. local function CheckRequestsInMinecolonies(flatRequests)
  110.     for i=1,#flatRequests do
  111.         local req = flatRequests[i]
  112.         local mineItem = minecoloniesBridge.getItem(req)
  113.         if mineItem ~= nil then
  114.             req.count = math.max(req.count-mineItem.amount,0)
  115.         end
  116.     end
  117.     return ArrayRemove(flatRequests,function(t, i, j)
  118.         local v = t[i];
  119.         return (v.count > 0);
  120.     end)
  121. end
  122.  
  123. local function MoveOrCraftRequestedItemsFromMain(flatRequests)
  124.     for i=1,#flatRequests do
  125.         local req = flatRequests[i]
  126.         local mnitem = mainNetworkBridge.getItem(req)
  127.         local pattern = mainNetworkBridge.getPattern(req)
  128.  
  129.         if mnitem == nil and pattern == nil then
  130.             --nothing we can do
  131.         elseif (mnitem ~= nil and pattern ~= nil and mnitem.amount == pattern.outputs[1].amount) or (mnitem == nil and pattern ~= nil) then
  132.             --network reports something, but its likely bug https://github.com/Seniorendi/AdvancedPeripherals/issues/270 or we dont have any but we can craft it    
  133.             if not mainNetworkBridge.isItemCrafting(req.name) then
  134.                 mainNetworkBridge.craftItem(req)
  135.             end
  136.         else
  137.             local missing = 0
  138.             local inNet = 0
  139.             if mnitem == nil then
  140.                 missing = req.count
  141.             else
  142.                 inNet = mnitem.amount
  143.                 missing = math.max(req.count-inNet,0)
  144.             end
  145.             if inNet > 0 then
  146.                 mainNetworkBridge.exportItem(req,mainToMinecoloniesDirection)
  147.             end
  148.  
  149.             if pattern ~= nil and missing > 0 then
  150.                 if not mainNetworkBridge.isItemCrafting(req.name) then
  151.                     req.count = missing
  152.                     mainNetworkBridge.craftItem(req)
  153.                 end
  154.             end
  155.         end
  156.     end
  157. end
  158.  
  159. local function CheckItemExistsInBlacklist(blacklist,item)
  160.     for i=1,#blacklist do
  161.         if blacklist[i].name == item.name then
  162.             return true
  163.         end
  164.     end
  165.     return false
  166. end
  167.  
  168. local function MoveItemsToMainNetworkWhenOverLimit(blacklist)
  169.     local minecoloniesItems = minecoloniesBridge.listItems()
  170.     for i=1,#minecoloniesItems do
  171.         local mineitem = minecoloniesItems[i]
  172.         if mineitem.amount > minecoloniesMaxAllowedItemCount then
  173.             if not CheckItemExistsInBlacklist(blacklist,mineitem) then
  174.                 minecoloniesBridge.exportItem({name = mineitem.name,count = mineitem.amount - minecoloniesMaxAllowedItemCount},minecoloniesToMainDirection)
  175.             end
  176.         end
  177.     end
  178. end
  179.  
  180. InitalizeComponents()
  181. local cycle = minecoloniesNetworkCleanupIterations
  182. repeat
  183.     local flatRequests = GetFlattenRequests()
  184.     if cycle >= minecoloniesNetworkCleanupIterations then
  185.         MoveItemsToMainNetworkWhenOverLimit(flatRequests or {})
  186.         cycle = 0
  187.     end
  188.    
  189.     if flatRequests == nil or next(flatRequests) == nil then
  190.         term.clear()
  191.         --term.setCursorPos(1,1)
  192.     else
  193.         flatRequests = CheckRequestsInMinecolonies(flatRequests)
  194.         PrintFlatRequests(flatRequests);
  195.         MoveOrCraftRequestedItemsFromMain(flatRequests)
  196.     end
  197.     cycle = cycle + 1
  198.     sleep(loopInterval)
  199. until (false)
Add Comment
Please, Sign In to add comment