Advertisement
osmarks

EChest

Aug 14th, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.12 KB | None | 0 0
  1. local mods = peripheral.find "manipulator"
  2. local buffer = "up"
  3. local inventory = mods.getInventory()
  4. local ender = mods.getEnder()
  5. mods.clearCaptures()
  6.  
  7. local item_name_cache = {}
  8.  
  9. local function stack_type_id(stack)
  10.     return stack.name .. ":" .. tostring(stack.damage or 0) .. "#" .. (stack.nbtHash or "")
  11. end
  12.  
  13. local function display_name(stack, inv, slot)
  14.     local type_id = stack_type_id(stack)
  15.     if item_name_cache[type_id] then return item_name_cache[type_id] end
  16.     item_name_cache[type_id] = inv.getItemMeta(slot).displayName
  17.     return item_name_cache[type_id]
  18. end
  19.  
  20. local function scan(inventory)
  21.     local inv = {}
  22.     for slot, stack in pairs(inventory.list()) do
  23.         inv[slot] = display_name(stack, inventory, slot)
  24.     end
  25.     return inv
  26. end
  27.  
  28. local function strip_section_codes(str)
  29.     return str:gsub("\167[0-9a-z]", "")
  30. end
  31.  
  32. local function normalize(str)
  33.     return strip_section_codes(str):gsub(" ", ""):lower()
  34. end
  35.  
  36. local function find_items(inventory, search)
  37.     local candidates = {}
  38.     local search = normalize(search)
  39.     for slot, name in pairs(scan(inventory)) do
  40.         if normalize(name):match(search) then table.insert(candidates, { slot, strip_section_codes(name) }) end
  41.     end
  42.     return candidates
  43. end
  44.  
  45. local function string_to_list(str)
  46.     local list = {}
  47.     for i = 1, #str do
  48.         table.insert(list, str:sub(i, i))
  49.     end
  50.     return list
  51. end
  52.  
  53. local function contains(l, x)
  54.     for k, v in pairs(l) do
  55.         if v == x then return true end
  56.     end
  57.     return false
  58. end
  59.  
  60. local max_tell_length
  61.  
  62. local function split_tell(msg)
  63.     local remaining = msg
  64.     repeat
  65.         local fst = remaining:sub(1, 100)
  66.         remaining = remaining:sub(101)
  67.         mods.tell(fst)
  68.     until remaining == ""
  69. end
  70.  
  71. local function run(flags, args)
  72.     local source, destination, source_name, destination_name = inventory, ender, "inventory", "enderchest"
  73.     if contains(flags, ">") and not contains(flags, "<") then -- if set to pull FROM enderchest
  74.         source, destination, source_name, destination_name = ender, inventory, "enderchest", "inventory"
  75.     end
  76.     local query_only_mode = contains(flags, "?")
  77.     local items = find_items(source, args == "any" and "" or args)
  78.     if query_only_mode then
  79.         local item_names = {}
  80.         for _, v in pairs(items) do
  81.             table.insert(item_names, v[2])
  82.         end
  83.         split_tell(("Items matching query %s in %s: %s."):format(args, source_name, table.concat(item_names, ", ")))
  84.     else
  85.         local fst = items[1]
  86.         if not fst then mods.tell(("No item matching query %s found in %s."):format(args, source_name)) return end
  87.         mods.tell(("Moving %s from %s to %s."):format(fst[2], source_name, destination_name))
  88.         source.pushItems(buffer, fst[1])
  89.         local moved = destination.pullItems(buffer, 1)
  90.         mods.tell(("Moved %d item(s)."):format(moved))
  91.     end
  92. end
  93.  
  94. mods.capture "^!e"
  95. local owner = mods.getName()
  96.  
  97. while true do
  98.     local _, msg, _, user = os.pullEvent "chat_capture"
  99.     if user == owner then
  100.         print(msg)
  101.         local flags, args = msg:match "^!e ([<?>\^]+) ([A-Za-z0-9_ -]+)"
  102.         if not flags then mods.tell "!e command parse error."
  103.         else
  104.             local ok, err = pcall(run, string_to_list(flags), args)
  105.             if not ok then printError(err) mods.tell(err:sub(1, 100)) end
  106.         end
  107.     end
  108. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement