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,62 +183,75 @@ 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);
Arrays.sort(filenames, (s1, s2) -> { paths.sort((p1, p2) -> {
int result = s1.compareToIgnoreCase(s2); final String s1 = p1.getFileName().toString();
if (result == 0) { final String s2 = p2.getFileName().toString();
return s2.compareTo(s1);
int result = s1.compareToIgnoreCase(s2);
if (result == 0) {
return s2.compareTo(s1);
}
return result;
});
final List<Component> fullList = new ArrayList<>();
int i = 0;
for (Path eachPath : paths) {
final boolean isDirectory = Files.isDirectory(eachPath);
Path location;
try {
location = path;
} catch (IllegalArgumentException e) {
location = Paths.get(""); // wtf mabe
}
final String joinedPath = (args.length < 2) ? eachPath.getFileName().toString() : Paths.get(location.getFileName().toString(), eachPath.getFileName().toString()).toString();
fullList.add(
Component
.text(eachPath.getFileName().toString(), (i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
.clickEvent(
ClickEvent.suggestCommand(
prefix +
name +
(isDirectory ? " list " : " play ") +
joinedPath
)
)
);
} }
return result;
});
final List<Component> fullList = new ArrayList<>(); final int eachSize = 100;
int i = 0;
for (String filename : filenames) {
final boolean isDirectory = Files.isDirectory(path);
Path location; int index = 0;
try {
location = path; while (index <= fullList.size()) {
} catch (IllegalArgumentException e) { // we MUST make a new copy of the list else everything will fard..,.
location = Paths.get(""); // wtf mabe List<Component> list = new ArrayList<>(fullList).subList(index, Math.min(index + eachSize, fullList.size()));
final Component component = Component.join(JoinConfiguration.separator(Component.space()), list);
context.sendOutput(component);
index += eachSize;
list.clear();
} }
final String joinedPath = (args.length < 2) ? filename : Paths.get(location.getFileName().toString(), filename).toString(); } catch (NotDirectoryException e) {
fullList.add( return Component.text("Directory doesn't exist").color(NamedTextColor.RED);
Component } catch (IOException ignored) {}
.text(filename, (i++ & 1) == 0 ? ColorUtilities.getColorByString(bot.config.colorPalette.primary) : ColorUtilities.getColorByString(bot.config.colorPalette.secondary))
.clickEvent(
ClickEvent.suggestCommand(
prefix +
name +
(isDirectory ? " list " : " play ") +
joinedPath
)
)
);
}
final int eachSize = 100;
int index = 0;
while (index <= fullList.size()) {
// we MUST make a new copy of the list else everything will fard..,.
List<Component> list = new ArrayList<>(fullList).subList(index, Math.min(index + eachSize, fullList.size()));
final Component component = Component.join(JoinConfiguration.separator(Component.space()), list);
context.sendOutput(component);
index += eachSize;
list.clear();
}
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