Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local RECORDS = {
- { "blocks", "minecraft:music_disc.blocks" },
- { "cat", "minecraft:music_disc.cat" },
- { "chirp", "minecraft:music_disc.chirp" },
- { "far", "minecraft:music_disc.far" },
- { "mall", "minecraft:music_disc.mall" },
- { "mellohi", "minecraft:music_disc.mellohi" },
- { "stal", "minecraft:music_disc.stal" },
- { "strad", "minecraft:music_disc.strad" },
- { "ward", "minecraft:music_disc.ward" },
- { "wait", "minecraft:music_disc.wait" },
- { "pigstep", "minecraft:music_disc.pigstep" },
- }
- ---@type peripheral.Speaker
- local SPEAKER = peripheral.find("speaker")
- local WIDTH, HEIGHT = term.getSize()
- local PAGE_SIZE = HEIGHT - 2
- local PAGE_COUNT = math.ceil(#RECORDS / PAGE_SIZE)
- local selection = 1
- local playing = 0
- ---@param y number
- ---@param text string
- local function writeCenter(y, text)
- local length = string.len(text)
- term.setCursorPos(math.floor(WIDTH / 2 - length / 2), y)
- write(text)
- end
- ---@param x number
- ---@param y number
- ---@param text string
- local function writeRight(x, y, text)
- local length = string.len(text)
- term.setCursorPos(x - length, y)
- write(text)
- end
- local function displayRecords()
- term.clear()
- term.setTextColor(colors.purple)
- writeCenter(1, "Jukebox")
- local currentPage = math.floor(selection / PAGE_SIZE)
- for offset = 1, PAGE_SIZE do
- local recordIndex = currentPage * PAGE_SIZE + offset
- local recordInfo = RECORDS[recordIndex]
- if not recordInfo then
- break
- end
- local name = recordInfo[1]
- term.setTextColor(colors.lightGray)
- if selection == recordIndex then
- term.setTextColor(colors.white)
- name = "> " .. name
- end
- name = string.format("%d. %s", offset, name)
- term.setCursorPos(2, offset + 2)
- write(name)
- end
- local playingRecord = RECORDS[playing]
- local playingName = playingRecord and playingRecord[1] or "None"
- term.setTextColor(colors.lightGray)
- writeRight(WIDTH, 2, string.format("Playing: %s", playingName))
- writeRight(WIDTH, HEIGHT, string.format("P. %d/%d", currentPage + 1, PAGE_COUNT))
- end
- displayRecords()
- while true do
- local _, key = os.pullEvent("key")
- if key == keys.up then
- selection = math.max(selection - 1, 1)
- elseif key == keys.down then
- selection = math.min(selection + 1, #RECORDS)
- elseif key == keys.enter then
- local info = RECORDS[selection]
- if SPEAKER.playSound(info[2], 2, 1) then
- playing = selection
- end
- end
- displayRecords()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement