Prevents stopping of a song unless it's necessary

This commit is contained in:
Kristian Knarvik 2022-10-30 20:47:51 +01:00
parent 2babceeb87
commit 386d6492aa
3 changed files with 21 additions and 20 deletions

View File

@ -65,6 +65,7 @@ public class AddSongCommand implements CommandExecutor {
Song song = new Song(category, songIdString, duration); Song song = new Song(category, songIdString, duration);
playlist.addSong(song); playlist.addSong(song);
//If this is the first song in the playlist, start playing
if (playlist.getSongs().size() == 1) { if (playlist.getSongs().size() == 1) {
playlist.play(minstrelTrait); playlist.play(minstrelTrait);
} }

View File

@ -35,10 +35,12 @@ public class RemoveSongCommand implements CommandExecutor {
return false; return false;
} }
if (index >= 0 && playlist.getSongs().size() > index) { if (index >= 0 && playlist.getSongs().size() > index) {
playlist.removeSong(index);
//Stop any minstrels from playing the removed song //Stop any minstrels from playing the removed song
minstrelTrait.getPlaylist().stop(); playlist.stop();
minstrelTrait.getPlaylist().play(minstrelTrait); playlist.removeSong(index);
if (!playlist.getSongs().isEmpty()) {
playlist.play(minstrelTrait);
}
sender.sendMessage("Song removed"); sender.sendMessage("Song removed");
} else { } else {
sender.sendMessage("The specified index is outside the bounds of the playlist"); sender.sendMessage("The specified index is outside the bounds of the playlist");

View File

@ -6,7 +6,9 @@ import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* A representation of a playlist containing songs * A representation of a playlist containing songs
@ -17,6 +19,7 @@ public class Playlist {
private final boolean loop; private final boolean loop;
private int currentlyPlaying = 0; private int currentlyPlaying = 0;
private int schedulerId = -1; private int schedulerId = -1;
private final Map<Player, Song> playerCurrentSong = new HashMap<>();
/** /**
* Instantiates a new playlist * Instantiates a new playlist
@ -62,10 +65,10 @@ public class Playlist {
* @param player <p>The player to stop the playlist for</p> * @param player <p>The player to stop the playlist for</p>
*/ */
public void stop(Player player) { public void stop(Player player) {
for (Song song : this.songs) { Song currentlyPlaying = playerCurrentSong.get(player);
if (song.isPlaying()) { if (currentlyPlaying != null) {
song.stop(player); currentlyPlaying.stop(player);
} playerCurrentSong.remove(player);
} }
} }
@ -73,14 +76,14 @@ public class Playlist {
* Stops all songs in this playlist for all players, and aborts scheduling for the next song * Stops all songs in this playlist for all players, and aborts scheduling for the next song
*/ */
public void stop() { public void stop() {
for (Song song : this.songs) { for (Player player : Bukkit.getOnlinePlayers()) {
if (song.isPlaying()) { for (Song song : this.getSongs()) {
for (Player player : Bukkit.getOnlinePlayers()) { song.stop(player);
song.stop(player);
}
} }
} }
Bukkit.getScheduler().cancelTask(schedulerId); if (Bukkit.getScheduler().isCurrentlyRunning(schedulerId)) {
Bukkit.getScheduler().cancelTask(schedulerId);
}
} }
/** /**
@ -100,6 +103,7 @@ public class Playlist {
stop(player); stop(player);
Song currentSong = this.songs.get(this.currentlyPlaying - 1); Song currentSong = this.songs.get(this.currentlyPlaying - 1);
playerCurrentSong.put(player, currentSong);
currentSong.play(trait, player, trait.getVolume(), trait.getPitch()); currentSong.play(trait, player, trait.getVolume(), trait.getPitch());
} }
@ -122,12 +126,6 @@ public class Playlist {
} }
} }
for (Player player : Bukkit.getOnlinePlayers()) {
for (Song song : this.songs) {
song.stop(player);
}
}
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++;
@ -136,7 +134,7 @@ public class Playlist {
Bukkit.getScheduler().cancelTask(schedulerId); Bukkit.getScheduler().cancelTask(schedulerId);
play(trait); play(trait);
} }
}, currentSong.getDuration(), 20); }, currentSong.getDuration() * 20L, 20);
} }
/** /**