Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle jukebox v1 by DrPepper
- -- Needs a turtle and a drive from Computercraft, the former placed above the latter
- -- Plays all musical disks loaded into the turtle inventory in random order
- local ScanInventory, FindDrive, TryEmptyDrive, LoadDisk, PlayRandom
- local records, drive, currentSong, disksTotal
- local recordLengths = {}
- recordLengths["13"] = 178
- recordLengths["cat"] = 185
- recordLengths["blocks"] = 345
- recordLengths["chirp"] = 185
- recordLengths["far"] = 174
- recordLengths["mall"] = 197
- recordLengths["mellohi"] = 96
- recordLengths["stal"] = 150
- recordLengths["strad"] = 188
- recordLengths["ward"] = 251
- recordLengths["11"] = 71
- recordLengths["wait"] = 238
- recordLengths["otherside"] = 195
- recordLengths["5"] = 178
- recordLengths["pigstep"] = 148
- local pause = 5
- function ScanInventory()
- records = {}
- disksTotal = 0
- for i = 1, 16 do
- local res = turtle.getItemDetail(i, true)
- if res ~= nil then
- if res.name ~= nil and string.match(tostring(res.name), "minecraft.record") then
- records[i] = string.sub(tostring(res.name), 18)
- disksTotal = disksTotal + 1
- else
- turtle.select(i)
- turtle.dropUp()
- end
- end
- end
- if disksTotal == 0 then
- print("Add some music disks")
- end
- end
- function FindDrive()
- if peripheral.getType("bottom") == "drive" then
- drive = peripheral.wrap("bottom")
- else
- print("No drive below turtle")
- return false
- end
- end
- function TryEmptyDrive()
- if records == nil then
- print("Run ScanInventory() first")
- return false
- end
- if drive == nil then
- print("Run FindDrive() first")
- return false
- end
- if not drive.isDiskPresent() then
- return true
- end
- local slot
- for i = 1, 16 do
- if records[i] == nil then
- slot = i
- break
- end
- end
- if slot == nil then
- print("No empty slots")
- return false
- end
- turtle.select(slot)
- turtle.suckDown()
- end
- function TryLoadDisk(slot)
- if records == nil then
- print("Run ScanInventory() first")
- return false
- end
- currentSong = records[slot]
- if drive == nil then
- print("Run FindDrive() first")
- return false
- end
- if drive.isDiskPresent() then
- print("Run TryEmptyDrive() first")
- return false
- end
- turtle.select(slot)
- turtle.dropDown()
- drive.playAudio()
- if recordLengths[currentSong] ~= nil then
- print(string.format("Playing \"%s\", %d seconds", records[slot], recordLengths[currentSong] ))
- sleep(recordLengths[currentSong] + pause)
- end
- end
- function PlayRandom()
- if records == nil or disksTotal == nil then
- print("Run ScanInventory() first")
- return false
- end
- local rand = math.random(1, disksTotal)
- print ("Rand: " .. rand .. " of " .. disksTotal)
- local slot
- for i = 1, 16 do
- if records[i] ~= nil then
- turtle.select(i)
- sleep(0.5)
- rand = rand - 1
- if rand == 0 then
- slot = i
- break
- end
- end
- end
- if slot == nil then
- print("WTF")
- return false
- end
- TryLoadDisk(slot)
- ScanInventory()
- TryEmptyDrive()
- ScanInventory()
- end
- if not turtle then
- print("You need a turtle to run this")
- return
- end
- ScanInventory()
- FindDrive()
- TryEmptyDrive()
- ScanInventory()
- while true do
- PlayRandom()
- end
Add Comment
Please, Sign In to add comment