Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---please enter name of big storage inventory
- targetName = "minecraft:chest_0~"
- if targetName == "minecraft:chest_0~" then
- shell.run("edit","addItem")
- end
- if fs.exists("wanteditems.lua") then
- wantedItems = dofile("wanteditems.lua")
- else
- wantedItems = {}
- local file = fs.open("wanteditems.lua", "w")
- file.write("return {}")
- file.close()
- end
- local function saveWantedItems()
- local file = fs.open("wanteditems.lua", "w")
- file.write("return {\n")
- for name, count in pairs(wantedItems) do
- file.write((" [%q] = %d,\n"):format(name, count))
- end
- file.write("}\n")
- file.close()
- end
- local function addWantedItem()
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the full item name:")
- local name = read()
- if name == "" then
- print("Field can't be empty.")
- sleep(1)
- return
- end
- print("Enter the desired maximum count for this item:")
- local count = tonumber(read())
- if count == nil then
- print("Field can't be empty.")
- sleep(1)
- return
- end
- wantedItems[name] = count
- saveWantedItems()
- print(("%d x %s added to wanted items"):format(count, name))
- local event, key, isHeld = os.pullEvent("key")
- end
- local function removeWantedItem()
- term.clear()
- term.setCursorPos(1,1)
- print("Enter the name of the item to remove:")
- local name = read()
- if wantedItems[name] then
- wantedItems[name] = nil
- saveWantedItems()
- print(("%s removed from wanted items"):format(name))
- else
- print("Item not found in wanted items")
- end
- local event, key, isHeld = os.pullEvent("key")
- end
- local function viewWantedItems()
- term.clear()
- term.setCursorPos(1,1)
- print("Wanted items:")
- local items = {}
- for name, count in pairs(wantedItems) do
- table.insert(items, {name = name, count = count})
- end
- local selected = 1
- while true do
- term.clear()
- term.setCursorPos(1,1)
- for i, item in ipairs(items) do
- local prefix = i == selected and "> " or " "
- print(prefix .. item.count .. " x " .. item.name)
- end
- local event, key = os.pullEvent("key")
- if key == keys.up and selected > 1 then
- selected = selected - 1
- elseif key == keys.down and selected < #items then
- selected = selected + 1
- elseif key == keys.enter then
- term.clear()
- term.setCursorPos(1,1)
- print("1. Delete entry")
- print("2. Change entry")
- print("3. Back")
- print("Enter your choice:")
- local subchoice = tonumber(read())
- if subchoice == 1 then
- wantedItems[items[selected].name] = nil
- saveWantedItems()
- print(("%s removed from wanted items"):format(items[selected].name))
- sleep(1)
- table.remove(items, selected)
- if selected > #items then
- selected = #items
- end
- elseif subchoice == 2 then
- print("Enter the new desired maximum count for this item:")
- local newCount = tonumber(read())
- if newCount == "" then return end
- wantedItems[items[selected].name] = newCount
- saveWantedItems()
- items[selected].count = newCount
- print(("%d x %s changed in wanted items"):format(newCount, items[selected].name))
- sleep(1)
- elseif subchoice == 3 then
- break
- else
- print("Invalid choice")
- sleep(1)
- end
- elseif key == keys.backspace or key == keys.escape then
- break
- end
- end
- end
- function searchItems(table, key, value)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- print("Enter the item name (partial matches are fine):")
- term.setTextColor(colors.white)
- local itemName = read(nil, history)
- if itemName == "" then
- term.setTextColor(colors.red)
- print("\nNo item name entered\n")
- print("Press any key to continue...")
- term.setTextColor(colors.white)
- os.pullEvent("key")
- return
- end
- local matchingKeys = {}
- for k, v in pairs(table) do
- if string.match(k, itemName) then
- matchingKeys[#matchingKeys+1] = k
- end
- end
- if #matchingKeys == 0 then
- term.setTextColor(colors.red)
- print("\nNo matching items found")
- term.setTextColor(colors.green)
- print("\nPress any key to continue...")
- term.setTextColor(colors.white)
- os.pullEvent("key")
- return
- elseif #matchingKeys == 1 then
- key = matchingKeys[1]
- else
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- print("Multiple matching items found:")
- term.setTextColor(colors.white)
- for i = 1, #matchingKeys do
- local currentVal = table[matchingKeys[i]]
- if currentVal ~= nil then
- print(i .. ": " .. matchingKeys[i] .. " (current value: " .. currentVal .. ")")
- else
- print(i .. ": " .. matchingKeys[i])
- end
- end
- term.setTextColor(colors.green)
- print("\nPlease click the item you want.")
- term.setTextColor(colors.white)
- local selection
- local a, b = term.getCursorPos()
- local k, l = term.getSize()
- term.setCursorPos(k -1 , 1)
- term.setTextColor(colors.red)
- term.setBackgroundColor(colors.gray)
- -- term.setCursorPos(k , 1)
- print(" X")
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(a, b)
- while true do
- local event, button, x, y = os.pullEvent()
- if event == "mouse_click" then
- if y == 1 and x == term.getSize() then
- os.reboot()
- elseif y >= 2 and y <= #matchingKeys + 1 then
- selection = y - 1
- break
- end
- elseif event == "key" then
- if button == keys.one then selection = 1
- elseif button == keys.two then selection = 2
- elseif button == keys.three then selection = 3
- elseif button == keys.four then selection = 4
- end
- end
- end
- key = matchingKeys[selection]
- end
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- local currentVal = table[key]
- print("Enter the new amount for " .. key)
- term.setTextColor(colors.lightGray)
- print("Or leave blank to delete.")
- term.setTextColor(colors.green)
- print("\nCurrent amount: " .. (currentVal or "").."\n")
- term.setTextColor(colors.white)
- local newValue = read()
- if newValue == "" then
- table[key] = nil
- term.setTextColor(colors.red)
- print("Deleted " .. key)
- term.setTextColor(colors.white)
- else
- table[key] = newValue
- term.setTextColor(colors.lime)
- print("\nSuccess!")
- term.setTextColor(colors.green)
- print("Updated " .. key .. " to " .. newValue)
- term.setTextColor(colors.white)
- end
- saveWantedItems(table)
- print("Press any key to continue...")
- os.pullEvent("key")
- end
- function scanAndSendInventories()
- local inventory = peripheral.wrap(targetName)
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.green)
- print("Success!")
- term.setTextColor(colors.white)
- term.setCursorPos(1,3)
- for slot, item in pairs(inventory.list()) do
- if not wantedItems[item.name] then
- wantedItems[item.name] = item.count
- print("Added " ..item.name)
- else
- --print("Already in list: " ..item.name)
- end
- end
- local file = fs.open("wanteditems.lua", "w")
- file.write("return {\n")
- for name, count in pairs(wantedItems) do
- file.write((" [%q] = %d,\n"):format(name, count))
- end
- file.write("}\n")
- file.close()
- term.setTextColor(colors.lightGray)
- print("\nPress any key to continue..")
- term.setTextColor(colors.white)
- event, key = os.pullEvent("key")
- end
- while true do
- term.clear()
- term.setCursorPos(1,1)
- print("1. Add new item")
- print("2. View and remove items")
- print("3. Search for items")
- print("4. Quit")
- print("5. Update Items")
- print("Enter your choice:")
- local choice = tonumber(read())
- if choice == 1 then
- addWantedItem()
- elseif choice == 2 then
- viewWantedItems()
- elseif choice == 3 then
- searchItems(wantedItems, key, value)
- elseif choice == 4 then
- os.reboot()
- elseif choice == 5 then
- scanAndSendInventories()
- else
- print("Invalid choice")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement