= Condensed track scheduler
This commit is contained in:
+1
-5
@@ -41,7 +41,6 @@ import net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent;
|
|||||||
import net.dv8tion.jda.core.hooks.ListenerAdapter;
|
import net.dv8tion.jda.core.hooks.ListenerAdapter;
|
||||||
import net.dv8tion.jda.core.managers.AudioManager;
|
import net.dv8tion.jda.core.managers.AudioManager;
|
||||||
import offline.Ref;
|
import offline.Ref;
|
||||||
import trash.TrackScheduler;
|
|
||||||
import utils.GlobalLog;
|
import utils.GlobalLog;
|
||||||
|
|
||||||
// http://www.slf4j.org/ - this JDA logging tool has been disabled by specifying NOP implementation.
|
// http://www.slf4j.org/ - this JDA logging tool has been disabled by specifying NOP implementation.
|
||||||
@@ -53,13 +52,10 @@ public class Main extends ListenerAdapter
|
|||||||
public class GuildMusicManager
|
public class GuildMusicManager
|
||||||
{
|
{
|
||||||
public final AudioPlayer player;
|
public final AudioPlayer player;
|
||||||
public final TrackScheduler scheduler;
|
|
||||||
|
|
||||||
public GuildMusicManager(AudioPlayerManager manager)
|
public GuildMusicManager(AudioPlayerManager manager)
|
||||||
{
|
{
|
||||||
player = manager.createPlayer();
|
player = manager.createPlayer();
|
||||||
scheduler = new TrackScheduler(player);
|
|
||||||
player.addListener(scheduler);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public AudioPlayerSendHandler getSendHandler()
|
public AudioPlayerSendHandler getSendHandler()
|
||||||
@@ -117,7 +113,7 @@ public class Main extends ListenerAdapter
|
|||||||
public void trackLoaded(AudioTrack track)
|
public void trackLoaded(AudioTrack track)
|
||||||
{
|
{
|
||||||
connectToFirstVoiceChannel(guild.getAudioManager());
|
connectToFirstVoiceChannel(guild.getAudioManager());
|
||||||
manager.scheduler.queue(track);
|
manager.player.startTrack(track, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void connectToFirstVoiceChannel(AudioManager audioManager)
|
private void connectToFirstVoiceChannel(AudioManager audioManager)
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
package trash;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
|
||||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
|
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This class schedules tracks for the audio player. It contains the queue of tracks.
|
|
||||||
*/
|
|
||||||
public class TrackScheduler extends AudioEventAdapter {
|
|
||||||
private final AudioPlayer player;
|
|
||||||
private final BlockingQueue<AudioTrack> queue;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param player The audio player this scheduler uses
|
|
||||||
*/
|
|
||||||
public TrackScheduler(AudioPlayer player) {
|
|
||||||
this.player = player;
|
|
||||||
this.queue = new LinkedBlockingQueue<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Add the next track to queue or play right away if nothing is in the queue.
|
|
||||||
*
|
|
||||||
* @param track The track to play or add to queue.
|
|
||||||
*/
|
|
||||||
public void queue(AudioTrack track) {
|
|
||||||
// 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 (!player.startTrack(track, true)) {
|
|
||||||
queue.offer(track);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Start the next track, stopping the current one if it is playing.
|
|
||||||
*/
|
|
||||||
public void nextTrack() {
|
|
||||||
// Start the next track, regardless of if something is already playing or not. In case queue was empty, we are
|
|
||||||
// giving null to startTrack, which is a valid argument and will simply stop the player.
|
|
||||||
player.startTrack(queue.poll(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
|
||||||
// Only start the next track if the end reason is suitable for it (FINISHED or LOAD_FAILED)
|
|
||||||
if (endReason.mayStartNext) {
|
|
||||||
nextTrack();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user