Advertisement
LaniusFNV

dustManager

Jan 19th, 2022
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 KB | None | 0 0
  1. local function show_items(items)
  2.   for slot, item in pairs(items) do
  3.     local item_s = ("%s (%d)"):format(item.name, item.count)
  4.     print(slot .. ": " .. item_s)
  5.   end
  6. end
  7.  
  8. local function show_item_details(inventory, items)
  9.   for slot, item in pairs(items) do
  10.     local details = inventory.getItemDetail(slot)
  11.     local item_s = textutils.serialize(details.tags)
  12.     print(slot .. ": " .. item_s)
  13.   end
  14. end
  15.  
  16. local function tags_contain_tag(tags, tag)
  17.   for tag_, value in pairs(tags) do
  18.     if (tag_ == tag and value == true) then
  19.       return true
  20.     end
  21.   end
  22.   return false
  23. end
  24.  
  25. local function items_contains_tag(inventory, items, tag)
  26.   for slot, item in pairs(items) do
  27.     local details = inventory.getItemDetail(slot)
  28.     local tags = details.tags
  29.  
  30.     if tags_contain_ore(tags, tag) then
  31.       return true
  32.     end
  33.   end
  34.  
  35.   return false
  36. end
  37.  
  38. local function transfer_matching_tag(inventory, other_inventory, tag, limit)
  39.   local own_items = inventory.list()
  40.   local total_limit = limit;
  41.   local limit = total_limit % 64;
  42.  
  43.   for slot, item in pairs(own_items) do
  44.     local details = inventory.getItemDetail(slot)
  45.     local tags = details.tags
  46.  
  47.     while (limit > 0) do
  48.       if (tags_contain_ore(tags, tag)) then
  49.         inventory.pushItems(other_inventory, slot, limit)
  50.       end
  51.  
  52.       total_limit = total_limit - limit
  53.       limit = total_limit % 64
  54.     end
  55.   end
  56. end
  57.  
  58. local function ingot_count(item_name, goal_id)
  59.   rednet.send(goal_id, "GET /ingots/" .. item_name)
  60.  
  61.   local id, message
  62.   repeat
  63.     id, message = rednet.receive("ores")
  64.   until id == goal_id and string.match(message, "%d+") ~= nil
  65.  
  66.   local count = math.tointeger(message)
  67.  
  68.   if count == nil then
  69.     print(("ERROR: %d is nil (was %s)"):format(count, message))
  70.   end
  71.  
  72.   return count
  73. end
  74.  
  75. local function find_item(inventory, items, item_name)
  76.   for slot, item in pairs(items) do
  77.     if item.name == item_name then
  78.       return slot
  79.     end
  80.   end
  81.  
  82.   return nil
  83. end
  84.  
  85. local function transfer_items(inventory, other_inventory, goal_id)
  86.   local this_items = inventory.list()
  87.   local other_items = other_inventory.list()
  88.  
  89.   for slot, item in pairs(other_items) do
  90.     local ingot = ingots[item.name]
  91.  
  92.     local ingot_count_ = ingot_count(ingot, goal_id)
  93.  
  94.     if ingot_count_ ~= nil then
  95.       if ingot_count_ < 8 then
  96.         local dust = corresponding_dust(item.name)
  97.         local dust_slot = find_item(inventory, this_items, dust)
  98.         inventory.push(other_inventory, dust_slot, 8)
  99.       end
  100.     end
  101.   end
  102. end
  103.  
  104. local function main()
  105.   local input_inventory = peripheral.wrap("right")
  106.   local output_inventory = peripheral.wrap("left")
  107.  
  108.   rednet.host("ores", "dust")
  109.  
  110.   local ingot_manager_id
  111.   repeat
  112.     ingot_manager_id = rednet.lookup("ores", "ingots")
  113.     os.sleep(30)
  114.   until ingot_manager_id ~= nil
  115.  
  116.   while true do
  117.     transfer_items(input_inventory, output_inventory, ingot_manager_id)
  118.  
  119.     os.sleep(120)
  120.   end
  121. end
  122.  
  123. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement