Advertisement
HappySunChild

MP3 Player - 1.12.2

Aug 7th, 2022 (edited)
643
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.40 KB | None | 0 0
  1. ---@diagnostic disable: undefined-field, need-check-nil (ignore this, just from a thing I use in vsc)
  2. local speaker = peripheral.find("speaker")
  3.  
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6.  
  7. local volume = 1
  8. local pitch = 1
  9.  
  10. local RECORDS = {
  11.     { "11", colors.brown },
  12.     { "13", colors.red },
  13.     { "blocks", colors.orange },
  14.     { "cat", colors.yellow },
  15.     { "chirp", colors.lime },
  16.     { "far", colors.green },
  17.     { "mall", colors.cyan },
  18.     { "mellohi", colors.lightBlue },
  19.     { "pigstep", colors.blue },
  20.     { "stal", colors.purple },
  21.     { "strad", colors.magenta },
  22.     { "wait", colors.pink },
  23.     { "ward", colors.red },
  24.     { "otherside", colors.orange }
  25. }
  26.  
  27. local MENU = {
  28.     { "RECORDS", colors.red },
  29.     { "SAVES", colors.orange },
  30.     { "SAVE A JSON ID", colors.yellow },
  31.     { "VOLUME", colors.lime },
  32.     { "PITCH", colors.green },
  33.     { "PLAY JSON ID", colors.cyan },
  34.     { "STOP", colors.blue },
  35.     { "REMOVE SAVES", colors.lightBlue }
  36. }
  37.  
  38. local function isValidId(id)
  39.     --local success = pcall(function()
  40.     --    speaker.playSound(id, 0, 0)
  41.     --    speaker.stop()
  42.     --end)
  43.  
  44.     return true
  45. end
  46.  
  47. local function promptInput(promptText)
  48.     term.write(promptText)
  49.     return read()
  50. end
  51.  
  52. local function printcenter(y, text, color)
  53.     local x = math.floor((26 - string.len(text)) / 2)
  54.  
  55.     term.setTextColor(tonumber(color) or colors.white)
  56.     term.setCursorPos(x, y)
  57.     term.clearLine()
  58.     term.write(text)
  59. end
  60.  
  61. local function playSound(sound_jsonid, l_pitch, l_volume)
  62.     if sound_jsonid and sound_jsonid ~= nil then
  63.         speaker.playSound(sound_jsonid, tonumber(l_volume) or tonumber(volume), tonumber(l_pitch) or tonumber(pitch))
  64.     end
  65. end
  66.  
  67. function colors.random()
  68.     local rand = math.random(1, 16)
  69.     local c = colors.white
  70.     local cols = {}
  71.  
  72.     for i, color in pairs(colors) do
  73.         if type(color) == "number" then
  74.             table.insert(cols, color)
  75.         end
  76.     end
  77.  
  78.     return cols[rand]
  79. end
  80.  
  81. local function createSaveFile()
  82.     if not fs.exists("saved.txt") then
  83.         local file = fs.open("saved.txt", "w")
  84.         file.write("")
  85.         file.close()
  86.  
  87.         return true
  88.     else
  89.         return false
  90.     end
  91. end
  92.  
  93. local function clearSaveFile()
  94.     if fs.exists("saved.txt") then
  95.         local file = fs.open("saved.txt", "w")
  96.         file.write("")
  97.         file.close()
  98.  
  99.         return true
  100.     else
  101.         createSaveFile()
  102.  
  103.         return false
  104.     end
  105. end
  106.  
  107. local function addToSaveFile(saveElement)
  108.     if fs.exists("saved.txt") then
  109.         local id, name, color = saveElement[1], saveElement[2], saveElement[3]
  110.  
  111.         if color == colors.black then
  112.             color = colors.white
  113.         end
  114.  
  115.         local file = fs.open("saved.txt", "a")
  116.         file.write(name .. "\n" .. color .. "\n" .. id .. "\n\n")
  117.         file.close()
  118.  
  119.         return true
  120.     else
  121.         createSaveFile()
  122.  
  123.         return false
  124.     end
  125. end
  126.  
  127. local function readSaveFile()
  128.     if fs.exists("saved.txt") then
  129.         local file = fs.open("saved.txt", "r")
  130.         local saved = {}
  131.  
  132.         local i = 1
  133.         repeat
  134.             local name = file.readLine()
  135.             local color = file.readLine()
  136.             local id = file.readLine()
  137.             file.readLine()
  138.  
  139.             if id then
  140.                 saved[i] = { name, color, id }
  141.             end
  142.             i = i + 1
  143.         until id == nil or name == nil or color == nil
  144.  
  145.         file.close()
  146.         return saved
  147.     else
  148.         createSaveFile()
  149.  
  150.         return false
  151.     end
  152. end
  153.  
  154. local function createCustomSavesDir()
  155.     if not fs.exists("custom") then
  156.         fs.makeDir("custom")
  157.     end
  158. end
  159.  
  160. local function readCustomSavesFile()
  161.     if fs.exists("custom") then
  162.         local saved = fs.list("custom")
  163.  
  164.         for i, file in pairs(saved) do
  165.             saved[i] = { file, colors.random() }
  166.         end
  167.  
  168.         return saved
  169.     else
  170.         createCustomSavesDir()
  171.     end
  172. end
  173.  
  174. -- ex list element; {text,color}
  175.  
  176. local function optionSelect(list, default, yoff, toptext, topcolor, exitkey, breakOnSelect, callback, keycallback)
  177.     printcenter(1, toptext, topcolor)
  178.  
  179.     if exitkey then
  180.         printcenter(19, "press " .. exitkey .. " to exit.")
  181.     end
  182.  
  183.     local currentSelected = math.min(default, #list) or 1
  184.     local lastSelected = math.min(default, #list) or 1
  185.  
  186.     for i, element in pairs(list) do
  187.         local text, color = element[1], element[2]
  188.  
  189.         printcenter(i + yoff, i == currentSelected and "> " .. text .. " <" or text,
  190.             i == currentSelected and colors.white or color)
  191.     end
  192.  
  193.     while true do
  194.         local _, keycode = os.pullEvent("key")
  195.         local key = keys.getName(keycode) or "null"
  196.  
  197.         lastSelected = currentSelected
  198.  
  199.         if key == "down" then
  200.             currentSelected = currentSelected + 1
  201.  
  202.             if currentSelected > #list then
  203.                 currentSelected = 1
  204.             end
  205.         elseif key == "up" then
  206.             currentSelected = currentSelected - 1
  207.  
  208.             if currentSelected < 1 then
  209.                 currentSelected = #list
  210.             end
  211.         end
  212.  
  213.         if key == "down" or key == "up" then
  214.             local currentElement = list[currentSelected]
  215.             local lastElement = list[lastSelected]
  216.             if lastElement and currentElement then
  217.                 local cname, _ = currentElement[1], currentElement[2]
  218.                 local lname, lcolor = lastElement[1], lastElement[2]
  219.  
  220.                 printcenter(currentSelected + yoff, "> " .. cname .. " <", colors.white)
  221.                 printcenter(lastSelected + yoff, lname, lcolor)
  222.             end
  223.         end
  224.  
  225.         if key == "enter" then
  226.             playSound("block.lever.click", 1, 0.3)
  227.             sleep(0.2)
  228.             callback(currentSelected, list[currentSelected])
  229.  
  230.             if breakOnSelect then
  231.                 break
  232.             end
  233.         end
  234.  
  235.         if key == exitkey then
  236.             term.clear()
  237.             break
  238.         end
  239.  
  240.         if keycallback then
  241.             pcall(keycallback, key)
  242.         end
  243.     end
  244. end
  245.  
  246. local menuCurrent = 1
  247. local savedCurrent = 1
  248.  
  249. while true do
  250.     optionSelect(MENU, menuCurrent, 2, "Menu Options", colors.pink, nil, true, function(selected, element)
  251.         menuCurrent = selected
  252.  
  253.         if selected == 1 then
  254.             optionSelect(RECORDS, 1, 2, "All Records", colors.green, "x", false, function(selected, element)
  255.                 playSound("music_disc." .. element[1])
  256.             end)
  257.         elseif selected == 2 then
  258.             if not fs.exists("saved.txt") then
  259.                 createSaveFile()
  260.             end
  261.  
  262.             local saved = readSaveFile()
  263.  
  264.             term.clear()
  265.             term.setCursorPos(1, 1)
  266.  
  267.             if #saved == 0 then
  268.                 printcenter(13, "No saves detected", colors.red)
  269.                 printcenter(19, "press any key to exit.")
  270.             end
  271.  
  272.             if saved ~= nil and #saved > 0 then
  273.                 optionSelect(saved, 1, 2, "All Saves", colors.lime, "x", false, function(selected, element)
  274.                     playSound(element[3])
  275.                 end)
  276.             end
  277.  
  278.             if #saved == 0 then
  279.                 os.pullEvent("key")
  280.  
  281.                 term.clear()
  282.             end
  283.         elseif selected == 3 then
  284.             term.clear()
  285.             term.setCursorPos(1, 1)
  286.             term.setTextColor(colors.yellow)
  287.  
  288.             local id = promptInput("Input json id: ")
  289.             local name = promptInput("Input name: ")
  290.  
  291.             if isValidId(id) then
  292.                 term.clear()
  293.  
  294.                 printcenter(5, "Saved", colors.lime)
  295.                 printcenter(6, id, colors.green)
  296.                 printcenter(7, "as", colors.lime)
  297.                 printcenter(8, name, colors.green)
  298.  
  299.                 addToSaveFile({ id, name, colors.random() })
  300.  
  301.                 sleep(1)
  302.             else
  303.                 term.clear()
  304.  
  305.                 printcenter(6, "not a valid id!", colors.red)
  306.  
  307.                 sleep(1)
  308.             end
  309.         elseif selected == 4 then
  310.             term.clear()
  311.             term.setCursorPos(1, 1)
  312.             term.setTextColor(colors.yellow)
  313.  
  314.             volume = promptInput("Input volume: ")
  315.         elseif selected == 5 then
  316.             term.clear()
  317.             term.setCursorPos(1, 1)
  318.             term.setTextColor(colors.yellow)
  319.  
  320.             pitch = promptInput("Input pitch: ")
  321.         elseif selected == 6 then
  322.             term.clear()
  323.             term.setCursorPos(1, 1)
  324.             term.setTextColor(colors.yellow)
  325.  
  326.             local id = promptInput("Input json id: ")
  327.  
  328.             playSound(id)
  329.         elseif selected == 7 then
  330.  
  331.         elseif selected == 8 then
  332.             term.clear()
  333.             optionSelect({ { "Yes", colors.green }, { "No", colors.red } }, 1, 3, "Are you sure?", colors.blue, nil, true
  334.                 , function(selected, element)
  335.                 if selected == 1 then
  336.                     clearSaveFile()
  337.  
  338.                     term.clear()
  339.                     printcenter(7, "Wiped Save File", colors.red)
  340.  
  341.                     speaker.playNote("bit", 2, 24)
  342.                     sleep(0.1)
  343.                     speaker.playNote("bit", 2, 20)
  344.                     sleep(0.11)
  345.                     speaker.playNote("bit", 1, 16)
  346.  
  347.                     sleep(1)
  348.                 end
  349.             end)
  350.         end
  351.     end)
  352. end
  353.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement