Advertisement
g1eagle

Minecolonies CC Tweaked Refined Storage importer V1

Jan 21st, 2023 (edited)
695
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.65 KB | None | 0 0
  1. -- initialize Monitor
  2. local monitor = peripheral.find("monitor")
  3. if not monitor then error("Monitor not found.") end
  4. monitor.setTextScale(0.5)
  5. monitor.clear()
  6. monitor.setCursorPos(1, 1)
  7. monitor.setCursorBlink(false)
  8. print("Monitor initialized.")
  9.  
  10. -- Init ColonyIntergrator
  11. local colony = peripheral.find("colonyIntegrator")
  12. if colony == nil then error("colonyIntegrator not found") end
  13. if not colony.isInColony then error("Block is not in a colony") end
  14.  
  15. -- Init RSBridge
  16. local bridge = peripheral.find("rsBridge")
  17. if bridge == nil then error("rsBridge not found") end
  18. --bridge.exportItem({name="minecraft:birch_stairs", count=1}, "left")
  19.  
  20.  
  21.  
  22. function PullRequiredMats(colony_requests)
  23.     colony_requests = colony.getRequests()
  24.     local UnableToCraft = {}
  25.  
  26.  
  27.            
  28.    
  29.     --get requests
  30.         local item_needed_key = {}
  31.         local item_needed_state = {}
  32.         local item_needed_name = {}
  33.         local item_needed_count = {}
  34.         local item_needed_target = {}
  35.         local item_needed_detailname = {}
  36.    
  37.         local requestcounter = 0
  38.     for key, item in pairs(colony_requests) do
  39.         item_needed_key[requestcounter] = key
  40.         item_needed_name[requestcounter] = item.name
  41.         item_needed_state[requestcounter] = item.state
  42.         item_needed_count[requestcounter] = item.count
  43.         item_needed_target[requestcounter] = item.target
  44.         --print("Item " .. key)
  45.         --print("Name: " .. item.name)
  46.         --print("State: " .. item.state)
  47.         --print("Count: " .. item.count)
  48.         --print("Target: " .. item.target)
  49.         --print("Items: ")
  50.         for j, item_detail in ipairs(item.items) do
  51.             print("\t" .. item_detail.name)
  52.         item_needed_detailname[requestcounter] = item_detail.name
  53.         end
  54.         requestcounter = requestcounter + 1
  55.     end
  56.    
  57.    
  58.     -- get items in system
  59.     local item_current_name = {}
  60.     local item_current_amount = {}
  61.     local item_current_isCraftable = {}
  62.     local item_current_displayName = {}
  63.    
  64.     current_items = bridge.listItems()
  65.     local currentitemcounter = 0
  66.     for _, item in pairs(current_items) do
  67.         item_current_name[currentitemcounter] = (item.name or "Unknown")
  68.         item_current_amount[currentitemcounter] = (item.amount)
  69.         item_current_isCraftable[currentitemcounter] = (tostring(item.isCraftable))
  70.         item_current_displayName[currentitemcounter] = (item.displayName)
  71.         currentitemcounter = currentitemcounter + 1
  72.     end
  73.    
  74.    
  75.     print (currentitemcounter)
  76.     for j = 0, requestcounter-1 do
  77.         local RSUseable = 0
  78.         local itemrequestedbutnotfound = item_needed_detailname[j]
  79.         for i = 0, currentitemcounter-1 do
  80.             if item_needed_state[j] == "IN_PROGRESS" then
  81.                 --print("Item1: " .. item_needed_detailname[j])
  82.                 --print("Itemfind: " .. item_current_name[i])
  83.                 --print("Needed: " .. item_current_isCraftable[i])
  84.                 --print("Available: " .. item_current_displayName[i])
  85.    
  86.                 if item_current_name[i]:match(item_needed_detailname[j]) then
  87.                     --print("Item: " .. item_current_displayName[i])
  88.                     --print ("Item Needed 1: " .. item_needed_detailname[j])
  89.    
  90.                     if item_current_amount[i] >= item_needed_count[j] then
  91.                             provided = bridge.exportItem({name=item_current_name[i], count=item_needed_count[j]}, "left")
  92.                             if provided == 0 then
  93.                                 print("Unable to Export " .. item_current_name[i])
  94.                             else
  95.                                 print("Exporting " .. provided .. " of " .. item_current_name[i])
  96.                                 local item = item_current_name[i]
  97.                                 local needed = item_needed_count[j]
  98.                                 RSUseable = 1  
  99.                             end
  100.                         if item_current_name[i] == "domum_ornamentum:shingle" then
  101.                             print ("Unable to export " .. item_current_name[i])
  102.                             RSUseable = 0
  103.                         end
  104.    
  105.                     else
  106.    
  107.    
  108.     -- If you don't find enough craft the missing ammount if craftable.
  109.    
  110.                         if item_current_isCraftable[i] == "true" then
  111.                             print("Craft more")
  112.                             print(item_current_name[i])
  113.                             bridge.craftItem({name=item_current_name[i], count=item_needed_count[j]})
  114.                             RSUseable = 1
  115.                         else
  116.                             print("Unable to craft")
  117.                             print(item_current_isCraftable[i])
  118.                             RSUseable = 0
  119.                             print(item_needed_detailname[j])
  120.                             NotCraftable = { name=item_needed_detailname[j]}
  121.                             table.insert(UnableToCraft, NotCraftable)
  122.                         end
  123.                         end
  124.                 else
  125.                 --print("No Has")
  126.                 --print("Item: " .. item_current_displayName[i])
  127.                 --print ("Item Needed: " .. item_needed_name[j])
  128.    
  129.                 end
  130.    
  131.             else
  132.             RSUseable = 1
  133.             end
  134.         end
  135.         if RSUseable == 0 then
  136.             --file.write(itemrequestedbutnotfound)
  137.             --file.write("\n")
  138.         end
  139.     end
  140.  
  141. ---- Display lists
  142.     row = 3
  143.     monitor.clear()
  144.     header_shown = 0
  145.     for e in pairs(UnableToCraft) do
  146.         NotCraftable = UnableToCraft[e]
  147.         if header_shown == 0 then
  148.             mPrintRowJustified(monitor, row, "center", "Unable To Craft")
  149.             header_shown = 1
  150.             row = row + 1
  151.         end
  152.         text = string.format("%d %s", 1, NotCraftable.name)
  153.         mPrintRowJustified(monitor, row, "left", text)
  154.         row = row + 1
  155.     end
  156.  
  157.     if row == 3 then mPrintRowJustified(monitor, row, "center", "No Open Requests") end
  158.     print("Scan completed at", textutils.formatTime(os.time(), false) .. " (" .. os.time() ..").")
  159. end
  160. --file.close()
  161. --itemfile.close()
  162.  
  163.  
  164. -- Prints strings left, centered, or right justified at a specific row and
  165. -- specific foreground/background color.
  166. function mPrintRowJustified(mon, y, pos, text, ...)
  167.     w, h = mon.getSize()
  168.     fg = mon.getTextColor()
  169.     bg = mon.getBackgroundColor()
  170.  
  171.     if pos == "left" then x = 1 end
  172.     if pos == "center" then x = math.floor((w - #text) / 2) end
  173.     if pos == "right" then x = w - #text end
  174.  
  175.     if #arg > 0 then mon.setTextColor(arg[1]) end
  176.     if #arg > 1 then mon.setBackgroundColor(arg[2]) end
  177.     mon.setCursorPos(x, y)
  178.     mon.write(text)
  179.     mon.setTextColor(fg)
  180.     mon.setBackgroundColor(bg)
  181. end
  182.  
  183.  
  184.  
  185.  
  186. ---Display countdown timer
  187. function displayTimer(mon, t)
  188.     now = os.time()
  189.  
  190.     cycle = "day"
  191.     cycle_color = colors.orange
  192.     if now >= 4 and now < 6 then
  193.         cycle = "sunrise"
  194.         cycle_color = colors.orange
  195.     elseif now >= 6 and now < 18 then
  196.         cycle = "day"
  197.         cycle_color = colors.yellow
  198.     elseif now >= 18 and now < 19.5 then
  199.         cycle = "sunset"
  200.         cycle_color = colors.orange
  201.     elseif now >= 19.5 or now < 5 then
  202.         cycle = "night"
  203.         cycle_color = colors.red
  204.     end
  205.  
  206.     timer_color = colors.orange
  207.     if t < 15 then timer_color = colors.yellow end
  208.     if t < 5 then timer_color = colors.red end
  209.  
  210.     mPrintRowJustified(mon, 1, "left", string.format("Time: %s [%s]    ", textutils.formatTime(now, false), cycle), cycle_color)
  211.     if cycle ~= "night" then mPrintRowJustified(mon, 1, "right", string.format("    Remaining: %ss", t), timer_color)
  212.     else mPrintRowJustified(mon, 1, "right", "    Remaining: PAUSED", colors.red) end
  213. end
  214.  
  215.  
  216.  
  217.  
  218. ---Main Function
  219. local time_between_runs = 30
  220. local current_run = time_between_runs
  221. displayTimer(monitor, current_run)
  222. local TIMER = os.startTimer(1)
  223.  
  224. while true do
  225.     local e = {os.pullEvent()}
  226.     if e[1] == "timer" and e[2] == TIMER then
  227.         now = os.time()
  228.         if now >= 5 and now < 19.5 then
  229.             current_run = current_run - 1
  230.             if current_run <= 0 then
  231.                 colony_requests = colony.getRequests()
  232.                 PullRequiredMats(colony_requests)
  233.                 current_run = time_between_runs
  234.             end
  235.         end
  236.         displayTimer(monitor, current_run)
  237.         TIMER = os.startTimer(1)
  238.     elseif e[1] == "monitor_touch" then
  239.         os.cancelTimer(TIMER)
  240.                 colony_requests = colony.getRequests()
  241.                 PullRequiredMats(colony_requests)
  242.         current_run = time_between_runs
  243.         displayTimer(monitor, current_run)
  244.         TIMER = os.startTimer(1)
  245.     end
  246. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement