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.
This commit is contained in:
Kristian Knarvik 2022-11-05 13:03:30 +01:00
parent 28d8a2e64b
commit 05781ff15e
2 changed files with 16 additions and 17 deletions

View File

@ -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);
}
/**

View File

@ -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<Player> playingFor = new HashSet<>();
/**
* Instantiates a new song
@ -45,13 +49,15 @@ public class Song {
* @param pitch <p>The pitch to play this song at</p>
*/
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
*
* <p>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.</p>
* <p>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.</p>
*
* @param trait <p>The minstrel to play this song</p>
* @param volume <p>The volume to play this song at</p>
@ -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 <p>True if this song is currently playing</p>
*/
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 <p>The player to stop this song for</p>
*/
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 {