Added subdirectory listing to songs

This commit is contained in:
hhhzzzsss 2023-06-17 00:51:59 -05:00
parent 7daf1049ef
commit c032f4d5ae
2 changed files with 49 additions and 4 deletions

View file

@ -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<String> 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<String> subdirectories = Util.listFilesSilently(dir)
.filter(Files::isDirectory)
.map(Path::getFileName)
.map(Path::toString)
.map(str -> str + "/")
.collect(Collectors.toList());
List<String> songs = Util.listFilesSilently(SongPlayer.SONG_DIR)
List<String> 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<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) {
if (!args.contains(" ")) {
return Util.giveSongDirectorySuggestions(args, suggestionsBuilder);
}
else {
return null;
}
}
}
private static class playlistCommand extends Command {

View file

@ -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<Suggestions> 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<Path> songFiles = listFilesSilently(dir);
if (songFiles == null) return null;
List<String> 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);