Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@diagnostic disable: undefined-field, need-check-nil (ignore this, just from a thing I use in vsc)
- local speaker = peripheral.find("speaker")
- term.clear()
- term.setCursorPos(1, 1)
- local volume = 1
- local pitch = 1
- local RECORDS = {
- { "11", colors.brown },
- { "13", colors.red },
- { "blocks", colors.orange },
- { "cat", colors.yellow },
- { "chirp", colors.lime },
- { "far", colors.green },
- { "mall", colors.cyan },
- { "mellohi", colors.lightBlue },
- { "pigstep", colors.blue },
- { "stal", colors.purple },
- { "strad", colors.magenta },
- { "wait", colors.pink },
- { "ward", colors.red },
- { "otherside", colors.orange }
- }
- local MENU = {
- { "RECORDS", colors.red },
- { "SAVES", colors.orange },
- { "SAVE A JSON ID", colors.yellow },
- { "VOLUME", colors.lime },
- { "PITCH", colors.green },
- { "PLAY JSON ID", colors.cyan },
- { "STOP", colors.blue },
- { "REMOVE SAVES", colors.lightBlue }
- }
- local function isValidId(id)
- --local success = pcall(function()
- -- speaker.playSound(id, 0, 0)
- -- speaker.stop()
- --end)
- return true
- end
- local function promptInput(promptText)
- term.write(promptText)
- return read()
- end
- local function printcenter(y, text, color)
- local x = math.floor((26 - string.len(text)) / 2)
- term.setTextColor(tonumber(color) or colors.white)
- term.setCursorPos(x, y)
- term.clearLine()
- term.write(text)
- end
- local function playSound(sound_jsonid, l_pitch, l_volume)
- if sound_jsonid and sound_jsonid ~= nil then
- speaker.playSound(sound_jsonid, tonumber(l_volume) or tonumber(volume), tonumber(l_pitch) or tonumber(pitch))
- end
- end
- function colors.random()
- local rand = math.random(1, 16)
- local c = colors.white
- local cols = {}
- for i, color in pairs(colors) do
- if type(color) == "number" then
- table.insert(cols, color)
- end
- end
- return cols[rand]
- end
- local function createSaveFile()
- if not fs.exists("saved.txt") then
- local file = fs.open("saved.txt", "w")
- file.write("")
- file.close()
- return true
- else
- return false
- end
- end
- local function clearSaveFile()
- if fs.exists("saved.txt") then
- local file = fs.open("saved.txt", "w")
- file.write("")
- file.close()
- return true
- else
- createSaveFile()
- return false
- end
- end
- local function addToSaveFile(saveElement)
- if fs.exists("saved.txt") then
- local id, name, color = saveElement[1], saveElement[2], saveElement[3]
- if color == colors.black then
- color = colors.white
- end
- local file = fs.open("saved.txt", "a")
- file.write(name .. "\n" .. color .. "\n" .. id .. "\n\n")
- file.close()
- return true
- else
- createSaveFile()
- return false
- end
- end
- local function readSaveFile()
- if fs.exists("saved.txt") then
- local file = fs.open("saved.txt", "r")
- local saved = {}
- local i = 1
- repeat
- local name = file.readLine()
- local color = file.readLine()
- local id = file.readLine()
- file.readLine()
- if id then
- saved[i] = { name, color, id }
- end
- i = i + 1
- until id == nil or name == nil or color == nil
- file.close()
- return saved
- else
- createSaveFile()
- return false
- end
- end
- local function createCustomSavesDir()
- if not fs.exists("custom") then
- fs.makeDir("custom")
- end
- end
- local function readCustomSavesFile()
- if fs.exists("custom") then
- local saved = fs.list("custom")
- for i, file in pairs(saved) do
- saved[i] = { file, colors.random() }
- end
- return saved
- else
- createCustomSavesDir()
- end
- end
- -- ex list element; {text,color}
- local function optionSelect(list, default, yoff, toptext, topcolor, exitkey, breakOnSelect, callback, keycallback)
- printcenter(1, toptext, topcolor)
- if exitkey then
- printcenter(19, "press " .. exitkey .. " to exit.")
- end
- local currentSelected = math.min(default, #list) or 1
- local lastSelected = math.min(default, #list) or 1
- for i, element in pairs(list) do
- local text, color = element[1], element[2]
- printcenter(i + yoff, i == currentSelected and "> " .. text .. " <" or text,
- i == currentSelected and colors.white or color)
- end
- while true do
- local _, keycode = os.pullEvent("key")
- local key = keys.getName(keycode) or "null"
- lastSelected = currentSelected
- if key == "down" then
- currentSelected = currentSelected + 1
- if currentSelected > #list then
- currentSelected = 1
- end
- elseif key == "up" then
- currentSelected = currentSelected - 1
- if currentSelected < 1 then
- currentSelected = #list
- end
- end
- if key == "down" or key == "up" then
- local currentElement = list[currentSelected]
- local lastElement = list[lastSelected]
- if lastElement and currentElement then
- local cname, _ = currentElement[1], currentElement[2]
- local lname, lcolor = lastElement[1], lastElement[2]
- printcenter(currentSelected + yoff, "> " .. cname .. " <", colors.white)
- printcenter(lastSelected + yoff, lname, lcolor)
- end
- end
- if key == "enter" then
- playSound("block.lever.click", 1, 0.3)
- sleep(0.2)
- callback(currentSelected, list[currentSelected])
- if breakOnSelect then
- break
- end
- end
- if key == exitkey then
- term.clear()
- break
- end
- if keycallback then
- pcall(keycallback, key)
- end
- end
- end
- local menuCurrent = 1
- local savedCurrent = 1
- while true do
- optionSelect(MENU, menuCurrent, 2, "Menu Options", colors.pink, nil, true, function(selected, element)
- menuCurrent = selected
- if selected == 1 then
- optionSelect(RECORDS, 1, 2, "All Records", colors.green, "x", false, function(selected, element)
- playSound("music_disc." .. element[1])
- end)
- elseif selected == 2 then
- if not fs.exists("saved.txt") then
- createSaveFile()
- end
- local saved = readSaveFile()
- term.clear()
- term.setCursorPos(1, 1)
- if #saved == 0 then
- printcenter(13, "No saves detected", colors.red)
- printcenter(19, "press any key to exit.")
- end
- if saved ~= nil and #saved > 0 then
- optionSelect(saved, 1, 2, "All Saves", colors.lime, "x", false, function(selected, element)
- playSound(element[3])
- end)
- end
- if #saved == 0 then
- os.pullEvent("key")
- term.clear()
- end
- elseif selected == 3 then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- local id = promptInput("Input json id: ")
- local name = promptInput("Input name: ")
- if isValidId(id) then
- term.clear()
- printcenter(5, "Saved", colors.lime)
- printcenter(6, id, colors.green)
- printcenter(7, "as", colors.lime)
- printcenter(8, name, colors.green)
- addToSaveFile({ id, name, colors.random() })
- sleep(1)
- else
- term.clear()
- printcenter(6, "not a valid id!", colors.red)
- sleep(1)
- end
- elseif selected == 4 then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- volume = promptInput("Input volume: ")
- elseif selected == 5 then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- pitch = promptInput("Input pitch: ")
- elseif selected == 6 then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- local id = promptInput("Input json id: ")
- playSound(id)
- elseif selected == 7 then
- elseif selected == 8 then
- term.clear()
- optionSelect({ { "Yes", colors.green }, { "No", colors.red } }, 1, 3, "Are you sure?", colors.blue, nil, true
- , function(selected, element)
- if selected == 1 then
- clearSaveFile()
- term.clear()
- printcenter(7, "Wiped Save File", colors.red)
- speaker.playNote("bit", 2, 24)
- sleep(0.1)
- speaker.playNote("bit", 2, 20)
- sleep(0.11)
- speaker.playNote("bit", 1, 16)
- sleep(1)
- end
- end)
- end
- end)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement