Correct AutoFlow loop

This commit is contained in:
Sebastien 2018-03-05 12:01:03 +01:00
parent 0e16423ac1
commit 65297f26f4
2 changed files with 28 additions and 6 deletions

View File

@ -28,6 +28,7 @@ public class TrackScheduler extends AudioEventAdapter {
private final BlockingDeque<UserAudioTrack> queue;
private UserAudioTrack currentPlayingTrack;
private boolean autoPlay = true;
private ArrayList<String> history = new ArrayList<>();
Logger logger = LogManager.getLogger();
/**
@ -49,6 +50,12 @@ public class TrackScheduler extends AudioEventAdapter {
// Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
// something is playing, it returns false and does nothing. In that case the player was already playing so this
// track goes to the queue instead.
if(track.getSubmittedUser() != MainBot.jda.getSelfUser()){
logger.debug("Flush history");
history = new ArrayList<>();
}
history.add(track.getAudioTrack().getIdentifier());
if (!player.startTrack(track.getAudioTrack(), true)) {
queue.offer(track);
}
@ -68,6 +75,12 @@ public class TrackScheduler extends AudioEventAdapter {
// Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
// something is playing, it returns false and does nothing. In that case the player was already playing so this
// track goes to the queue instead.
if(track.getSubmittedUser() != MainBot.jda.getSelfUser()){
logger.debug("Flush history");
history = new ArrayList<>();
}
history.add(track.getAudioTrack().getIdentifier());
if (!player.startTrack(track.getAudioTrack(), true)) {
queue.addFirst(track);
}
@ -163,7 +176,7 @@ public class TrackScheduler extends AudioEventAdapter {
AudioM audioM = AudioM.getInstance(null);
YoutubeTools youtubeTools = YoutubeTools.getInstance(null);
try {
String id = youtubeTools.getRelatedVideo(currentPlayingTrack.getAudioTrack().getInfo().identifier);
String id = youtubeTools.getRelatedVideo(currentPlayingTrack.getAudioTrack().getInfo().identifier, history);
logger.info("Related id: "+id);
audioM.loadAndPlayAuto(id);
@ -178,5 +191,6 @@ public class TrackScheduler extends AudioEventAdapter {
public void setAutoPlay(boolean autoPlay) {
this.autoPlay = autoPlay;
needAutoPlay();
}
}

View File

@ -12,14 +12,13 @@ import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.youtube.YouTube;
import com.google.api.services.youtube.YouTubeScopes;
import com.google.api.services.youtube.model.SearchListResponse;
import com.google.api.services.youtube.model.SearchResult;
import net.dv8tion.jda.core.entities.Guild;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.*;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.*;
public class YoutubeTools {
/** Application name. */
@ -109,7 +108,7 @@ public class YoutubeTools {
.build();
}
public String getRelatedVideo(String videoId) throws IOException, GoogleJsonResponseException, Throwable {
public String getRelatedVideo(String videoId, ArrayList<String> history) throws IOException, GoogleJsonResponseException, Throwable {
YouTube youtube = getYouTubeService();
@ -118,7 +117,7 @@ public class YoutubeTools {
parameters.put("relatedToVideoId", videoId);
parameters.put("type", "video");
YouTube.Search.List searchListRelatedVideosRequest = youtube.search().list(parameters.get("part").toString());
YouTube.Search.List searchListRelatedVideosRequest = youtube.search().list(parameters.get("part"));
if (parameters.containsKey("relatedToVideoId") && parameters.get("relatedToVideoId") != "") {
searchListRelatedVideosRequest.setRelatedToVideoId(parameters.get("relatedToVideoId"));
}
@ -129,6 +128,15 @@ public class YoutubeTools {
SearchListResponse response = searchListRelatedVideosRequest.execute();
for(SearchResult item : response.getItems()){
if(!history.contains(item.getId().getVideoId())){
return item.getId().getVideoId();
}
else
logger.debug("ID already on history");
}
logger.debug("All on history ?");
return response.getItems().get(0).getId().getVideoId();
}