From 7785f1e12e584231c98156871e6bafb589c2b6a5 Mon Sep 17 00:00:00 2001 From: TsonT Date: Sun, 26 Mar 2023 09:04:11 -0400 Subject: [PATCH] Add feature to load directory of songs as playlist --- .../jmusicbot/playlist/PlaylistLoader.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java index 602235fcf..024e0ed66 100644 --- a/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java +++ b/src/main/java/com/jagrosh/jmusicbot/playlist/PlaylistLoader.java @@ -96,7 +96,7 @@ public Playlist getPlaylist(String name) { boolean[] shuffle = {false}; List list = new ArrayList<>(); - Files.readAllLines(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt")).forEach(str -> + Files.readAllLines(OtherUtil.getPath(config.getPlaylistsFolder()+File.separator+name+".txt")).forEach(str -> { String s = str.trim(); if(s.isEmpty()) @@ -107,6 +107,9 @@ public Playlist getPlaylist(String name) if(s.equalsIgnoreCase("#shuffle") || s.equalsIgnoreCase("//shuffle")) shuffle[0]=true; } + else if(isDirectory(s)){ + list.addAll(getFilePaths(s)); + } else list.add(s); }); @@ -125,6 +128,25 @@ public Playlist getPlaylist(String name) return null; } } + + public static boolean isDirectory(String filePath) { + File file = new File(filePath); + return file.isDirectory(); + } + + public static List getFilePaths(String filePath) { + List filePaths = new ArrayList<>(); + File directory = new File(filePath); + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + filePaths.add(file.getAbsolutePath()); + } + } + } + return filePaths; + } private static void shuffle(List list)