mirror of
https://github.com/JonasunderscoreJones/ZtereoMUSIC.git
synced 2025-10-25 13:39:19 +02:00
Clean up and rename TrackScheduer to TrackManager
This commit is contained in:
parent
6bd6645918
commit
562015b40c
1 changed files with 48 additions and 36 deletions
|
|
@ -1,80 +1,88 @@
|
|||
package codes.ztereohype.ztereomusic.audio;
|
||||
|
||||
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEvent;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
|
||||
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventListener;
|
||||
import com.sedmelluq.discord.lavaplayer.tools.FriendlyException;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
|
||||
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
|
||||
import net.dv8tion.jda.api.entities.Invite;
|
||||
import lombok.Getter;
|
||||
import net.dv8tion.jda.api.entities.MessageChannel;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class TrackScheduer extends AudioEventAdapter {
|
||||
|
||||
private final AudioPlayer player;
|
||||
public class TrackManager extends AudioEventAdapter {
|
||||
private final @Getter AudioPlayer player;
|
||||
private final List<AudioTrack> trackQueue = new ArrayList<>();
|
||||
private AudioTrack currentTrack;
|
||||
private final MessageChannel infoChannel;
|
||||
|
||||
public TrackScheduer(AudioPlayer player, MessageChannel infoChannel) {
|
||||
public TrackManager(AudioPlayer player, MessageChannel infoChannel) {
|
||||
this.player = player;
|
||||
this.infoChannel = infoChannel;
|
||||
}
|
||||
|
||||
public void queue(AudioTrack track) {
|
||||
// change this to add to queue and call onTrackEnd!
|
||||
if (this.currentTrack == null) {
|
||||
System.out.println("Playing song directly as there is no song playing rn");
|
||||
this.currentTrack = track;
|
||||
player.playTrack(track);
|
||||
if (player.getPlayingTrack() == null) {
|
||||
play(track);
|
||||
} else {
|
||||
trackQueue.add(track);
|
||||
System.out.println("Added song to queue");
|
||||
infoChannel.sendMessage("Queued " + track.getInfo().title);
|
||||
}
|
||||
}
|
||||
|
||||
public AudioPlayer getPlayer() {
|
||||
return this.player;
|
||||
private void play(AudioTrack track) {
|
||||
player.playTrack(track);
|
||||
}
|
||||
|
||||
private void playNext() {
|
||||
// if the player was playing a track (probably means it's a skip), stop it
|
||||
if (player.getPlayingTrack() != null) {
|
||||
player.stopTrack();
|
||||
}
|
||||
|
||||
if (trackQueue.isEmpty()) {
|
||||
infoChannel.sendMessage("The queue is empty!").queue();
|
||||
return;
|
||||
}
|
||||
|
||||
AudioTrack nextTrack = trackQueue.get(0);
|
||||
trackQueue.remove(nextTrack);
|
||||
play(nextTrack);
|
||||
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
||||
}
|
||||
|
||||
public void pause() {
|
||||
player.setPaused(true);
|
||||
}
|
||||
|
||||
public void resume() {
|
||||
player.setPaused(false);
|
||||
}
|
||||
|
||||
public void skip() {
|
||||
playNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerPause(AudioPlayer player) {
|
||||
infoChannel.sendMessage("Pausing...").queue();
|
||||
// infoChannel.sendMessage("Pausing...").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlayerResume(AudioPlayer player) {
|
||||
infoChannel.sendMessage("Resuming...").queue();
|
||||
// infoChannel.sendMessage("Resuming...").queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackStart(AudioPlayer player, AudioTrack track) {
|
||||
infoChannel.sendMessage("Starting track: " + track.getInfo().title).queue();
|
||||
// infoChannel.sendMessage("Starting track: " + track.getInfo().title).queue();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
|
||||
currentTrack = null;
|
||||
|
||||
if (endReason.mayStartNext) {
|
||||
if (trackQueue.isEmpty()) {
|
||||
System.out.println("track finishd, no more tracks in queue!");
|
||||
infoChannel.sendMessage("no more tracks in queue!").queue();
|
||||
} else {
|
||||
System.out.println("track finishd, trying next track");
|
||||
AudioTrack nextTrack = trackQueue.get(0);
|
||||
trackQueue.remove(0);
|
||||
currentTrack = nextTrack;
|
||||
infoChannel.sendMessage("Playing next track: " + nextTrack.getInfo().title).queue();
|
||||
player.playTrack(currentTrack);
|
||||
}
|
||||
} else {
|
||||
System.out.println("track finishd, cant start next, bye");
|
||||
infoChannel.sendMessage("oopsie woopsie byebye").queue();
|
||||
playNext();
|
||||
}
|
||||
|
||||
// endReason == FINISHED: A track finished or died by an exception (mayStartNext = true).
|
||||
|
|
@ -87,11 +95,15 @@ public class TrackScheduer extends AudioEventAdapter {
|
|||
|
||||
@Override
|
||||
public void onTrackException(AudioPlayer player, AudioTrack track, FriendlyException exception) {
|
||||
// An already playing track threw an exception (track end event will still be received separately)
|
||||
infoChannel.sendMessage("Uh oh, a track did something strange. Skipping...").queue();
|
||||
trackQueue.remove(track);
|
||||
playNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackStuck(AudioPlayer player, AudioTrack track, long thresholdMs) {
|
||||
// Audio track has been unable to provide us any audio, might want to just start a new track
|
||||
infoChannel.sendMessage("Unable to play track " + track.getInfo().title + ". Skipping...").queue();
|
||||
trackQueue.remove(track);
|
||||
playNext();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue