Skip to content

Commit

Permalink
Allow the start time of the song to be changed
Browse files Browse the repository at this point in the history
  • Loading branch information
FluxCapacitor2 committed Dec 22, 2023
1 parent 6943bb2 commit d6461ce
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
41 changes: 21 additions & 20 deletions src/main/kotlin/com/bluedragonmc/jukebox/NBSNote.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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()]
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/com/bluedragonmc/jukebox/Song.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d6461ce

Please sign in to comment.