Advertisement
HappySunChild

jukebox

Apr 9th, 2025 (edited)
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.42 KB | None | 0 0
  1. local RECORDS = {
  2.     { "blocks", "minecraft:music_disc.blocks" },
  3.     { "cat", "minecraft:music_disc.cat" },
  4.     { "chirp", "minecraft:music_disc.chirp" },
  5.     { "far", "minecraft:music_disc.far" },
  6.     { "mall", "minecraft:music_disc.mall" },
  7.     { "mellohi", "minecraft:music_disc.mellohi" },
  8.     { "stal", "minecraft:music_disc.stal" },
  9.     { "strad", "minecraft:music_disc.strad" },
  10.     { "ward", "minecraft:music_disc.ward" },
  11.     { "wait", "minecraft:music_disc.wait" },
  12.     { "pigstep", "minecraft:music_disc.pigstep" },
  13. }
  14.  
  15. ---@type peripheral.Speaker
  16. local SPEAKER = peripheral.find("speaker")
  17. local WIDTH, HEIGHT = term.getSize()
  18.  
  19. local PAGE_SIZE = HEIGHT - 2
  20. local PAGE_COUNT = math.ceil(#RECORDS / PAGE_SIZE)
  21.  
  22. local selection = 1
  23. local playing = 0
  24.  
  25. ---@param y number
  26. ---@param text string
  27. local function writeCenter(y, text)
  28.     local length = string.len(text)
  29.  
  30.     term.setCursorPos(math.floor(WIDTH / 2 - length / 2), y)
  31.     write(text)
  32. end
  33.  
  34. ---@param x number
  35. ---@param y number
  36. ---@param text string
  37. local function writeRight(x, y, text)
  38.     local length = string.len(text)
  39.  
  40.     term.setCursorPos(x - length, y)
  41.     write(text)
  42. end
  43.  
  44. local function displayRecords()
  45.     term.clear()
  46.     term.setTextColor(colors.purple)
  47.     writeCenter(1, "Jukebox")
  48.  
  49.     local currentPage = math.floor(selection / PAGE_SIZE)
  50.  
  51.     for offset = 1, PAGE_SIZE do
  52.         local recordIndex = currentPage * PAGE_SIZE + offset
  53.         local recordInfo = RECORDS[recordIndex]
  54.  
  55.         if not recordInfo then
  56.             break
  57.         end
  58.  
  59.         local name = recordInfo[1]
  60.  
  61.         term.setTextColor(colors.lightGray)
  62.  
  63.         if selection == recordIndex then
  64.             term.setTextColor(colors.white)
  65.  
  66.             name = "> " .. name
  67.         end
  68.  
  69.         name = string.format("%d. %s", offset, name)
  70.  
  71.         term.setCursorPos(2, offset + 2)
  72.         write(name)
  73.     end
  74.  
  75.     local playingRecord = RECORDS[playing]
  76.     local playingName = playingRecord and playingRecord[1] or "None"
  77.  
  78.     term.setTextColor(colors.lightGray)
  79.     writeRight(WIDTH, 2, string.format("Playing: %s", playingName))
  80.     writeRight(WIDTH, HEIGHT, string.format("P. %d/%d", currentPage + 1, PAGE_COUNT))
  81. end
  82.  
  83. displayRecords()
  84.  
  85. while true do
  86.     local _, key = os.pullEvent("key")
  87.  
  88.     if key == keys.up then
  89.         selection = math.max(selection - 1, 1)
  90.     elseif key == keys.down then
  91.         selection = math.min(selection + 1, #RECORDS)
  92.     elseif key == keys.enter then
  93.         local info = RECORDS[selection]
  94.  
  95.         if SPEAKER.playSound(info[2], 2, 1) then
  96.             playing = selection
  97.         end
  98.     end
  99.  
  100.     displayRecords()
  101. end
  102.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement