fix music

This commit is contained in:
Chayapak 2023-07-27 17:36:54 +07:00
parent aa36a4c0d9
commit 22f5f5a7f8
2 changed files with 64 additions and 52 deletions

View file

@ -14,11 +14,10 @@ import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.*;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
@ -184,15 +183,23 @@ false
final String prefix = context.prefix; final String prefix = context.prefix;
final Path _path = Path.of(root.toString(), String.join(" ", Arrays.copyOfRange(args, 1, args.length))); final Path path = (args.length < 2) ?
final Path path = (args.length < 2) ? root : _path; root :
Path.of(
root.toString(),
String.join(" ", Arrays.copyOfRange(args, 1, args.length))
);
if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED); if (!path.normalize().startsWith(root.toString())) return Component.text("no").color(NamedTextColor.RED);
final String[] filenames = path.toFile().list(); try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
if (filenames == null) return Component.text("Directory doesn't exist").color(NamedTextColor.RED); final List<Path> paths = new ArrayList<>();
for (Path eachPath : stream) paths.add(eachPath);
paths.sort((p1, p2) -> {
final String s1 = p1.getFileName().toString();
final String s2 = p2.getFileName().toString();
Arrays.sort(filenames, (s1, s2) -> {
int result = s1.compareToIgnoreCase(s2); int result = s1.compareToIgnoreCase(s2);
if (result == 0) { if (result == 0) {
return s2.compareTo(s1); return s2.compareTo(s1);
@ -202,8 +209,8 @@ false
final List<Component> fullList = new ArrayList<>(); final List<Component> fullList = new ArrayList<>();
int i = 0; int i = 0;
for (String filename : filenames) { for (Path eachPath : paths) {
final boolean isDirectory = Files.isDirectory(path); final boolean isDirectory = Files.isDirectory(eachPath);
Path location; Path location;
try { try {
@ -211,10 +218,12 @@ false
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
location = Paths.get(""); // wtf mabe location = Paths.get(""); // wtf mabe
} }
final String joinedPath = (args.length < 2) ? filename : Paths.get(location.getFileName().toString(), filename).toString();
final String joinedPath = (args.length < 2) ? eachPath.getFileName().toString() : Paths.get(location.getFileName().toString(), eachPath.getFileName().toString()).toString();
fullList.add( fullList.add(
Component Component
.text(filename, (i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary)) .text(eachPath.getFileName().toString(), (i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
.clickEvent( .clickEvent(
ClickEvent.suggestCommand( ClickEvent.suggestCommand(
prefix + prefix +
@ -240,6 +249,9 @@ false
index += eachSize; index += eachSize;
list.clear(); list.clear();
} }
} catch (NotDirectoryException e) {
return Component.text("Directory doesn't exist").color(NamedTextColor.RED);
} catch (IOException ignored) {}
return null; return null;
} }

View file

@ -260,8 +260,8 @@ public class MusicPlayerPlugin extends Bot.Listener {
} }
public void stopPlaying () { public void stopPlaying () {
currentSong = null;
removeBossBar(); removeBossBar();
currentSong = null;
} }
@Override @Override