Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- xo = -2501
- yo= 64
- zo = 2637
- os.loadAPI("gcapi")
- sensor = peripheral.find("openperipheral_sensor")
- --------------------------------------------
- --> Sensor <--
- --------------------------------------------
- function updateTableWithSensor(dataPlayers)
- --{"name", "x", "y", "z", "world", "updated_at"}
- for i=1,#dataPlayers do
- player_data = dataPlayers[i]
- player_name = player_data["name"]
- sensor_data = sensor.getPlayerData(player_name)
- if sensor_data then
- dataPlayers[i].x = math.floor(xo + sensor_data.position.x)
- dataPlayers[i].y = math.floor(yo + sensor_data.position.y)
- dataPlayers[i].z = math.floor(zo + sensor_data.position.z)
- dataPlayers[i].world = "world"
- dataPlayers[i].updated_at = "Updated now"
- end
- end
- return dataPlayers
- end
- --------------------------------------------------------
- --> MUSIC <--
- --------------------------------------------------------
- local pitchs = {[0]=0, [1]=0.53, [2]=0.56, [3]=0.6, [4]=0.63, [5]=0.67, [6]=0.7, [7]=0.75,
- [8]=0.8,[9]=0.85,[10]=0.9,[11]=0.95,[12]=1,[13]=1.05,[14]=1.1,[15]=1.2,[16]=1.25,[17]=1.32,[18]=1.4,
- [19]=1.5,[20]=1.6,[21]=1.7,[22]=1.8,[23]=1.9,[24]=2}
- local instruments = {[1]="note.harp",[2]="note.bassattack",[3]="note.bd",[4]="note.snare",[5]="note.hat"}
- local playerPositions = {}
- local playersInRadio = {}
- local vol = 0.7
- local song = nil
- local noteblock = peripheral.find("music")
- function playNote(instrument, pitch)
- for _,playerData in pairs(playerPositions) do
- --if playersInRadio[playerData["name"]] then
- local x = playerData["x"]
- local y = playerData["y"]
- local z = playerData["z"]
- noteblock.playSound(instrument, pitch, vol, x-xo, y-yo+4, z-zo)
- --end
- end
- end
- function getPlayersInRadio()
- local players = {}
- for i, playerData in pairs(playerPositions) do
- local player = playerData["name"]
- if playersInRadio[player] then
- table.insert(players, playerData)
- end
- end
- return players
- end
- function removePlayerFromRadio(player)
- playersInRadio[player] = nil
- end
- function addPlayerToRadio(player)
- playersInRadio[player] = true
- end
- function addAllPlayers()
- for i, playerData in pairs(playerPositions) do
- local player = playerData["name"]
- playersInRadio[player] = true
- end
- end
- --------------------------------------------------------
- --> Commands <--
- --------------------------------------------------------
- function hasPermissions(player)
- return gcapi.hasPermissions(player)
- end
- function executeCommand(player, co, isFromChat)
- local co = (co .. " ")
- if isFromChat then
- co = co:sub(2)
- end
- local command = gcapi.split(co .. " ", " ")
- if not command[1] then
- return nil
- end
- if command[1] == "music" and hasPermissions(player) then
- if command[2] then
- filePath = "/songs/" .. command[2]
- if fs.exists(filePath) then
- file = fs.open(filePath, "r")
- song = textutils.unserialize(file.readAll())
- file.close()
- print("Playing " .. command[2] .. "...")
- else
- print("Song not found!")
- end
- end
- return
- end
- if string.lower(command[1]) == "stopmusic" and hasPermissions(player) then
- endSong = true
- return
- end
- if string.lower(command[1]) == "addall" and hasPermissions(player) then
- addAllPlayers()
- print("Players added!")
- return
- end
- if string.lower(command[1]) == "joinmusic" then
- addPlayerToRadio(player)
- return
- end
- if string.lower(command[1]) == "mutemusic" then
- removePlayerFromRadio(player)
- return
- end
- if string.lower(command[1]) == "download" and hasPermissions(player) then
- if command[2] and command[3] then
- local name = "/songs/" .. command[3]
- local code = command[2]
- fs.delete(name)
- shell.run("pastebin get " .. code .. " " .. name)
- end
- return
- end
- if string.lower(command[1]) == "addplayer" and command[2] and hasPermissions(player) then
- addPlayerToRadio(command[2])
- return
- end
- if string.lower(command[1]) == "vol" and command[2] and hasPermissions(player) then
- local newvol = tonumber(command[2])
- if newvol and newvol <= 1 and newvol >= 0 then
- vol = newvol
- end
- return
- end
- if string.lower(command[1]) == "update" and hasPermissions(player) then
- error()
- return
- end
- if not hasPermissions(player) then
- return
- end
- local instrument = command[1]
- local pitch = 1
- if tonumber(command[2]) then
- pitch = tonumber(command[2])
- end
- local vol = 1
- if tonumber(command[3]) then
- vol = tonumber(command[3])
- end
- for _,playerData in pairs(playerPositions) do
- local x = playerData["x"]
- local y = playerData["y"]
- local z = playerData["z"]
- noteblock.playSound(instrument, pitch, vol, x-xo, y-yo+4, z-zo)
- end
- end
- --------------------------------------------------------
- --> Main programs <--
- --------------------------------------------------------
- function updaters()
- parallel.waitForAny(updatePositions, listener)
- end
- function listener()
- sleep(2)
- while true do
- tEvent = {os.pullEvent()}
- if tEvent[1] == "chatEvent" then
- local player = tEvent[2]
- local co = tEvent[3]
- executeCommand(player, co, true)
- print(player .. ": " .. co)
- end
- if tEvent[1] == "chat_command" then
- local co = tostring(tEvent[2])
- local player = tostring(tEvent[3])
- executeCommand(player, co)
- print(player .. ": " .. co)
- end
- end
- end
- function updatePositions()
- while true do
- playerPositions = gcapi.getPosPlayers()
- if sensor then
- playerPositions = updateTableWithSensor(playerPositions)
- end
- sleep(1)
- end
- end
- function updateSong()
- while true do
- if song then
- delay = song["delay"]
- for _,tick in ipairs(song) do
- local players = getPlayersInRadio()
- for instrumentID, notePitchs in pairs(tick) do
- for _,pitchID in pairs(notePitchs) do
- playNote(instruments[instrumentID], pitchs[pitchID])
- end
- end
- if endSong then
- break
- end
- sleep(delay)
- end
- song = nil
- end
- endSong = false
- sleep(2)
- end
- end
- function startProgram()
- parallel.waitForAny(updaters, updateSong)
- end
- parallel.waitForAny(gcapi.startChatEventQueue, startProgram)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement