From 4506edb7941d749b4799d2ce98382c5e6d043c93 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 5 Dec 2017 21:23:19 +0100 Subject: [PATCH] Avoiding duplicates in playlist add by checking for identical URL. --- .../lavaplayer/playlist/Playlist.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java index 0847e29..1ba35e0 100644 --- a/src/main/java/handiebot/lavaplayer/playlist/Playlist.java +++ b/src/main/java/handiebot/lavaplayer/playlist/Playlist.java @@ -66,6 +66,20 @@ public class Playlist { this.tracks.remove(track); } + /** + * Determines if the playlist already has a song with the same URL. + * @param track The track to check existence of. + * @return True if the track exists in the playlist, and false if not. + */ + public boolean hasTrack(UnloadedTrack track){ + for (UnloadedTrack t : this.tracks){ + if (t.getURL().equalsIgnoreCase(track.getURL())){ + return true; + } + } + return false; + } + /** * Copies all the tracks from another playlist onto this one. * @param playlist A playlist. @@ -108,7 +122,9 @@ public class Playlist { public void loadTrack(String url){ try { UnloadedTrack track = new UnloadedTrack(url); - this.tracks.add(track); + if (!this.hasTrack(track)) { + this.tracks.add(track); + } log.log(BotLog.TYPE.MUSIC, MessageFormat.format(resourceBundle.getString("playlist.loadTrack.log"), track.getTitle(), this.name)); } catch (Exception e) { log.log(BotLog.TYPE.ERROR, MessageFormat.format(resourceBundle.getString("playlist.loadTrack.error"), url, this.name));