Advertisement
Shaka01

craftTurtle_display_dependancy

Feb 11th, 2023 (edited)
822
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.98 KB | None | 0 0
  1. --initialize
  2. local craftingRecipes = {}
  3. local file = fs.open("craftingRecipes", "r")
  4. local recipeNames = {}
  5. usingMenu = true
  6. term.setTextColor(colors.white)
  7.  
  8. ---load recipes
  9. if file then
  10.   craftingRecipes = textutils.unserialize(file.readAll())
  11.   file.close()
  12. end
  13. for k, v in pairs(craftingRecipes) do
  14.   table.insert(recipeNames, k)
  15. end
  16.  
  17. local itemNames = {}
  18. for itemName in pairs(craftingRecipes) do
  19.   table.insert(itemNames, itemName)
  20. end
  21.  
  22. local function scrollList()
  23.  x, y = term.getSize()
  24.   local itemsPerPage = y - 1
  25.   local currentPage = 1
  26.   local maxPages = math.ceil(#itemNames / itemsPerPage)
  27.   local selectedIndex = 1
  28.  
  29.   while true do
  30.     term.clear()
  31.     term.setCursorPos(1,1)
  32.     local startIndex = (currentPage - 1) * itemsPerPage + 1
  33.     local endIndex = math.min(startIndex + itemsPerPage - 1, #itemNames)
  34.  
  35.     local displayedItems = {}
  36.     for i = startIndex, endIndex do
  37.       table.insert(displayedItems, itemNames[i]:match(":(.*)"))
  38.     end
  39.  
  40.     -- sort the displayed items alphabetically
  41.     table.sort(displayedItems)
  42.  
  43.     local function printItem(itemName, selected)
  44.       local prefix = ""
  45.       if selected then
  46.         prefix = "> "
  47.       end
  48.       print(prefix .. itemName)
  49.     end
  50.  
  51.     for i, itemName in ipairs(displayedItems) do
  52.       printItem(itemName, i + startIndex - 1 == selectedIndex)
  53.     end
  54.  
  55.     local event, userInput = os.pullEvent("key")
  56.     if userInput == keys.down and selectedIndex < #itemNames then
  57.       selectedIndex = selectedIndex + 1
  58.       if selectedIndex > endIndex then
  59.         currentPage = currentPage + 1
  60.       end
  61.     elseif userInput == keys.up and selectedIndex > 1 then
  62.       selectedIndex = selectedIndex - 1
  63.       if selectedIndex < startIndex then
  64.         currentPage = currentPage - 1
  65.       end
  66.     elseif userInput == keys.delete then
  67.   local originalSelectedIndex = selectedIndex + (currentPage - 1) * itemsPerPage
  68.   local selectedRecipe = craftingRecipes[itemNames[originalSelectedIndex]]
  69.   local selectedRecipeName = itemNames[originalSelectedIndex]:match(":(.*)")
  70.  
  71.   term.clear()
  72.   term.setCursorPos(1,1)
  73.   term.setTextColor(colors.red)
  74.   print("Delete recipe for")
  75.   print(selectedRecipeName.. "?")
  76.   term.setTextColor(colors.white)
  77.   print("\nPress Enter to delete, any other key to cancel.")
  78.   local event, key = os.pullEvent("key")
  79.     if key ~= keys.enter then
  80.       os.reboot()
  81.       return
  82.     end
  83.   craftingRecipes[itemNames[originalSelectedIndex]] = nil
  84.   local file = fs.open("craftingRecipes", "w")
  85.     file.write(textutils.serialize(craftingRecipes))
  86.     file.close()
  87.       os.reboot()
  88.  
  89.       -- print(selectedRecipe)
  90.       return
  91.     elseif userInput == keys.enter then
  92.             term.clear()
  93.             term.setCursorPos(1,1)
  94.             term.setTextColor(colors.yellow)
  95.             print("Exiting.")
  96.             usingMenu = false
  97.             sleep(1)
  98.             os.reboot()
  99.         return
  100.     end
  101.   end
  102.   return itemNames[selectedIndex]
  103. end
  104.  
  105.  
  106.  
  107. local selectedItem = scrollList()
  108.  
  109.  
  110. -- print("Selected item: " .. selectedItem)
  111.  
  112.  
  113.  
  114.  
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement