Advertisement
hornedcommando

Minecraft ComputerCraft Modem ListPeripherals Above and Beyond

Nov 16th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.97 KB | Gaming | 0 0
  1. ---
  2. ---Desc: A utility program which lists the peripherals  
  3. ---Which the computer can detect
  4. ---via wired modems
  5. ---
  6.  
  7. --By: Spank
  8.  
  9. -- Get the peripheral name from the command line argument
  10. local args = {...}
  11. if #args < 1 then
  12.     print("Usage: script <peripheral_name>")
  13.     return
  14. end
  15.  
  16. local chestName = args[1]
  17.  
  18. -- Wrap the specified peripheral
  19. local chest = peripheral.wrap(chestName)
  20. if not chest then
  21.     print("Error: Peripheral not found.")
  22.     return
  23. end
  24.  
  25. -- Function to print output with pagination
  26. local function printWithPagination(output)
  27.     local lines = {}
  28.     for line in output:gmatch("[^\n]+") do
  29.         table.insert(lines, line)
  30.     end
  31.  
  32.     local currentPage = 1
  33.     local pageSize = 5
  34.     local totalPages = math.ceil(#lines / pageSize)
  35.  
  36.     while true do
  37.         term.clear()
  38.         term.setCursorPos(1, 1)
  39.  
  40.         for i = 1, pageSize do
  41.             local lineIndex = (currentPage - 1) * pageSize + i
  42.             if lineIndex <= #lines then
  43.                 print(lines[lineIndex])
  44.             else
  45.                 break
  46.             end
  47.         end
  48.  
  49.         print(("Page %d/%d. Page Up/Down, Home/End to navigate. Press 'x' to exit."):format(currentPage, totalPages))
  50.  
  51.         local event, key = os.pullEvent("key")
  52.         if key == keys.pageUp and currentPage > 1 then
  53.             currentPage = currentPage - 1
  54.         elseif key == keys.pageDown and currentPage < totalPages then
  55.             currentPage = currentPage + 1
  56.         elseif key == keys.home then
  57.             currentPage = 1
  58.         elseif key == keys["end"] then
  59.             currentPage = totalPages
  60.         elseif key == keys.x then
  61.             break
  62.         end
  63.     end
  64. end
  65.  
  66. -- Get the items from the chest and format them
  67. local output = {}
  68. for slot, item in pairs(chest.list()) do
  69.     table.insert(output, ("%d x %s in slot %d"):format(item.count, item.name, slot))
  70. end
  71.  
  72. -- Print output with pagination
  73. printWithPagination(table.concat(output, "\n"))
  74.  
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement