Skip to content

Commit

Permalink
Add events and expose more song details through the API
Browse files Browse the repository at this point in the history
  • Loading branch information
FluxCapacitor2 committed Dec 22, 2023
1 parent d6461ce commit 3b2e9f0
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/main/kotlin/com/bluedragonmc/jukebox/JukeboxPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class JukeboxPlugin @Inject constructor(
.filter { path -> path.isRegularFile() && path.extension == "nbs" }
.map { path -> Song.load(path) }
.onEach {
logger.info("Loaded song \"${it.songName}\" by ${it.originalAuthor.ifEmpty { it.author } } (${it.getDuration()})")
logger.info("Loaded song \"${it.songName}\" by ${it.originalAuthor.ifEmpty { it.author }} (${it.getDuration()})")
}

proxyServer.commandManager.register(PlayCommand.create())
Expand All @@ -64,4 +64,13 @@ class JukeboxPlugin @Inject constructor(

logger.info("Jukebox plugin successfully initialized.")
}

@Subscribe
fun onPlayerLeave(event: DisconnectEvent) {
val status = Song.status[event.player]
if (status != null) {
status.task.cancel()
Song.status.remove(event.player)
}
}
}
45 changes: 44 additions & 1 deletion src/main/kotlin/com/bluedragonmc/jukebox/Song.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.bluedragonmc.jukebox

import com.bluedragonmc.jukebox.event.SongEndEvent
import com.bluedragonmc.jukebox.event.SongPauseEvent
import com.bluedragonmc.jukebox.event.SongResumeEvent
import com.bluedragonmc.jukebox.event.SongStartEvent
import com.velocitypowered.api.proxy.Player
import com.velocitypowered.api.proxy.ProxyServer
import com.velocitypowered.api.scheduler.ScheduledTask
Expand All @@ -10,6 +14,7 @@ import java.time.Duration
import java.util.concurrent.atomic.AtomicInteger
import kotlin.io.path.nameWithoutExtension
import kotlin.io.path.readBytes
import kotlin.io.path.relativeTo

class Song(file: Path) {

Expand All @@ -25,10 +30,13 @@ class Song(file: Path) {
*/
private val ticks: Array<List<NBSNote>?>

val fileName: String = file.relativeTo(JukeboxPlugin.INSTANCE.dataDirectory).toString()

val songName: String
val author: String
val originalAuthor: String
val description: String
val durationInTicks: Int get() = ticks.size

init {
val buffer = ByteBuffer.wrap(file.readBytes()).order(ByteOrder.LITTLE_ENDIAN)
Expand Down Expand Up @@ -130,6 +138,7 @@ class Song(file: Path) {
// Song has ended
task.cancel()
status.remove(player)
proxyServer.eventManager.fireAndForget(SongEndEvent(player, this))
return@buildTask
}

Expand All @@ -138,6 +147,7 @@ class Song(file: Path) {
}
}.repeat(Duration.ofMillis(interval)).schedule()
status[player] = Status(false, this, task, currentTick)
proxyServer.eventManager.fireAndForget(SongStartEvent(player, this, startTimeInTicks))
}

fun getDuration(): String {
Expand All @@ -161,14 +171,35 @@ class Song(file: Path) {
val status = mutableMapOf<Player, Status>()

fun pause(player: Player) {
status[player]?.let { status ->
JukeboxPlugin.INSTANCE.proxyServer.eventManager.fireAndForget(
SongPauseEvent(
player,
status.song,
status.currentTimeInTicks
)
)
}
status[player]?.isPaused = true
}

fun resume(player: Player) {
status[player]?.let { status ->
JukeboxPlugin.INSTANCE.proxyServer.eventManager.fireAndForget(
SongResumeEvent(
player,
status.song,
status.currentTimeInTicks
)
)
}
status[player]?.isPaused = false
}

fun stop(player: Player) {
status[player]?.song?.let { song ->
JukeboxPlugin.INSTANCE.proxyServer.eventManager.fireAndForget(SongEndEvent(player, song))
}
status[player]?.task?.cancel()
status.remove(player)
}
Expand All @@ -182,7 +213,19 @@ class Song(file: Path) {
song.play(JukeboxPlugin.INSTANCE.proxyServer, player, startTimeInTicks)
}

/**
* Loads a song from an arbitrary path
*/
fun load(path: Path) = Song(path)
fun load(name: String) = Song(JukeboxPlugin.INSTANCE.dataDirectory.resolve("songs/$name"))

/**
* Loads a song relative to the Jukebox plugin's `data/songs` directory
*/
fun load(name: String) = loadRelative(Path.of("songs", name))

/**
* Loads a song relative to the Jukebox plugin's data directory
*/
fun loadRelative(path: Path) = Song(JukeboxPlugin.INSTANCE.dataDirectory.resolve(path))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedragonmc.jukebox.event

import com.bluedragonmc.jukebox.Song
import com.velocitypowered.api.proxy.Player

class SongEndEvent(player: Player, song: Song) : SongEvent(player, song)
6 changes: 6 additions & 0 deletions src/main/kotlin/com/bluedragonmc/jukebox/event/SongEvent.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedragonmc.jukebox.event

import com.bluedragonmc.jukebox.Song
import com.velocitypowered.api.proxy.Player

abstract class SongEvent(val player: Player, val song: Song)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedragonmc.jukebox.event

import com.bluedragonmc.jukebox.Song
import com.velocitypowered.api.proxy.Player

class SongPauseEvent(player: Player, song: Song, val timeInTicks: Int) : SongEvent(player, song)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedragonmc.jukebox.event

import com.bluedragonmc.jukebox.Song
import com.velocitypowered.api.proxy.Player

class SongResumeEvent(player: Player, song: Song, val timeInTicks: Int) : SongEvent(player, song)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.bluedragonmc.jukebox.event

import com.bluedragonmc.jukebox.Song
import com.velocitypowered.api.proxy.Player

class SongStartEvent(player: Player, song: Song, val startTimeInTicks: Int) : SongEvent(player, song)

0 comments on commit 3b2e9f0

Please sign in to comment.