Discord bot that plays music from every website ever via youtube-dl
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Chords/src/main/java/moe/nekojimi/chords/QueueManager.java

143 lines
3.6 KiB

/*
* Copyright (C) 2022 jimj316
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package moe.nekojimi.chords;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.function.Consumer;
/**
*
* @author jimj316
*/
public class QueueManager implements Consumer<Song>
{
private Song restartingSong = null;
private final PriorityQueue<Song> jukeboxQueue = new PriorityQueue<>();
private Playlist playlist;
private MusicHandler handler;
public QueueManager()
{
// jukeboxQueue = new LinkedList<>();
}
@Override
public void accept(Song t)
{
jukeboxQueue.add(t);
if (!handler.isPlaying() || handler.getCurrentSong() == null)
handler.playNext();
if (handler.isPlaying() != (handler.getCurrentSong() == null))
System.err.println("WARNING: handler isPlaying violates contract! Something dumb going on!");
}
/**
* Called by the music handler when the current song has ended, or if
* playNext is called with nothing playing.
*
* @return the next track to play, or null to stop playing.
*/
public Song nextSongNeeded()
{
// if we're restarting the current song: store, clear, and return it
if (restartingSong != null)
{
Song ret = restartingSong;
restartingSong = null;
return ret;
}
// if there's anything in the queue, play that first
if (!jukeboxQueue.isEmpty())
{
return jukeboxQueue.poll();
}
// otherwise if there's a playlist, shuffle from that
else if (playlist != null)
{
return playlist.getNextSong();
}
// otherwise stop playing
else
return null;
}
public MusicHandler getHandler()
{
return handler;
}
public void addSong(Song song)
{
System.out.println("Song added to queue: " + song.getLocation().getAbsolutePath());
jukeboxQueue.add(song);
}
public boolean removeSong(int i)
{
try
{
return jukeboxQueue.remove((Song) jukeboxQueue.toArray()[i]);
} catch (ArrayIndexOutOfBoundsException ex)
{
return false;
}
}
public boolean removeSong(Song song)
{
return jukeboxQueue.remove(song);
}
public void setHandler(MusicHandler handler)
{
this.handler = handler;
handler.setQueueManager(this);
}
public Queue<Song> getJukeboxQueue()
{
return jukeboxQueue;
}
public Playlist getPlaylist()
{
return playlist;
}
public void setPlaylist(Playlist playlist)
{
this.playlist = playlist;
}
public boolean restartSong()
{
restartingSong = handler.getCurrentSong();
if (restartingSong != null)
{
handler.playNext();
return true;
} else
return false;
}
}