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:
parent
28d8a2e64b
commit
05781ff15e
@ -137,12 +137,8 @@ public class Playlist {
|
|||||||
Song currentSong = this.songs.get(this.currentlyPlaying);
|
Song currentSong = this.songs.get(this.currentlyPlaying);
|
||||||
currentSong.play(trait, trait.getVolume(), trait.getPitch());
|
currentSong.play(trait, trait.getVolume(), trait.getPitch());
|
||||||
currentlyPlaying++;
|
currentlyPlaying++;
|
||||||
schedulerId = Bukkit.getScheduler().scheduleSyncRepeatingTask(MinstrelPlugin.getInstance(), () -> {
|
schedulerId = Bukkit.getScheduler().scheduleSyncDelayedTask(MinstrelPlugin.getInstance(), () -> play(trait),
|
||||||
if (!currentSong.isPlaying()) {
|
currentSong.getDuration() * 20L);
|
||||||
Bukkit.getScheduler().cancelTask(schedulerId);
|
|
||||||
play(trait);
|
|
||||||
}
|
|
||||||
}, currentSong.getDuration() * 20L, 20);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,6 +7,9 @@ import org.bukkit.Location;
|
|||||||
import org.bukkit.SoundCategory;
|
import org.bukkit.SoundCategory;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A representation of a song playable by a minstrel
|
* A representation of a song playable by a minstrel
|
||||||
*
|
*
|
||||||
@ -19,6 +22,7 @@ public class Song {
|
|||||||
private final SoundCategory category;
|
private final SoundCategory category;
|
||||||
private final String sound;
|
private final String sound;
|
||||||
private boolean isPlaying = false;
|
private boolean isPlaying = false;
|
||||||
|
private final Set<Player> playingFor = new HashSet<>();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Instantiates a new song
|
* Instantiates a new song
|
||||||
@ -45,13 +49,15 @@ public class Song {
|
|||||||
* @param pitch <p>The pitch to play this song at</p>
|
* @param pitch <p>The pitch to play this song at</p>
|
||||||
*/
|
*/
|
||||||
public void play(MinstrelTrait trait, Player player, float volume, float pitch) {
|
public void play(MinstrelTrait trait, Player player, float volume, float pitch) {
|
||||||
|
playingFor.add(player);
|
||||||
play(player, trait.getLocation(), volume, pitch);
|
play(player, trait.getLocation(), volume, pitch);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plays this song at the given minstrel's location
|
* 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 trait <p>The minstrel to play this song</p>
|
||||||
* @param volume <p>The volume to play this song at</p>
|
* @param volume <p>The volume to play this song at</p>
|
||||||
@ -64,16 +70,8 @@ public class Song {
|
|||||||
|
|
||||||
this.isPlaying = true;
|
this.isPlaying = true;
|
||||||
//Mark this song as ended, once
|
//Mark this song as ended, once
|
||||||
Bukkit.getScheduler().runTaskLater(MinstrelPlugin.getInstance(), () -> this.isPlaying = false, durationSeconds * 20L);
|
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -91,6 +89,11 @@ public class Song {
|
|||||||
* @param player <p>The player to stop this song for</p>
|
* @param player <p>The player to stop this song for</p>
|
||||||
*/
|
*/
|
||||||
public void stop(Player player) {
|
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) {
|
if (this.category == null) {
|
||||||
player.stopSound(this.sound);
|
player.stopSound(this.sound);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user