Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- SMART INVENTORY SYSTEM BY ORION.
- -- @the_orion.5
- -- in craft OS emulation, dont forget to attach the emulated inventories, here's the commands:
- -- attach top chest
- -- attach right chest
- -- attach bottom chest
- local BAPI = require("button_api")
- local main = BAPI.Desktop()
- local style = BAPI.Style
- local position = BAPI.Position
- local version = "v1.2"
- local width, height = term.getSize()
- if not fs.exists("list.json") then local a = fs.open("list.json","w") a.write("{}") a.close() end
- local item_knowledge_json = fs.open("list.json", "r")
- local item_knowledge = textutils.unserialiseJSON(item_knowledge_json.readAll())
- item_knowledge_json.close()
- local controller = peripheral.find("storagedrawers:controller") or peripheral.wrap("bottom")
- local vault = peripheral.find("create:item_vault") or peripheral.find("create_connected:item_silo") or peripheral.wrap("top")
- local io_chest = peripheral.wrap("right")
- -- tools
- local function reset()
- term.setTextColour(colors.white)
- paintutils.drawFilledBox(1,1,width,height, colors.black)
- term.clear()
- term.setCursorPos(1,1)
- end
- local function timer()
- os.sleep(300)
- reset()
- write("TIMED OUT.")
- end
- local function fracture(str)
- local f = {}
- for i in string.gmatch(str, "%a") do
- table.insert( f,i )
- end
- return f
- end
- local function synth()
- local function grab_unique(inventory)
- for slot, slot_obj in pairs(inventory.list()) do
- if slot_obj and not item_knowledge[slot_obj.name] then
- item_knowledge[slot_obj.name] = inventory.getItemDetail(slot).displayName
- end
- end
- end
- grab_unique(vault)
- grab_unique(controller)
- local t = fs.open("list.json", "w")
- t.write(textutils.serialiseJSON(item_knowledge))
- t.close()
- end
- local function pull(item, amount)
- local increase = 0
- local prev = amount
- while amount > 0 and prev ~= 0 do
- prev = amount
- for slot, slot_obj in pairs(controller.list()) do
- if amount <= 0 then
- return
- end
- if slot_obj.name == item then
- amount = amount - controller.pushItems(peripheral.getName(io_chest), slot, amount)
- end
- end
- for slot, slot_obj in pairs(vault.list()) do
- if amount <= 0 then
- return
- end
- if slot_obj.name == item then
- amount = amount - vault.pushItems(peripheral.getName(io_chest), slot, amount)
- end
- end
- prev = prev - amount
- end
- end
- local search_modes = {
- {
- name = "CLOSEST",
- exec = function(str)
- str = fracture(string.lower(str))
- local scores = {}
- for name, displayName in pairs(item_knowledge) do
- local score = 0
- for pointer, character in pairs(fracture(string.lower(displayName))) do
- if str[pointer] == character then
- score = score + 1
- else
- score = score - 1
- end
- end
- table.insert(scores,{
- name = name,
- displayName = displayName,
- score = score
- })
- end
- table.sort(scores,function(a,b)
- return a.score > b.score
- end)
- return scores[1].name, scores[1].displayName
- end
- },
- {
- name = "INCLUDES",
- exec = function(str)
- local found = {}
- for name, displayName in pairs(item_knowledge) do
- if string.find(string.lower(displayName), string.lower(str)) then
- table.insert(found, {name = name, displayName = displayName})
- end
- end
- local mx,my = term.getSize()
- local x, y = term.getCursorPos()
- local oy = y
- for i, v in pairs(found) do
- write(i..". "..v.displayName.."\n")
- y = y + 1
- if y == my-1 then
- write("PRESS ANY TO CONTINUE")
- os.pullEvent("key")
- for j = oy, my, 1 do
- term.setCursorPos(1,j)
- term.clearLine()
- end
- term.setCursorPos(1,oy)
- end
- end
- term.setCursorPos(1,3)
- write("INSERT NUMBER: ")
- local r = read()
- if string.lower(r) == "exit" then
- return "SPECIAL_END", nil
- end
- local selection = tonumber(r)
- if selection then
- for j = oy, my, 1 do
- term.setCursorPos(1,j)
- term.clearLine()
- end
- term.setCursorPos(1,oy)
- return found[selection].name, found[selection].displayName
- else
- return nil
- end
- end
- },
- {
- name = "EXACT",
- exec = function(str)
- for name, displayName in pairs(item_knowledge) do
- if string.lower(str) == string.lower(displayName) then
- return name, displayName
- end
- end
- return nil
- end
- }
- }
- local operations = {
- {
- name = "ORGANIZE INVENTORY",
- color = colors.yellow,
- exec = function()
- reset()
- write("ORGANIZING\n")
- local function organize(inventory, to)
- for slot, slot_obj in pairs(inventory) do
- write("[ "..slot.."/"..#inventory.." ]")
- local _,y = term.getCursorPos()
- term.setCursorPos(1,y)
- if slot_obj.count < 64 then
- pcall(controller.pushItems, to, slot)
- else
- pcall(vault.pushItems, to,slot)
- end
- end
- end
- organize(controller.list(), peripheral.getName(vault))
- write("\n")
- organize(vault.list(), peripheral.getName(controller))
- write("\nORGANIZATION COMPLETE")
- os.sleep(5)
- end
- },
- {
- name = "WITHDRAW ITEMS ",
- color = colors.orange,
- exec = function()
- reset()
- local mode = 2
- write("INSERT ITEM SEARCH\nCURRENT MODE: "..search_modes[mode].name.."\n")
- local x = false
- local search;
- local function m()
- local r = read()
- if string.lower(r) == "exit" then
- x = true
- return
- end
- search, search_display = search_modes[mode].exec(r)
- if search == "SPECIAL_END" then
- x = true
- end
- if not search then
- write("ITEM NOT FOUND")
- os.sleep(2)
- reset()
- write("INSERT ITEM SEARCH\nCURRENT MODE: "..search_modes[mode].name.."\n")
- m()
- return
- end
- end
- local function s()
- local x,y = term.getCursorPos()
- local _, key = os.pullEvent("key")
- if key == keys.right then
- mode = mode+1>#search_modes and 1 or mode+1
- elseif key == keys.left then
- mode = mode-1<1 and #search_modes or mode-1
- end
- term.setCursorPos(1,2)
- term.clearLine()
- write("CURRENT MODE: "..search_modes[mode].name.."\n")
- term.setCursorPos(x,y)
- s()
- end
- parallel.waitForAny(m,s)
- if x then
- return
- end
- term.setCursorPos(1,1)
- term.clearLine()
- write("INSERT AMOUNT")
- local amount
- term.setCursorPos(1,3)
- term.clearLine()
- write("ITEM: "..search_display.."\n")
- local count = 0
- local function c(inventory)
- for slot, slot_obj in pairs(inventory) do
- if slot_obj.name == search then
- count = count + slot_obj.count
- end
- end
- end
- c(controller.list())
- c(vault.list())
- write("AVAILABLE: "..count.."\n")
- local function get_amount()
- r = read()
- if string.lower(r) == "exit" then
- return
- end
- amount = tonumber(r)
- if amount == nil then
- --try to get a calculation
- local func, err = load("return "..r)
- amount = func()
- if not amount then
- term.setCursorPos(1,5)
- write("INVALID NUMBER/EQUATION")
- os.sleep(2)
- term.clearLine()
- get_amount()
- return
- end
- end
- end
- get_amount()
- if search and amount then
- pull(search, amount)
- write("ITEMS WITHDRAWN.")
- os.sleep(2)
- end
- end
- },
- {
- name = "DEPOSIT ITEMS ",
- color = colors.red,
- exec = function()
- reset()
- term.setTextColor(colors.red)
- write("ALL ITEMS IN THE CHEST SHALL BE DEPOSITED, CONTINUE?\n(Y/N)\n")
- local function confirm()
- local _, key = os.pullEvent("key")
- if key == keys.y then
- local inventory = io_chest.list()
- for slot, _ in pairs(inventory) do
- write("[ "..slot.."/"..#inventory.." ]")
- local _,y = term.getCursorPos()
- term.setCursorPos(1,y)
- io_chest.pushItems(peripheral.getName(vault), slot)
- end
- write("\nDEPOSIT COMPLETE")
- synth()
- elseif key == keys.n then
- return
- else
- confirm()
- end
- end
- confirm()
- end
- }
- }
- -- execute
- local function main_loop()
- reset()
- write(" NSP SMART INVENTORY BY NUNOTO")
- local ops = {}
- for i, obj in pairs(operations) do
- local s = style(obj.color)
- s.text = obj.name
- s.textColor = colors.white
- local p = position(width+1, height, "left", "", obj.name,1,1)
- p.s_y = (i) * 4
- p.e_y = (i) * 4 + 2
- main.addButton(p,s,obj.exec)
- end
- main:awaitButtonClick()
- main_loop()
- end
- item_knowledge = {}
- synth()
- main_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement