Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ---@diagnostic disable: undefined-field
- local speaker = peripheral.find("speaker")
- term.clear()
- term.setCursorPos(1,1)
- local function promptInput(promptText)
- term.write(promptText)
- return read()
- end
- local volume = 1
- local pitch = 1
- local function playSound(soundPath)
- if soundPath and soundPath ~= "" then
- speaker.playSound(soundPath,tonumber(volume) or 1,tonumber(pitch) or 1)
- end
- end
- 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}
- }
- local function isARecord(str)
- for i,record in pairs(RECORDS) do
- if str == record[1] then
- return true, "music_disc." .. str
- end
- end
- return false
- end
- local function printCenter(y, str, col)
- term.setTextColor(col or colors.white)
- local x = math.floor((26 - string.len(str)) / 2)
- term.setCursorPos(x,y)
- term.clearLine()
- term.write(str)
- end
- local function resetTerm()
- term.setCursorPos(1,1)
- term.clear()
- term.setTextColor(colors.white)
- 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()
- end
- end
- local function resetSaveFile()
- if not fs.exists("saved.txt") then
- createSaveFile()
- else
- local file = fs.open("saved.txt","w")
- file.write("")
- file.close()
- end
- end
- local function addJsonIdToSaveFile(id,name)
- if not fs.exists("saved.txt") then
- createSaveFile()
- addJsonIdToSavedFile(id,name)
- else
- local file = fs.open("saved.txt","a")
- file.write(id .. "\n" .. name .. "\n" .. colors.random() .. "\n")
- file.close()
- end
- end
- local function readSaveFile()
- if not fs.exists("saved.txt") then
- createSaveFile()
- readSavedFile()
- else
- local file = fs.open("saved.txt","r")
- local saved = {}
- local i = 1
- repeat
- local id = file.readLine()
- local name = file.readLine()
- local color = file.readLine()
- if id then
- saved[i] = {Id = id, Name = name, Color = color}
- end
- i = i + 1
- until id == nil or name == nil or color == nil
- file.close()
- return saved
- end
- end
- local function reconstructSaveFile(saveTable)
- if not fs.exists("saved.txt") then
- createSaveFile()
- else
- resetSaveFile()
- local file = fs.open("saved.txt","a")
- for _,save in pairs(saveTable) do
- file.write(save.Id .. "\n" .. save.Name .. "\n" .. save.Color .. "\n")
- end
- file.close()
- end
- end
- local function List()
- term.clear()
- printCenter(10,"Press up or down arrow")
- printCenter(11,"to continue")
- local currentSelected = 1
- while true do
- local event, key = os.pullEvent("key_up")
- local name = keys.getName(key) or "not a key"
- if name == "down" then
- currentSelected = currentSelected + 1
- if currentSelected > #RECORDS then
- currentSelected = 1
- end
- elseif name == "up" then
- currentSelected = currentSelected - 1
- if currentSelected < 1 then
- currentSelected = #RECORDS
- end
- end
- if name == "down" or name == "up" then
- resetTerm()
- printCenter(1,"All records: ")
- for i,record in pairs(RECORDS) do
- if i == currentSelected then
- printCenter(i+2,"> " .. record[1] .. " <")
- else
- printCenter(i+2,record[1],record[2])
- end
- end
- printCenter(19,"Press x to exit.")
- elseif name == "enter" then
- speaker.playSound("block.comparator.click",1)
- sleep(0.2)
- playSound("music_disc." .. RECORDS[currentSelected][1])
- elseif name == "x" then
- break
- end
- end
- term.clear()
- term.setCursorPos(1,1)
- end
- function Saves()
- term.clear()
- local currentSelected = 1
- local currentPlaying = 1
- local SAVED = readSaveFile()
- if #SAVED > 0 then
- printCenter(9,"Press up or down arrow")
- printCenter(10,"to continue")
- printCenter(13,"You have " .. #SAVED .. " saved song(s)")
- while true do
- local event, key = os.pullEvent("key_up")
- local name = keys.getName(key) or "not a key"
- if name == "down" then
- currentSelected = currentSelected + 1
- if currentSelected > #SAVED then
- currentSelected = 1
- end
- elseif name == "up" then
- currentSelected = currentSelected - 1
- if currentSelected < 1 then
- currentSelected = #SAVED
- end
- end
- if name == "down" or name == "up" then
- resetTerm()
- local currentPlayingName = SAVED[currentPlaying] and SAVED[currentPlaying].Name or "null"
- printCenter(1," All Saves",colors.green)
- printCenter(2," Playing: " .. currentPlayingName,colors.red)
- for i,save in pairs(SAVED) do
- if i == currentSelected then
- printCenter(i+3,"> " .. save.Name .. " <",tonumber(save.Color))
- else
- printCenter(i+3,save.Name,tonumber(save.Color))
- end
- end
- printCenter(19," Press x to exit")
- printCenter(18," Press r to remove")
- elseif name == "enter" then
- speaker.playSound("block.comparator.click",0.4)
- sleep(0.2)
- currentPlaying = currentSelected
- currentPlayingName = SAVED[currentPlaying] and SAVED[currentPlaying].Name or "null"
- printCenter(2," Playing: " .. currentPlayingName,colors.red)
- playSound(SAVED[currentSelected].Id)
- elseif name == "r" then
- resetTerm()
- printCenter(6," Delete?",colors.red)
- printCenter(10,'"' .. SAVED[currentSelected].Name .. '"',colors.white)
- printCenter(13,"No")
- printCenter(14,"Yeah")
- local currentSelected2 = 1
- local options = {"No","Yeah"}
- while true do
- local event, key = os.pullEvent("key_up")
- local name = keys.getName(key) or "not a key"
- if name == "down" then
- currentSelected2 = currentSelected2 + 1
- if currentSelected2 > #options then
- currentSelected2 = 1
- end
- elseif name == "up" then
- currentSelected2 = currentSelected2 - 1
- if currentSelected2 < 1 then
- currentSelected2 = #options
- end
- end
- if name == "down" or name == "up" then
- resetTerm()
- printCenter(6, " Delete?",colors.red)
- printCenter(10,'"' .. SAVED[currentSelected].Name .. '"', colors.white)
- printCenter(13,"No")
- printCenter(14,"Yeah")
- for i, option in pairs(options) do
- if i == currentSelected2 then
- printCenter(i + 12,"> " .. option .. " <", colors.red)
- else
- printCenter(i + 12,option)
- end
- end
- elseif name == "enter" then
- if currentSelected2 == 2 then
- local newSAVED = {}
- for i, save in pairs(SAVED) do
- if not (save.Id == SAVED[currentSelected].Id and save.Name == SAVED[currentSelected].Name) then
- newSAVED[i] = save
- end
- end
- reconstructSaveFile(newSAVED)
- resetTerm()
- printCenter(10,"Deleted " .. SAVED[currentSelected].Name,colors.red)
- speaker.playNote("bit",1,24)
- sleep(0.15)
- speaker.playNote("bit",2,20)
- sleep(0.1)
- speaker.playNote("bit",2,16)
- sleep(1)
- resetTerm()
- SAVED = newSAVED
- printCenter(9,"Press up or down arrow")
- printCenter(10,"to continue")
- printCenter(13,"You have " .. #SAVED .. " saved song(s)")
- break
- else
- resetTerm()
- break
- end
- end
- end
- elseif name == "x" then
- break
- end
- end
- else
- printCenter(10," You don't have any saves",colors.red)
- sleep(2)
- end
- resetTerm()
- end
- function Save()
- resetTerm()
- local id = promptInput("Save id: ")
- local name = promptInput("Save name: ")
- addJsonIdToSaveFile(id,name)
- play = ""
- resetTerm()
- printCenter(10,"Saved " .. id,colors.lime)
- printCenter(11,"as " .. name, colors.lime)
- speaker.playNote("bit",1,16)
- sleep(0.15)
- speaker.playNote("bit",2,20)
- sleep(0.1)
- speaker.playNote("bit",2,24)
- sleep(1)
- resetTerm()
- end
- local currentSelected = 1
- local options = {{Name = "Records",Color = colors.red},{Name = "Saves",Color = colors.orange},{Name = "Save a song",Color = colors.yellow},{Name = "Volume", Color = colors.green},{Name = "Pitch", Color = colors.lime},{Name = "Manual Input",Color = colors.blue},{Name = "Stop",Color = colors.lightBlue}}
- printCenter(9,"If the screen is")
- printCenter(10,"just black then")
- printCenter(11,"press up or down")
- while true do
- local event, key = os.pullEvent("key_up")
- local name = keys.getName(key) or "not a key"
- if name == "down" then
- currentSelected = currentSelected + 1
- if currentSelected > #options then
- currentSelected = 1
- end
- elseif name == "up" then
- currentSelected = currentSelected - 1
- if currentSelected < 1 then
- currentSelected = #options
- end
- end
- if name == "down" or name == "up" then
- resetTerm()
- printCenter(1,"Player Options")
- for i,option in pairs(options) do
- if i == currentSelected then
- printCenter(i+2,"> " .. option.Name .. " <")
- else
- printCenter(i+2,option.Name,option.Color)
- end
- end
- elseif name == "enter" then
- if currentSelected == 1 then
- List()
- elseif currentSelected == 2 then
- Saves()
- elseif currentSelected == 3 then
- Save()
- elseif currentSelected == 4 then
- resetTerm()
- volume = promptInput("Enter volume: ")
- currentSelected = 0
- resetTerm()
- elseif currentSelected == 5 then
- resetTerm()
- pitch = promptInput("Enter pitch: ")
- currentSelected = 0
- resetTerm()
- end
- end
- playSound(play)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement