Improve music list

The code for it is messier now but it is better functionality-wise so yuup!!!!
This commit is contained in:
Chipmunk 2023-04-16 22:19:03 -04:00
parent 1b6f040122
commit 46a3d8e3ef

View file

@ -1,5 +1,6 @@
package land.chipmunk.chipmunkbot.commands;
import land.chipmunk.chipmunkbot.ChipmunkBot;
import land.chipmunk.chipmunkbot.song.Song;
import land.chipmunk.chipmunkbot.plugins.SongPlayer;
import land.chipmunk.chipmunkbot.command.*;
@ -21,7 +22,10 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.JoinConfiguration;
import java.io.File;
import java.nio.file.Path;
import java.util.List;
import java.util.ArrayList;
@ -134,23 +138,66 @@ public class MusicCommand {
public int list (CommandContext<CommandSource> context, Path path) throws CommandSyntaxException {
final CommandSource source = context.getSource();
final SongPlayer songPlayer = source.client().songPlayer();
final ChipmunkBot client = source.client();
final String prefix = client.chatCommandHandler().prefix();
final String[] filenames = path.toFile().list();
final File directory = path.toFile();
final String[] filenames = directory.list();
if (filenames == null) throw DIRECTORY_DOES_NOT_EXIST.create();
final List<Component> list = new ArrayList<>();
final Path root = Path.of(SongPlayer.SONG_DIR.getAbsoluteFile().getPath()).toAbsolutePath();
String relativePath;
if (path.getNameCount() - root.getNameCount() > 0) relativePath = path.subpath(root.getNameCount(), path.getNameCount()).toString();
else relativePath = "";
final List<Component> directories = new ArrayList<>();
final List<Component> files = new ArrayList<>();
int i = 0;
for (String filename : filenames) {
list.add(Component.text(filename, (i++ & 1) == 0 ? NamedTextColor.DARK_GREEN : NamedTextColor.GREEN));
final NamedTextColor color = (i++ & 1) == 0 ? NamedTextColor.DARK_GREEN : NamedTextColor.GREEN;
final File file = new File(directory, filename);
final Path relativeFilepath = Path.of(relativePath, filename);
final String escapedPath = escapePath(relativeFilepath.toString());
if (file.isDirectory()) {
directories.add(
Component.text(filename + "/", color)
.clickEvent(ClickEvent.suggestCommand(prefix + "music list " + escapedPath))
.hoverEvent(HoverEvent.showText(Component.translatable("Click to list %s", Component.text(filename, NamedTextColor.GREEN))))
);
} else {
files.add(
Component.text(filename, color)
.clickEvent(ClickEvent.suggestCommand(prefix + "music play " + escapedPath))
.hoverEvent(HoverEvent.showText(Component.translatable("Click to play %s", Component.text(filename, NamedTextColor.GREEN))))
);
}
}
final Component component = Component.translatable("Songs - %s", Component.join(JoinConfiguration.separator(Component.space()), list)).color(NamedTextColor.GREEN);
final ArrayList<Component> mergedList = new ArrayList<>();
for (Component component : directories) mergedList.add(component);
for (Component component : files) mergedList.add(component);
final Component component = Component.translatable("Songs - %s", Component.join(JoinConfiguration.separator(Component.space()), mergedList)).color(NamedTextColor.GREEN);
source.sendOutput(component, false);
return 1;
}
// TODO: Move this into some utility class, as it is more related to brigadier strings in general than to the list command in specific
private String escapePath (String path) {
final StringBuilder sb = new StringBuilder("'");
for (char character : path.toCharArray()) {
if (character == '\'' || character == '\\') sb.append('\\');
sb.append(character);
}
sb.append("'");
return sb.toString();
}
public int toggleLoop (CommandContext<CommandSource> context) throws CommandSyntaxException {
final CommandSource source = context.getSource();
final SongPlayer songPlayer = source.client().songPlayer();