From c032f4d5ae8311e77f3e9d0025b29e4cdf94d0ea Mon Sep 17 00:00:00 2001 From: hhhzzzsss Date: Sat, 17 Jun 2023 00:51:59 -0500 Subject: [PATCH] Added subdirectory listing to songs --- .../songplayer/CommandProcessor.java | 30 ++++++++++++++++--- .../com/github/hhhzzzsss/songplayer/Util.java | 23 ++++++++++++++ 2 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java index a0dac95..3aadd04 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/CommandProcessor.java @@ -421,28 +421,42 @@ public class CommandProcessor { return "Lists available songs. If an argument is provided, lists all songs in the subdirectory."; } public boolean processCommand(String args) { - if (args.length() == 0) { - List subdirectories = Util.listFilesSilently(SongPlayer.SONG_DIR) + if (!args.contains(" ")) { + Path dir; + if (args.length() == 0) { + dir = SongPlayer.SONG_DIR; + } + else { + dir = SongPlayer.SONG_DIR.resolve(args); + if (!Files.isDirectory(dir)) { + SongPlayer.addChatMessage("§cDirectory not found"); + return true; + } + } + List subdirectories = Util.listFilesSilently(dir) .filter(Files::isDirectory) .map(Path::getFileName) .map(Path::toString) .map(str -> str + "/") .collect(Collectors.toList()); - List songs = Util.listFilesSilently(SongPlayer.SONG_DIR) + List songs = Util.listFilesSilently(dir) .filter(Files::isRegularFile) .map(Path::getFileName) .map(Path::toString) .collect(Collectors.toList()); if (subdirectories.size() == 0 && songs.size() == 0) { - SongPlayer.addChatMessage("§6No songs found. You can put midi or nbs files in the §3.minecraft/songs §6folder."); + SongPlayer.addChatMessage("§bNo songs found. You can put midi or nbs files in the §3.minecraft/songs §6folder."); } else { + SongPlayer.addChatMessage("§6----------------------------------------"); + SongPlayer.addChatMessage("§eContents of .minecraft/songs/" + args); if (subdirectories.size() > 0) { SongPlayer.addChatMessage("§6Subdirectories: §3" + String.join(" ", subdirectories)); } if (songs.size() > 0) { SongPlayer.addChatMessage("§6Songs: §7" + String.join(", ", songs)); } + SongPlayer.addChatMessage("§6----------------------------------------"); } return true; } @@ -450,6 +464,14 @@ public class CommandProcessor { return false; } } + public CompletableFuture getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) { + if (!args.contains(" ")) { + return Util.giveSongDirectorySuggestions(args, suggestionsBuilder); + } + else { + return null; + } + } } private static class playlistCommand extends Command { diff --git a/src/main/java/com/github/hhhzzzsss/songplayer/Util.java b/src/main/java/com/github/hhhzzzsss/songplayer/Util.java index 5f8105e..a4feb1d 100644 --- a/src/main/java/com/github/hhhzzzsss/songplayer/Util.java +++ b/src/main/java/com/github/hhhzzzsss/songplayer/Util.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -113,6 +114,28 @@ public class Util { suggestionsBuilder); } + public static CompletableFuture giveSongDirectorySuggestions(String arg, SuggestionsBuilder suggestionsBuilder) { + int lastSlash = arg.lastIndexOf("/"); + String dirString; + Path dir = SongPlayer.SONG_DIR; + if (lastSlash >= 0) { + dirString = arg.substring(0, lastSlash+1); + dir = dir.resolve(dirString); + } + else { + dirString = ""; + } + + Stream songFiles = listFilesSilently(dir); + if (songFiles == null) return null; + + List suggestions = songFiles + .filter(Files::isDirectory) + .map(path -> dirString + path.getFileName().toString() + "/") + .collect(Collectors.toList()); + return CommandSource.suggestMatching(suggestions, suggestionsBuilder); + } + public static MutableText getStyledText(String str, Style style) { MutableText text = MutableText.of(new LiteralTextContent(str)); text.setStyle(style);