drpepper240

turtle jukebox v1

Jul 9th, 2022 (edited)
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1. -- Turtle jukebox v1 by DrPepper
  2. -- Needs a turtle and a drive from Computercraft, the former placed above the latter
  3. -- Plays all musical disks loaded into the turtle inventory in random order
  4.  
  5. local ScanInventory, FindDrive, TryEmptyDrive, LoadDisk, PlayRandom
  6. local records, drive, currentSong, disksTotal
  7. local recordLengths = {}
  8. recordLengths["13"]         = 178
  9. recordLengths["cat"]        = 185
  10. recordLengths["blocks"]     = 345
  11. recordLengths["chirp"]      = 185
  12. recordLengths["far"]        = 174
  13. recordLengths["mall"]       = 197
  14. recordLengths["mellohi"]    = 96
  15. recordLengths["stal"]       = 150
  16. recordLengths["strad"]      = 188
  17. recordLengths["ward"]       = 251
  18. recordLengths["11"]         = 71
  19. recordLengths["wait"]       = 238
  20. recordLengths["otherside"]  = 195
  21. recordLengths["5"]          = 178
  22. recordLengths["pigstep"]    = 148
  23. local pause = 5
  24.  
  25. function ScanInventory()
  26.     records = {}
  27.     disksTotal = 0
  28.     for i = 1, 16 do
  29.         local res = turtle.getItemDetail(i, true)
  30.         if res ~= nil then
  31.             if res.name ~= nil and string.match(tostring(res.name), "minecraft.record") then
  32.                 records[i] = string.sub(tostring(res.name), 18)
  33.                 disksTotal = disksTotal + 1
  34.             else
  35.             turtle.select(i)
  36.             turtle.dropUp()
  37.             end
  38.         end
  39.     end
  40.     if disksTotal == 0 then
  41.         print("Add some music disks")
  42.     end
  43. end
  44.  
  45. function FindDrive()
  46.     if peripheral.getType("bottom") == "drive" then
  47.         drive = peripheral.wrap("bottom")
  48.     else
  49.         print("No drive below turtle")
  50.         return false
  51.     end
  52. end
  53.  
  54. function TryEmptyDrive()
  55.     if records == nil then
  56.         print("Run ScanInventory() first")
  57.         return false
  58.     end
  59.     if drive == nil then
  60.         print("Run FindDrive() first")
  61.         return false
  62.     end
  63.     if not drive.isDiskPresent() then
  64.         return true
  65.     end
  66.     local slot
  67.     for i = 1, 16 do
  68.         if records[i] == nil then
  69.             slot = i
  70.             break
  71.         end
  72.     end
  73.     if slot == nil then
  74.         print("No empty slots")
  75.         return false
  76.     end
  77.     turtle.select(slot)
  78.     turtle.suckDown()
  79. end
  80.  
  81. function TryLoadDisk(slot)
  82.     if records == nil then
  83.         print("Run ScanInventory() first")
  84.         return false
  85.     end
  86.     currentSong = records[slot]
  87.     if drive == nil then
  88.         print("Run FindDrive() first")
  89.         return false
  90.     end
  91.     if drive.isDiskPresent() then
  92.         print("Run TryEmptyDrive() first")
  93.         return false
  94.     end
  95.     turtle.select(slot)
  96.     turtle.dropDown()
  97.     drive.playAudio()
  98.     if recordLengths[currentSong] ~= nil then
  99.         print(string.format("Playing \"%s\", %d seconds", records[slot], recordLengths[currentSong] ))
  100.         sleep(recordLengths[currentSong] + pause)
  101.     end
  102. end
  103.  
  104. function PlayRandom()
  105.     if records == nil or disksTotal == nil then
  106.         print("Run ScanInventory() first")
  107.         return false
  108.     end
  109.     local rand = math.random(1, disksTotal)
  110.     print ("Rand: " .. rand .. " of " .. disksTotal)
  111.     local slot
  112.     for i = 1, 16 do
  113.         if records[i] ~= nil then
  114.             turtle.select(i)
  115.             sleep(0.5)
  116.             rand = rand - 1
  117.             if rand == 0 then
  118.                 slot = i
  119.                 break
  120.             end
  121.         end
  122.     end
  123.     if slot == nil then
  124.         print("WTF")
  125.         return false
  126.     end
  127.     TryLoadDisk(slot)
  128.     ScanInventory()
  129.     TryEmptyDrive()
  130.     ScanInventory()
  131. end
  132.  
  133. if not turtle then
  134.     print("You need a turtle to run this")
  135.     return
  136. end
  137.  
  138. ScanInventory()
  139. FindDrive()
  140. TryEmptyDrive()
  141. ScanInventory()
  142. while true do
  143.     PlayRandom()
  144. end
  145.  
  146.  
Add Comment
Please, Sign In to add comment