Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ME SYSTEM BY NUNOTO
- term.clear()
- local controller = peripheral.wrap("left")
- local output = peripheral.wrap("right")
- local item_queue = {}
- local function reset()
- term.clear()
- term.setCursorPos(1,1)
- end
- local function list()
- return controller.list()
- -- RETURNS A TABLE WITH SLOT OBJECTS ({count = 64, name = "..."})
- end
- local function special_unpack(s,e,tr)
- local t = {}
- for i = s, e, 1 do
- table.insert(t, tr[i])
- end
- return t
- end
- local function split(str, point)
- return string.sub(str,1, string.find(str,point)-1), string.sub(str, string.find(str,point)+1)
- end
- local function is_in_table(haystack,needle)
- for _, v in pairs(haystack) do
- if v == needle then
- return true
- end
- end
- return false
- end
- local function get_slot(name)
- local l = list()
- for i, v in pairs(l) do
- if name == v.name then
- return i
- end
- end
- end
- local function open_page(page)
- reset()
- local l = list()
- local section = special_unpack((page+1),page+16,l)
- for slot, item in pairs(section) do
- if item ~= nil then
- local source, name = split(item.name, ":")
- write(item.count.."x "..name:gsub("_", " ").."\n")
- end
- end
- term.setCursorPos(1,18)
- write("< | >")
- local event, button, x, y = os.pullEvent("mouse_click")
- print(x,y)
- os.pullEvent("redstone")
- end
- local function pull(slot, amount)
- controller.pushItems(peripheral.getName(output),slot,amount)
- end
- local function push()
- for i = 1, #output.list(), 1 do
- output.pushItems(peripheral.getName(controller),i)
- end
- end
- function levenshtein(str1, str2)
- local matrix = {}
- for i = 0, #str1 do
- matrix[i] = {[0] = i}
- end
- for j = 0, #str2 do
- matrix[0][j] = j
- end
- for i = 1, #str1 do
- for j = 1, #str2 do
- local cost = (str1:sub(i, i) == str2:sub(j, j) and 0 or 1)
- matrix[i][j] = math.min(matrix[i-1][j] + 1, matrix[i][j-1] + 1, matrix[i-1][j-1] + cost)
- end
- end
- return matrix[#str1][#str2]
- end
- function closest_match(target, strings)
- local closest, distance = nil, math.huge
- for index, str in pairs(strings) do
- if str.name ~= nil then
- local d = levenshtein(target, str.name)
- if d < distance then
- closest, distance = str.name, d
- end
- end
- end
- return closest
- end
- local function flush()
- for i, v in pairs(list()) do
- if v.count <= 64 then
- pull(i, 64)
- end
- end
- reset()
- write("ITEMS FLUSHED.")
- os.sleep(5)
- end
- local function deposit()
- push()
- reset()
- write("ITEMS SUCCESSFULLY DEPOSITED.")
- os.sleep(5)
- end
- local function withdraw()
- reset()
- write("INSERT ITEM NAME:\nITEM: ")
- local search = string.lower(read()):gsub(" ", "_")
- local found = closest_match(search,list())
- local slot = get_slot(found)
- if found == nil then
- write("\nITEM NOT FOUND")
- os.sleep(2)
- withdraw()
- return
- end
- reset()
- write("INSERT AMOUNT:\n")
- local mod, item_ = split(found, ":")
- write("ITEM: "..item_.."\nAMOUNT: ")
- local amount = read()
- amount = tonumber(amount)
- pull(slot, amount)
- end
- while true do
- reset()
- write("NSP CC INVENTORY SYSTEM\nO: ORDER ITEM\nI: INPUT ITEM\nF: FLUSH ITEMS\n")
- local key = read()
- --if key == keys.l then
- -- open_page(1)
- --end
- if key == "o" then
- withdraw()
- elseif key == "i" then
- deposit()
- elseif key == "f" then
- flush()
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement