From 3b2e9f051a3e0497e5bb7c2131115ec61f8ef230 Mon Sep 17 00:00:00 2001 From: FluxCapacitor2 <31071265+FluxCapacitor2@users.noreply.github.com> Date: Thu, 21 Dec 2023 23:40:31 -0500 Subject: [PATCH] Add events and expose more song details through the API --- .../com/bluedragonmc/jukebox/JukeboxPlugin.kt | 11 ++++- .../kotlin/com/bluedragonmc/jukebox/Song.kt | 45 ++++++++++++++++++- .../jukebox/event/SongEndEvent.kt | 6 +++ .../bluedragonmc/jukebox/event/SongEvent.kt | 6 +++ .../jukebox/event/SongPauseEvent.kt | 6 +++ .../jukebox/event/SongResumeEvent.kt | 6 +++ .../jukebox/event/SongStartEvent.kt | 6 +++ 7 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/com/bluedragonmc/jukebox/event/SongEndEvent.kt create mode 100644 src/main/kotlin/com/bluedragonmc/jukebox/event/SongEvent.kt create mode 100644 src/main/kotlin/com/bluedragonmc/jukebox/event/SongPauseEvent.kt create mode 100644 src/main/kotlin/com/bluedragonmc/jukebox/event/SongResumeEvent.kt create mode 100644 src/main/kotlin/com/bluedragonmc/jukebox/event/SongStartEvent.kt diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/JukeboxPlugin.kt b/src/main/kotlin/com/bluedragonmc/jukebox/JukeboxPlugin.kt index c257e5e..87d6b71 100644 --- a/src/main/kotlin/com/bluedragonmc/jukebox/JukeboxPlugin.kt +++ b/src/main/kotlin/com/bluedragonmc/jukebox/JukeboxPlugin.kt @@ -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()) @@ -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) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt b/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt index 2e7fac1..16c6ed2 100644 --- a/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt +++ b/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt @@ -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 @@ -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) { @@ -25,10 +30,13 @@ class Song(file: Path) { */ private val ticks: Array?> + 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) @@ -130,6 +138,7 @@ class Song(file: Path) { // Song has ended task.cancel() status.remove(player) + proxyServer.eventManager.fireAndForget(SongEndEvent(player, this)) return@buildTask } @@ -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 { @@ -161,14 +171,35 @@ class Song(file: Path) { val status = mutableMapOf() 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) } @@ -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)) } } \ No newline at end of file diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEndEvent.kt b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEndEvent.kt new file mode 100644 index 0000000..0e9675d --- /dev/null +++ b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEndEvent.kt @@ -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) \ No newline at end of file diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEvent.kt b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEvent.kt new file mode 100644 index 0000000..1398b54 --- /dev/null +++ b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongEvent.kt @@ -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) diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/event/SongPauseEvent.kt b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongPauseEvent.kt new file mode 100644 index 0000000..8c607d7 --- /dev/null +++ b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongPauseEvent.kt @@ -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) \ No newline at end of file diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/event/SongResumeEvent.kt b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongResumeEvent.kt new file mode 100644 index 0000000..f8c5856 --- /dev/null +++ b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongResumeEvent.kt @@ -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) \ No newline at end of file diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/event/SongStartEvent.kt b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongStartEvent.kt new file mode 100644 index 0000000..7e09e7a --- /dev/null +++ b/src/main/kotlin/com/bluedragonmc/jukebox/event/SongStartEvent.kt @@ -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) \ No newline at end of file