From 05781ff15ed6a72ea1f5b41cec760c14968aed07 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 5 Nov 2022 13:03:30 +0100 Subject: [PATCH] Changes song scheduling to hopefully fix a bug There was an observed bug, where two songs in a playlist were playing at once after a third song was removed. --- .../knarcraft/minstrel/music/Playlist.java | 8 ++---- .../net/knarcraft/minstrel/music/Song.java | 25 +++++++++++-------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/java/net/knarcraft/minstrel/music/Playlist.java b/src/main/java/net/knarcraft/minstrel/music/Playlist.java index 87e77a0..8faa057 100644 --- a/src/main/java/net/knarcraft/minstrel/music/Playlist.java +++ b/src/main/java/net/knarcraft/minstrel/music/Playlist.java @@ -137,12 +137,8 @@ public class Playlist { Song currentSong = this.songs.get(this.currentlyPlaying); currentSong.play(trait, trait.getVolume(), trait.getPitch()); currentlyPlaying++; - schedulerId = Bukkit.getScheduler().scheduleSyncRepeatingTask(MinstrelPlugin.getInstance(), () -> { - if (!currentSong.isPlaying()) { - Bukkit.getScheduler().cancelTask(schedulerId); - play(trait); - } - }, currentSong.getDuration() * 20L, 20); + schedulerId = Bukkit.getScheduler().scheduleSyncDelayedTask(MinstrelPlugin.getInstance(), () -> play(trait), + currentSong.getDuration() * 20L); } /** diff --git a/src/main/java/net/knarcraft/minstrel/music/Song.java b/src/main/java/net/knarcraft/minstrel/music/Song.java index dbbf3a4..b01484e 100644 --- a/src/main/java/net/knarcraft/minstrel/music/Song.java +++ b/src/main/java/net/knarcraft/minstrel/music/Song.java @@ -7,6 +7,9 @@ import org.bukkit.Location; import org.bukkit.SoundCategory; import org.bukkit.entity.Player; +import java.util.HashSet; +import java.util.Set; + /** * A representation of a song playable by a minstrel * @@ -19,6 +22,7 @@ public class Song { private final SoundCategory category; private final String sound; private boolean isPlaying = false; + private final Set playingFor = new HashSet<>(); /** * Instantiates a new song @@ -45,13 +49,15 @@ public class Song { * @param pitch

The pitch to play this song at

*/ public void play(MinstrelTrait trait, Player player, float volume, float pitch) { + playingFor.add(player); play(player, trait.getLocation(), volume, pitch); } /** * Plays this song at the given minstrel's location * - *

Volume of 1 means the max volume, which can be heard up to 1 chunk away. Setting it to 2 means it can be heard 2 chunks away.

+ *

Volume of 1 means the max volume, which can be heard up to 1 chunk away. Setting it to 2 means it can be + * heard 2 chunks away.

* * @param trait

The minstrel to play this song

* @param volume

The volume to play this song at

@@ -64,16 +70,8 @@ public class Song { this.isPlaying = true; //Mark this song as ended, once - Bukkit.getScheduler().runTaskLater(MinstrelPlugin.getInstance(), () -> this.isPlaying = false, durationSeconds * 20L); - } - - /** - * Gets whether this song is currently playing - * - * @return

True if this song is currently playing

- */ - public boolean isPlaying() { - return this.isPlaying; + Bukkit.getScheduler().runTaskLater(MinstrelPlugin.getInstance(), () -> this.isPlaying = false, + durationSeconds * 20L); } /** @@ -91,6 +89,11 @@ public class Song { * @param player

The player to stop this song for

*/ public void stop(Player player) { + //Don't bother stopping if not playing + if (!playingFor.contains(player) && !isPlaying) { + return; + } + playingFor.remove(player); if (this.category == null) { player.stopSound(this.sound); } else {