Hydrotronics

ComputerCraft christmas music

Nov 20th, 2020 (edited)
2,530
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.63 KB | None | 0 0
  1. _G["HyTunes"] = {}
  2. HyTunes["speaker"] = peripheral.find("speaker")
  3. if HyTunes.speaker == null then
  4.     print("Could not find a speaker!")
  5.     return
  6. end
  7. HyTunes["songs"] = {}
  8. HyTunes["Volume"] = 1
  9. HyTunes.songs[1] = {
  10.         ["title"] = "Padoru",
  11.         ["track"] = "minecraft:music_disc.11",
  12.         ["artist"] = "Senzawa"
  13.     }
  14. HyTunes.songs[2] = {
  15.         ["title"] = "Feliz Navidad",
  16.         ["track"] = "minecraft:music_disc.far",
  17.         ["artist"] = "Jose Faliciano"
  18.     }
  19. HyTunes.songs[3] = {
  20.         ["title"] = "Last Christmas",
  21.         ["track"] = "minecraft:music_disc.mall",
  22.         ["artist"] = "Wham!"
  23.     }
  24. HyTunes.songs[4] = {
  25.         ["title"] = "Carol of the Bells",
  26.         ["track"] = "minecraft:music_disc.wait",
  27.         ["artist"] = "Some choir Archie found"
  28.     }
  29. HyTunes.songs[5] = {
  30.         ["title"] = "Beginning to Look a Lot like Christmas",
  31.         ["track"] = "minecraft:music_disc.chirp",
  32.         ["artist"] = "Michael Bubble"
  33.     }
  34. HyTunes.songs[6] = {
  35.         ["title"] = "Rubber Tree",
  36.         ["track"] = "minecraft:music_disc.13",
  37.         ["artist"] = "Duncan Lalna"
  38.     }
  39. HyTunes.songs[7] = {
  40.         ["title"] = "Jingle Bells",
  41.         ["track"] = "minecraft:music_disc.stal",
  42.         ["artist"] = "Kidz Music Official"
  43.     }
  44. HyTunes.songs[8] = {
  45.         ["title"] = "Christmas Time",
  46.         ["track"] = "minecraft:music_disc.pigstep",
  47.         ["artist"] = "The Darkness"
  48.     }
  49. HyTunes.songs[9] = {
  50.         ["title"] = "Christmas Tree",
  51.         ["track"] = "minecraft:music_disc.cat",
  52.         ["artist"] = "Crouch-End Festival Chorus"
  53.     }
  54. HyTunes.songs[10] = {
  55.         ["title"] = "We wish you a merry Christmas",
  56.         ["track"] = "minecraft:music_disc.blocks",
  57.         ["artist"] = "Some Orchestra Archie found"
  58.     }
  59. HyTunes.songs[11] = {
  60.         ["title"] = "Fairytale of New York",
  61.         ["track"] = "minecraft:music_disc.strad",
  62.         ["artist"] = "Pogues"
  63.     }
  64. HyTunes.songs[12] = {
  65.         ["title"] = "The Christmas Song",
  66.         ["track"] = "minecraft:music_disc.ward",
  67.         ["artist"] = "Nat King Cole"
  68. }
  69.  
  70. HyTunes["currentSong"] = HyTunes.songs[1]
  71.  
  72. HyTunes["termSize"] = { term.getSize() }
  73.  
  74. function renderList()
  75.     term.setBackgroundColor(colors.lightGray)
  76.     term.clear()
  77.    
  78.     paintutils.drawLine(1,1,HyTunes.termSize[1],1,colors.magenta)
  79.     term.setCursorPos(1,1)
  80.     term.setTextColor(colors.black)
  81.     write("HyTunes 0.5")
  82.     for i=1,HyTunes.termSize[2]-1 do
  83.         local color = i % 2 == 1 and colors.cyan or colors.lightBlue
  84.         paintutils.drawLine(1,i+1,HyTunes.termSize[1],i+1,color)
  85.     end
  86.  
  87.     term.setCursorPos(1,2)
  88.     color = colors.cyan
  89.     for i = 1,#HyTunes.songs do
  90.         name = HyTunes.songs[i].title
  91.         if #name > HyTunes.termSize[1] then
  92.             print(name:sub(1,HyTunes.termSize[1] - 3).."...")
  93.         else
  94.             print(name)
  95.         end
  96.         color = color == colors.lightBlue and colors.cyan or colors.lightBlue
  97.         term.setBackgroundColor(color)
  98.     end
  99.    
  100.     term.setCursorPos(HyTunes.termSize[1]-2,1)
  101.     for i = 1,3 do
  102.         if HyTunes.Volume == i then
  103.             term.setBackgroundColor(colors.pink)
  104.         else
  105.             term.setBackgroundColor(colors.magenta)
  106.         end
  107.         write(i)
  108.     end
  109.    
  110.     HyTunes.currentHandler = handleList
  111. end
  112.  
  113. function breakIntoLines(text)
  114.     if #text > HyTunes.termSize[1] then
  115.         words = {}
  116.         for word in text:gmatch("%w+") do table.insert(words,word) end
  117.         lines = {}
  118.         line = ""
  119.         for _,word in pairs(words) do
  120.             if #line + #word > HyTunes.termSize[1] then
  121.                 table.insert(lines, line)
  122.                 line = word
  123.             else
  124.                 if #line > 0 then line = line.." "..word else line = word end
  125.             end
  126.         end
  127.         if #line > 0 then
  128.             table.insert(lines, line)
  129.         end
  130.         return lines
  131.     else
  132.         return { text }
  133.     end
  134. end
  135.  
  136. function renderMusic()
  137.     color = colors.cyan
  138.     term.setBackgroundColor(colors.cyan)
  139.     term.clear()
  140.     lines = breakIntoLines(HyTunes.currentSong.title)
  141.     titleWord = "Currently Playing"
  142.    
  143.     lineNum = ((HyTunes.termSize[2] / 2) - (#lines / 2)) - 3
  144.     term.setCursorPos((HyTunes.termSize[1] / 2) - (#titleWord / 2), lineNum - 1)
  145.     write(titleWord)
  146.     for _,line in pairs(lines) do
  147.         term.setCursorPos((HyTunes.termSize[1] / 2) - (#line / 2), lineNum)
  148.         write(line)
  149.         lineNum = lineNum + 1
  150.     end
  151.     lineNum = lineNum + 1
  152.     artistWord = "Artist"
  153.     lines = breakIntoLines(HyTunes.currentSong.artist)
  154.     for _,line in pairs(lines) do
  155.         term.setCursorPos((HyTunes.termSize[1] / 2) - (#line / 2), lineNum)
  156.         write(line)
  157.         lineNum = lineNum + 1
  158.     end
  159.    
  160.     paintutils.drawLine(1,HyTunes.termSize[2],HyTunes.termSize[1],HyTunes.termSize[2],colors.lightBlue)
  161.     term.setCursorPos(1,HyTunes.termSize[2])
  162.     write("Back")
  163.    
  164.     HyTunes.currentHandler = handleMusic
  165. end
  166.  
  167. function handleMusic()
  168.     local event, button, x, y = os.pullEvent("mouse_click")
  169.     if x < 5 and y == HyTunes.termSize[2] then
  170.         HyTunes.currentPage = renderList
  171.     end
  172. end
  173.  
  174. function handleList()
  175.     local event, button, x, y = os.pullEvent("mouse_click")
  176.     if y > 1 then
  177.         if HyTunes.songs[y-1] then
  178.             HyTunes.currentSong = HyTunes.songs[y-1]
  179.             HyTunes.speaker.playSound(HyTunes.currentSong.track, HyTunes.Volume)
  180.             HyTunes.currentPage = renderMusic
  181.         end
  182.     else
  183.         if x >= HyTunes.termSize[1] - 2 then
  184.             HyTunes.Volume = 3 - (HyTunes.termSize[1] - x)
  185.             term.setCursorPos(HyTunes.termSize[1]-2,1)
  186.             for i = 1,3 do
  187.                 if HyTunes.Volume == i then
  188.                     term.setBackgroundColor(colors.pink)
  189.                 else
  190.                     term.setBackgroundColor(colors.magenta)
  191.                 end
  192.                 write(i)
  193.             end
  194.         end
  195.     end
  196. end
  197.  
  198. HyTunes["currentPage"] = renderList
  199.  
  200. function doInteractions()
  201.     processedPage = HyTunes.currentPage
  202.     processedPage()
  203.     processedHandler = HyTunes.currentHandler
  204.     while true do
  205.         processedHandler()
  206.         if processedHandler ~= HyTunes.currentHandler then
  207.             processedHandler = HyTunes.currentHandler
  208.         end
  209.         if processedPage ~= HyTunes.currentPage then
  210.             processedPage = HyTunes.currentPage
  211.             processedPage()
  212.             processedHandler = HyTunes.currentHandler
  213.         end
  214.     end
  215. end
  216.  
  217. doInteractions()
Advertisement
Add Comment
Please, Sign In to add comment