Add playlist playback support, loading & saving.

playlists
Nekojimi 3 years ago
parent f6fde8d694
commit 916e73eebd
  1. 61
      src/main/java/moe/nekojimi/chords/Main.java
  2. 67
      src/main/java/moe/nekojimi/chords/Playlist.java
  3. 18
      src/main/java/moe/nekojimi/chords/QueueManager.java
  4. 1
      src/main/java/moe/nekojimi/chords/Song.java
  5. 2
      src/main/java/moe/nekojimi/chords/TrackPlayer.java

@ -5,22 +5,27 @@
*/
package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.security.auth.login.LoginException;
import moe.nekojimi.chords.commands.*;
import moe.nekojimi.musicsearcher.Result;
import moe.nekojimi.musicsearcher.providers.MetaSearcher;
import moe.nekojimi.musicsearcher.providers.Searcher;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.JDABuilder;
import net.dv8tion.jda.api.MessageBuilder;
import net.dv8tion.jda.api.entities.*;
import net.dv8tion.jda.api.events.guild.voice.GuildVoiceLeaveEvent;
import net.dv8tion.jda.api.events.message.guild.GuildMessageReceivedEvent;
@ -34,9 +39,12 @@ import net.dv8tion.jda.api.utils.cache.CacheFlag;
*
* @author jimj316
*/
public class Main extends ListenerAdapter
public final class Main extends ListenerAdapter
{
private final File dataDirectory;
private final File playlistsDirectory;
private MusicHandler musicHandler;
private final Downloader downloader;
private final Searcher searcher;
@ -51,10 +59,12 @@ public class Main extends ListenerAdapter
private int trackNumber = 1;
private final Map<String, Playlist> playlists = new HashMap<>();
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws LoginException
public static void main(String[] args) throws LoginException, IOException
{
// We only need 2 gateway intents enabled for this example:
EnumSet<GatewayIntent> intents = EnumSet.of(
@ -125,11 +135,19 @@ public class Main extends ListenerAdapter
};
public Main()
public Main() throws IOException
{
log("INFO", "Starting up...");
// init dirs
dataDirectory = new File(System.getProperty("user.dir"));
playlistsDirectory = initDirectory(dataDirectory, "playlists");
// init downloader
downloader = new Downloader();
downloader.setMessageHandler(downloaderMessageHandler);
// init searcher
searcher = MetaSearcher.loadYAML(new File("searchproviders.yml"));
// init queue manager
@ -147,6 +165,9 @@ public class Main extends ListenerAdapter
helpCommand = new HelpCommand(this);
addCommand(helpCommand);
// load playlists
loadPlaylists();
log("INFO", "Started OK!");
}
@ -329,6 +350,35 @@ public class Main extends ListenerAdapter
System.out.println(type + " " + LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME) + "\t" + message);
}
public File initDirectory(File parent, String name) throws IOException
{
File ret = new File(parent, name);
if (!ret.exists())
Files.createDirectories(ret.toPath());
if (!ret.canRead())
throw new RuntimeException("Cannot read directory " + ret.getAbsolutePath() + "!");
if (!ret.canWrite())
throw new RuntimeException("Cannot write to directory " + ret.getAbsolutePath() + "!");
return ret;
}
private void loadPlaylists()
{
File[] files = playlistsDirectory.listFiles((File file, String name) -> name.endsWith(".yaml"));
for (File file : files)
{
try
{
YamlMapping map = Yaml.createYamlInput(file).readYamlMapping();
Playlist playlist = Playlist.fromYaml(map);
playlists.put(playlist.getName(), playlist);
} catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public MusicHandler getMusicHandler()
{
return musicHandler;
@ -363,4 +413,5 @@ public class Main extends ListenerAdapter
return trackNumber;
}
}

@ -16,19 +16,28 @@
*/
package moe.nekojimi.chords;
import com.amihaiemil.eoyaml.Yaml;
import com.amihaiemil.eoyaml.YamlMapping;
import java.util.ArrayList;
import java.util.List;
import com.amihaiemil.eoyaml.YamlSequence;
import com.amihaiemil.eoyaml.YamlSequenceBuilder;
import java.net.MalformedURLException;
import java.util.*;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author jimj316
*/
public class Playlist
public class Playlist implements Consumer<Song>
{
private static final int SHUFFLE_DONT_REPEAT_LAST = 3;
private final String name;
private final List<Song> songs = new ArrayList<>();
private final LinkedList<Song> playHistory = new LinkedList<>();
public Playlist(String name)
{
@ -37,12 +46,31 @@ public class Playlist
public YamlMapping toYaml()
{
throw new UnsupportedOperationException("Not supported yet.");
YamlSequenceBuilder songList = Yaml.createYamlSequenceBuilder();
for (Song song : songs)
songList = songList.add(song.toYaml());
return Yaml.createYamlMappingBuilder()
.add("name", name)
.add("songs", songList.build())
.build();
}
public static Playlist fromYaml(YamlMapping yaml)
{
throw new UnsupportedOperationException("Not supported yet.");
Playlist ret = new Playlist(yaml.string("name"));
YamlSequence songList = yaml.value("songs").asSequence();
for (int i = 0; i < songList.size(); i++)
{
try
{
ret.addSong(Song.fromYaml(songList.yamlMapping(i)));
} catch (MalformedURLException ex)
{
Logger.getLogger(Playlist.class.getName()).log(Level.SEVERE, null, ex);
}
}
return ret;
}
public void addSong(Song song)
@ -61,9 +89,34 @@ public class Playlist
return songs;
}
Song getNextSong()
public Song getNextSong()
{
Song ret;
// copy the song list
List<Song> toShuffle = new LinkedList<>(songs);
// remove play history from candidates, latest first, unless we'd have less than 2 options
for (int i = playHistory.size() - 1; i >= 0; i--)
{
if (toShuffle.size() <= 2)
break;
toShuffle.remove(playHistory.get(i));
}
Collections.shuffle(toShuffle);
ret = toShuffle.get(0);
playHistory.add(ret);
if (playHistory.size() > SHUFFLE_DONT_REPEAT_LAST)
playHistory.remove();
return ret;
}
@Override
public void accept(Song t)
{
throw new UnsupportedOperationException("Not supported yet.");
addSong(t);
}
}

@ -26,8 +26,6 @@ import java.util.function.Consumer;
*/
public class QueueManager implements Consumer<Song>
{
private Mode mode;
private final Queue<Song> jukeboxQueue;
private Playlist playlist;
private MusicHandler handler;
@ -118,24 +116,8 @@ public class QueueManager implements Consumer<Song>
this.playlist = playlist;
}
public Mode getMode()
{
return mode;
}
public void setMode(Mode mode)
{
this.mode = mode;
}
public boolean restartSong()
{
throw new UnsupportedOperationException("Not supported yet.");
}
public enum Mode
{
JUKEBOX,
PLAYLIST;
}
}

@ -60,6 +60,7 @@ public class Song
{
Song song = new Song(new URL(map.string("url")));
song.setArtist(map.string("artist"));
song.setTitle(map.string("title"));
song.setLocation(new File(map.string("location")));
song.setNumber(map.integer("num"));
song.setKept(Boolean.parseBoolean(map.string("kept")));

@ -132,7 +132,7 @@ public class TrackPlayer implements Closeable
public void close() throws IOException
{
input.close();
input.close(); //q
}

Loading…
Cancel
Save