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

187 lines
4.8 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.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.function.Consumer;
/**
*
* @author jimj316
*/
public class QueueManager extends QueueThing<Track, Track>
{
private final int QUEUE_TARGET_SIZE = 5;
private Track restartingTrack = null;
// private final PriorityQueue<Track> jukeboxQueue = new PriorityQueue<>();
private QueueThing<?, Track> trackSource;
private Playlist playlist;
private MusicHandler handler;
public QueueManager()
{
super(new PriorityQueue<Track>());
queueTargetSize = QUEUE_TARGET_SIZE;
// jukeboxQueue = new LinkedList<>();
}
@Override
protected void notifyNewInput()
{
super.notifyNewInput();
// if (inputQueue.size() < QUEUE_TARGET_SIZE)
// demandInput(QUEUE_TARGET_SIZE - inputQueue.size());
if (handler != null && !handler.isPlaying() || handler.getCurrentTrack() == null)
handler.requestTrack();
}
@Override
protected boolean completePromise(Promise<Track, Track> request)
{
final Track input = request.getInput();
if (input != null)
{
request.complete(input);
return true;
}
return false;
}
@Override
public List<Promise<Track, Track>> request(int count, Consumer<Track> destination)
{
if (restartingTrack != null)
{
Promise ret = new Promise<>(restartingTrack, destination);
restartingTrack = null;
return List.of(ret);
}
List<Promise<Track, Track>> ret = super.request(count, destination);
if (inputQueue.size() < QUEUE_TARGET_SIZE)
demandInput(QUEUE_TARGET_SIZE - inputQueue.size());
return ret;
}
/**
* Called by the music handler when the current track has ended, or if
* playNext is called with nothing playing.
*
* @return the next track to play, or null to stop playing.
*/
// public Track nextTrackNeeded()
// {
// Track ret;
// // if we're restarting the current track: store, clear, and return it
// if (restartingTrack != null)
// {
// ret = restartingTrack;
// restartingTrack = null;
// }
// // if there's anything in the queue, play that first
// else if (!jukeboxQueue.isEmpty())
// {
// ret = jukeboxQueue.poll();
// }
// // otherwise if there's a playlist, shuffle from that
// else if (playlist != null)
// {
// ret = playlist.getNextTrack();
// }
// // otherwise stop playing
// else
// ret = null;
//
// return ret;
// }
public MusicHandler getHandler()
{
return handler;
}
public void addTrack(Track track)
{
System.out.println("Track added to queue: " + track.getLocation().getAbsolutePath());
inputQueue.add(track);
}
public boolean removeTrack(int i)
{
try
{
return inputQueue.remove((Track) inputQueue.toArray()[i]);
} catch (ArrayIndexOutOfBoundsException ex)
{
return false;
}
}
public boolean removeTrack(Track track)
{
return inputQueue.remove(track);
}
public void setHandler(MusicHandler handler)
{
this.handler = handler;
handler.setQueueManager(this);
}
public Queue<Track> getJukeboxQueue()
{
return inputQueue;
}
public Playlist getPlaylist()
{
return playlist;
}
public void setPlaylist(Playlist playlist)
{
this.playlist = playlist;
}
public boolean restartTrack()
{
restartingTrack = handler.getCurrentTrack();
if (restartingTrack != null)
{
handler.requestTrack();
return true;
} else
return false;
}
public QueueThing<?, Track> getTrackSource()
{
return trackSource;
}
public void setTrackSource(QueueThing<?, Track> trackSource)
{
this.trackSource = trackSource;
}
}