Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --initialize
- local craftingRecipes = {}
- local file = fs.open("craftingRecipes", "r")
- local recipeNames = {}
- usingMenu = true
- term.setTextColor(colors.white)
- ---load recipes
- if file then
- craftingRecipes = textutils.unserialize(file.readAll())
- file.close()
- end
- for k, v in pairs(craftingRecipes) do
- table.insert(recipeNames, k)
- end
- local itemNames = {}
- for itemName in pairs(craftingRecipes) do
- table.insert(itemNames, itemName)
- end
- local function scrollList()
- x, y = term.getSize()
- local itemsPerPage = y - 1
- local currentPage = 1
- local maxPages = math.ceil(#itemNames / itemsPerPage)
- local selectedIndex = 1
- while true do
- term.clear()
- term.setCursorPos(1,1)
- local startIndex = (currentPage - 1) * itemsPerPage + 1
- local endIndex = math.min(startIndex + itemsPerPage - 1, #itemNames)
- local displayedItems = {}
- for i = startIndex, endIndex do
- table.insert(displayedItems, itemNames[i]:match(":(.*)"))
- end
- -- sort the displayed items alphabetically
- table.sort(displayedItems)
- local function printItem(itemName, selected)
- local prefix = ""
- if selected then
- prefix = "> "
- end
- print(prefix .. itemName)
- end
- for i, itemName in ipairs(displayedItems) do
- printItem(itemName, i + startIndex - 1 == selectedIndex)
- end
- local event, userInput = os.pullEvent("key")
- if userInput == keys.down and selectedIndex < #itemNames then
- selectedIndex = selectedIndex + 1
- if selectedIndex > endIndex then
- currentPage = currentPage + 1
- end
- elseif userInput == keys.up and selectedIndex > 1 then
- selectedIndex = selectedIndex - 1
- if selectedIndex < startIndex then
- currentPage = currentPage - 1
- end
- elseif userInput == keys.delete then
- local originalSelectedIndex = selectedIndex + (currentPage - 1) * itemsPerPage
- local selectedRecipe = craftingRecipes[itemNames[originalSelectedIndex]]
- local selectedRecipeName = itemNames[originalSelectedIndex]:match(":(.*)")
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.red)
- print("Delete recipe for")
- print(selectedRecipeName.. "?")
- term.setTextColor(colors.white)
- print("\nPress Enter to delete, any other key to cancel.")
- local event, key = os.pullEvent("key")
- if key ~= keys.enter then
- os.reboot()
- return
- end
- craftingRecipes[itemNames[originalSelectedIndex]] = nil
- local file = fs.open("craftingRecipes", "w")
- file.write(textutils.serialize(craftingRecipes))
- file.close()
- os.reboot()
- -- print(selectedRecipe)
- return
- elseif userInput == keys.enter then
- term.clear()
- term.setCursorPos(1,1)
- term.setTextColor(colors.yellow)
- print("Exiting.")
- usingMenu = false
- sleep(1)
- os.reboot()
- return
- end
- end
- return itemNames[selectedIndex]
- end
- local selectedItem = scrollList()
- -- print("Selected item: " .. selectedItem)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement