From d6461ce412c222c5df63da077198fc3eab976b24 Mon Sep 17 00:00:00 2001 From: FluxCapacitor2 <31071265+FluxCapacitor2@users.noreply.github.com> Date: Thu, 21 Dec 2023 21:35:22 -0500 Subject: [PATCH] Allow the start time of the song to be changed --- .../com/bluedragonmc/jukebox/NBSNote.kt | 41 ++++++++++--------- .../kotlin/com/bluedragonmc/jukebox/Song.kt | 8 ++-- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/NBSNote.kt b/src/main/kotlin/com/bluedragonmc/jukebox/NBSNote.kt index d047e9f..840ed5f 100644 --- a/src/main/kotlin/com/bluedragonmc/jukebox/NBSNote.kt +++ b/src/main/kotlin/com/bluedragonmc/jukebox/NBSNote.kt @@ -23,6 +23,25 @@ class NBSNote(val instrument: Byte, val key: Byte, val velocity: Byte?, val pan: NBSNote(byte, byte, null, null, null) } } + + private val sounds = arrayOf( + Sound.BLOCK_NOTE_BLOCK_HARP, + Sound.BLOCK_NOTE_BLOCK_BASS, + Sound.BLOCK_NOTE_BLOCK_BASEDRUM, + Sound.BLOCK_NOTE_BLOCK_SNARE, + Sound.BLOCK_NOTE_BLOCK_HAT, + Sound.BLOCK_NOTE_BLOCK_GUITAR, + Sound.BLOCK_NOTE_BLOCK_FLUTE, + Sound.BLOCK_NOTE_BLOCK_BELL, + Sound.BLOCK_NOTE_BLOCK_CHIME, + Sound.BLOCK_NOTE_BLOCK_XYLOPHONE, + Sound.BLOCK_NOTE_BLOCK_IRON_XYLOPHONE, + Sound.BLOCK_NOTE_BLOCK_COW_BELL, + Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, + Sound.BLOCK_NOTE_BLOCK_BIT, + Sound.BLOCK_NOTE_BLOCK_BANJO, + Sound.BLOCK_NOTE_BLOCK_PLING + ) } private fun getHorizontalDirection(location: Location): Vec2d { @@ -64,12 +83,13 @@ class NBSNote(val instrument: Byte, val key: Byte, val velocity: Byte?, val pan: } try { + val finalKey = key + (pitch ?: 0) / 100 wrappedPlayer.playSound( position, getSound(), SoundCategory.RECORDS, (velocity?.toFloat() ?: 100f) / 100f, - 2f.pow((key - 45) / 12f) + 2f.pow((finalKey - 45) / 12f) ) } catch (e: IllegalStateException) { e.printStackTrace() @@ -82,25 +102,6 @@ class NBSNote(val instrument: Byte, val key: Byte, val velocity: Byte?, val pan: } } - private val sounds = arrayOf( - Sound.BLOCK_NOTE_BLOCK_HARP, - Sound.BLOCK_NOTE_BLOCK_BASS, - Sound.BLOCK_NOTE_BLOCK_BASEDRUM, - Sound.BLOCK_NOTE_BLOCK_SNARE, - Sound.BLOCK_NOTE_BLOCK_HAT, - Sound.BLOCK_NOTE_BLOCK_GUITAR, - Sound.BLOCK_NOTE_BLOCK_FLUTE, - Sound.BLOCK_NOTE_BLOCK_BELL, - Sound.BLOCK_NOTE_BLOCK_CHIME, - Sound.BLOCK_NOTE_BLOCK_XYLOPHONE, - Sound.BLOCK_NOTE_BLOCK_IRON_XYLOPHONE, - Sound.BLOCK_NOTE_BLOCK_COW_BELL, - Sound.BLOCK_NOTE_BLOCK_DIDGERIDOO, - Sound.BLOCK_NOTE_BLOCK_BIT, - Sound.BLOCK_NOTE_BLOCK_BANJO, - Sound.BLOCK_NOTE_BLOCK_PLING - ) - private fun getSound(): Sound { return if (instrument >= sounds.size) sounds.last() else sounds[instrument.toInt()] } diff --git a/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt b/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt index 7ffbdca..2e7fac1 100644 --- a/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt +++ b/src/main/kotlin/com/bluedragonmc/jukebox/Song.kt @@ -119,9 +119,9 @@ class Song(file: Path) { } } - private fun play(proxyServer: ProxyServer, player: Player) { + private fun play(proxyServer: ProxyServer, player: Player, startTimeInTicks: Int = 0) { val interval = (1000.0 / tempo).toLong() - val currentTick = AtomicInteger(0) + val currentTick = AtomicInteger(startTimeInTicks) lateinit var task: ScheduledTask task = proxyServer.scheduler.buildTask(JukeboxPlugin.INSTANCE) { if (status[player]?.isPaused == true) return@buildTask @@ -177,9 +177,9 @@ class Song(file: Path) { return status[player] } - fun play(song: Song, player: Player) { + fun play(song: Song, player: Player, startTimeInTicks: Int = 0) { stop(player) - song.play(JukeboxPlugin.INSTANCE.proxyServer, player) + song.play(JukeboxPlugin.INSTANCE.proxyServer, player, startTimeInTicks) } fun load(path: Path) = Song(path)