Added spaces support and removed listFilesSilently

This commit is contained in:
hhhzzzsss 2023-06-20 20:08:40 -05:00
parent 82c4e90228
commit 142ec5cbb1
3 changed files with 161 additions and 95 deletions

View file

@ -21,6 +21,7 @@ import java.nio.file.Path;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream; import java.util.stream.Stream;
import static com.github.hhhzzzsss.songplayer.SongPlayer.MC; import static com.github.hhhzzzsss.songplayer.SongPlayer.MC;
@ -213,12 +214,7 @@ public class CommandProcessor {
} }
} }
public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) { public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) {
if (!args.contains(" ")) { return Util.giveSongSuggestions(args, suggestionsBuilder);
return Util.giveSongSuggestions(args, suggestionsBuilder);
}
else {
return null;
}
} }
} }
@ -434,17 +430,27 @@ public class CommandProcessor {
return true; return true;
} }
} }
List<String> subdirectories = Util.listFilesSilently(dir)
.filter(Files::isDirectory) List<String> subdirectories = null;
.map(Path::getFileName) List<String> songs = null;
.map(Path::toString) try {
.map(str -> str + "/") subdirectories = Files.list(dir)
.collect(Collectors.toList()); .filter(Files::isDirectory)
List<String> songs = Util.listFilesSilently(dir) .map(Path::getFileName)
.filter(Files::isRegularFile) .map(Path::toString)
.map(Path::getFileName) .map(str -> str + "/")
.map(Path::toString) .collect(Collectors.toList());
.collect(Collectors.toList()); songs = Files.list(dir)
.filter(Files::isRegularFile)
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toList());
}
catch (IOException e) {
SongPlayer.addChatMessage("§cError reading folder: §4" + e.getMessage());
return true;
}
if (subdirectories.size() == 0 && songs.size() == 0) { if (subdirectories.size() == 0 && songs.size() == 0) {
SongPlayer.addChatMessage("§bNo 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.");
} }
@ -466,12 +472,7 @@ public class CommandProcessor {
} }
} }
public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) { public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) {
if (!args.contains(" ")) { return Util.giveSongDirectorySuggestions(args, suggestionsBuilder);
return Util.giveSongDirectorySuggestions(args, suggestionsBuilder);
}
else {
return null;
}
} }
} }
@ -487,7 +488,7 @@ public class CommandProcessor {
"delete <playlist> <song>", "delete <playlist> <song>",
"addSong <playlist> <song>", "addSong <playlist> <song>",
"removeSong <playlist> <song>", "removeSong <playlist> <song>",
"renameSong <playlist> <old name> <new name>", "renameSong <playlist> <index> <new name>",
"loop", "loop",
"shuffle", "shuffle",
}; };
@ -506,7 +507,7 @@ public class CommandProcessor {
playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]); playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]);
} }
switch (split[0].toLowerCase(Locale.ROOT)) { switch (split[0].toLowerCase(Locale.ROOT)) {
case "play": case "play": {
if (split.length != 2) return false; if (split.length != 2) return false;
if (!Files.exists(playlistDir)) { if (!Files.exists(playlistDir)) {
SongPlayer.addChatMessage("§cPlaylist does not exist"); SongPlayer.addChatMessage("§cPlaylist does not exist");
@ -514,7 +515,8 @@ public class CommandProcessor {
} }
SongHandler.getInstance().setPlaylist(playlistDir); SongHandler.getInstance().setPlaylist(playlistDir);
return true; return true;
case "create": }
case "create": {
if (split.length > 2) { if (split.length > 2) {
SongPlayer.addChatMessage("§cCannot have spaces in playlist name"); SongPlayer.addChatMessage("§cCannot have spaces in playlist name");
return true; return true;
@ -523,15 +525,17 @@ public class CommandProcessor {
Playlist.createPlaylist(split[1]); Playlist.createPlaylist(split[1]);
SongPlayer.addChatMessage(String.format("§6Created playlist §3%s", split[1])); SongPlayer.addChatMessage(String.format("§6Created playlist §3%s", split[1]));
return true; return true;
case "delete": }
case "delete": {
if (split.length != 2) return false; if (split.length != 2) return false;
Playlist.deletePlaylist(playlistDir); Playlist.deletePlaylist(playlistDir);
SongPlayer.addChatMessage(String.format("§6Deleted playlist §3%s", split[1])); SongPlayer.addChatMessage(String.format("§6Deleted playlist §3%s", split[1]));
return true; return true;
case "list": }
case "list": {
if (split.length == 1) { if (split.length == 1) {
if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return true; if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return true;
List<String> playlists = Util.listFilesSilently(SongPlayer.PLAYLISTS_DIR) List<String> playlists = Files.list(SongPlayer.PLAYLISTS_DIR)
.filter(Files::isDirectory) .filter(Files::isDirectory)
.map(Path::getFileName) .map(Path::getFileName)
.map(Path::toString) .map(Path::toString)
@ -552,47 +556,63 @@ public class CommandProcessor {
} }
SongPlayer.addChatMessage("§6------------------------------"); SongPlayer.addChatMessage("§6------------------------------");
return true; return true;
case "addsong": }
if (split.length != 3) return false; case "addsong": {
Playlist.addSong(playlistDir, SongPlayer.SONG_DIR.resolve(split[2])); if (split.length < 3) return false;
SongPlayer.addChatMessage(String.format("§6Added §3%s §6to §3%s", split[2], split[1])); String location = String.join(" ", Arrays.copyOfRange(split, 2, split.length));
Playlist.addSong(playlistDir, SongPlayer.SONG_DIR.resolve(location));
SongPlayer.addChatMessage(String.format("§6Added §3%s §6to §3%s", location, split[1]));
return true; return true;
case "removesong": }
if (split.length != 3) return false; case "removesong": {
Playlist.removeSong(playlistDir, split[2]); if (split.length < 3) return false;
SongPlayer.addChatMessage(String.format("§6Removed §3%s §6from §3%s", split[2], split[1])); String location = String.join(" ", Arrays.copyOfRange(split, 2, split.length));
Playlist.removeSong(playlistDir, location);
SongPlayer.addChatMessage(String.format("§6Removed §3%s §6from §3%s", location, split[1]));
return true; return true;
case "renamesong": }
if (split.length != 4) return false; case "renamesong": {
Playlist.renameSong(playlistDir, split[2], split[3]); if (split.length < 4) return false;
SongPlayer.addChatMessage(String.format("§6Renamed song from §3%s §6from to §3%s", split[2], split[3])); String location = String.join(" ", Arrays.copyOfRange(split, 3, split.length));
int index = 0;
try {
index = Integer.parseInt(split[2]);
}
catch (Exception e) {
SongPlayer.addChatMessage(String.format("§cIndex must be an integer"));
return true;
}
String oldName = Playlist.renameSong(playlistDir, index-1, location);
SongPlayer.addChatMessage(String.format("§6Renamed §3%s §6to §3%s", oldName, location));
return true; return true;
case "loop": }
case "loop": {
if (split.length != 1) return false; if (split.length != 1) return false;
Config.getConfig().loopPlaylists = !Config.getConfig().loopPlaylists; Config.getConfig().loopPlaylists = !Config.getConfig().loopPlaylists;
SongHandler.getInstance().setPlaylistLoop(Config.getConfig().loopPlaylists); SongHandler.getInstance().setPlaylistLoop(Config.getConfig().loopPlaylists);
if (Config.getConfig().loopPlaylists) { if (Config.getConfig().loopPlaylists) {
SongPlayer.addChatMessage("§6Enabled playlist looping"); SongPlayer.addChatMessage("§6Enabled playlist looping");
} } else {
else {
SongPlayer.addChatMessage("§6Disabled playlist looping"); SongPlayer.addChatMessage("§6Disabled playlist looping");
} }
Config.saveConfigWithErrorHandling(); Config.saveConfigWithErrorHandling();
return true; return true;
case "shuffle": }
case "shuffle": {
if (split.length != 1) return false; if (split.length != 1) return false;
Config.getConfig().shufflePlaylists = !Config.getConfig().shufflePlaylists; Config.getConfig().shufflePlaylists = !Config.getConfig().shufflePlaylists;
SongHandler.getInstance().setPlaylistShuffle(Config.getConfig().shufflePlaylists); SongHandler.getInstance().setPlaylistShuffle(Config.getConfig().shufflePlaylists);
if (Config.getConfig().loopPlaylists) { if (Config.getConfig().loopPlaylists) {
SongPlayer.addChatMessage("§6Enabled playlist shuffling"); SongPlayer.addChatMessage("§6Enabled playlist shuffling");
} } else {
else {
SongPlayer.addChatMessage("§6Disabled playlist shuffling"); SongPlayer.addChatMessage("§6Disabled playlist shuffling");
} }
Config.saveConfigWithErrorHandling(); Config.saveConfigWithErrorHandling();
return true; return true;
default: }
default: {
return false; return false;
}
} }
} }
catch (IOException e) { catch (IOException e) {
@ -603,7 +623,7 @@ public class CommandProcessor {
public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) { public CompletableFuture<Suggestions> getSuggestions(String args, SuggestionsBuilder suggestionsBuilder) {
String[] split = args.split(" ", -1); String[] split = args.split(" ", -1);
if (split.length <= 1) { if (split.length <= 1) {
return CommandSource.suggestMatching(new String[] { return CommandSource.suggestMatching(new String[]{
"play", "play",
"create", "create",
"delete", "delete",
@ -619,29 +639,30 @@ public class CommandProcessor {
case "create": case "create":
case "loop": case "loop":
case "shuffle": case "shuffle":
default: default: {
return null; return null;
}
case "play": case "play":
case "list": case "list":
case "delete": case "delete": {
if (split.length == 2) { if (split.length == 2) {
return Util.givePlaylistSuggestions(suggestionsBuilder); return Util.givePlaylistSuggestions(suggestionsBuilder);
} }
return null; return null;
case "addsong": }
case "addsong": {
if (split.length == 2) { if (split.length == 2) {
return Util.givePlaylistSuggestions(suggestionsBuilder); return Util.givePlaylistSuggestions(suggestionsBuilder);
} } else if (split.length >= 3) {
else if (split.length == 3) { String location = String.join(" ", Arrays.copyOfRange(split, 2, split.length));
return Util.giveSongSuggestions(split[2], suggestionsBuilder); return Util.giveSongSuggestions(location, suggestionsBuilder);
} }
return null; return null;
case "removesong": }
case "renamesong": case "removesong": {
if (split.length == 2) { if (split.length == 2) {
return Util.givePlaylistSuggestions(suggestionsBuilder); return Util.givePlaylistSuggestions(suggestionsBuilder);
} } else if (split.length == 3) {
else if (split.length == 3) {
Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]); Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]);
Stream<Path> playlistFiles = Playlist.getSongFiles(playlistDir); Stream<Path> playlistFiles = Playlist.getSongFiles(playlistDir);
if (playlistFiles == null) { if (playlistFiles == null) {
@ -653,6 +674,22 @@ public class CommandProcessor {
suggestionsBuilder); suggestionsBuilder);
} }
return null; return null;
}
case "renamesong": {
if (split.length == 2) {
return Util.givePlaylistSuggestions(suggestionsBuilder);
} else if (split.length == 3) {
Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]);
Stream<Path> playlistFiles = Playlist.getSongFiles(playlistDir);
if (playlistFiles == null) {
return null;
}
int max = playlistFiles.collect(Collectors.toList()).size();
Stream<String> suggestions = IntStream.range(1, max+1).mapToObj(Integer::toString);
return CommandSource.suggestMatching(suggestions, suggestionsBuilder);
}
return null;
}
} }
} }
} }
@ -923,9 +960,10 @@ public class CommandProcessor {
String[] split = args.split(" "); String[] split = args.split(" ");
switch (split[0].toLowerCase(Locale.ROOT)) { switch (split[0].toLowerCase(Locale.ROOT)) {
case "create": case "create":
if (split.length != 2) return false; if (split.length < 2) return false;
String location = String.join(" ", Arrays.copyOfRange(split, 1, split.length));
try { try {
(new SongItemCreatorThread(split[1])).start(); (new SongItemCreatorThread(location)).start();
} catch (IOException e) { } catch (IOException e) {
SongPlayer.addChatMessage("§cError creating song item: §4" + e.getMessage()); SongPlayer.addChatMessage("§cError creating song item: §4" + e.getMessage());
} }
@ -957,8 +995,9 @@ public class CommandProcessor {
} }
switch (split[0].toLowerCase(Locale.ROOT)) { switch (split[0].toLowerCase(Locale.ROOT)) {
case "create": case "create":
if (split.length == 2) { if (split.length >= 2) {
return Util.giveSongSuggestions(split[1], suggestionsBuilder); String location = String.join(" ", Arrays.copyOfRange(split, 1, split.length));
return Util.giveSongSuggestions(location, suggestionsBuilder);
} }
case "setsongname": case "setsongname":
default: default:

View file

@ -30,15 +30,6 @@ public class Util {
catch (IOException e) {} catch (IOException e) {}
} }
public static Stream<Path> listFilesSilently(Path path) {
try {
return Files.list(path);
}
catch (IOException e) {
return null;
}
}
public static String formatTime(long milliseconds) { public static String formatTime(long milliseconds) {
long temp = Math.abs(milliseconds); long temp = Math.abs(milliseconds);
temp /= 1000; temp /= 1000;
@ -89,29 +80,48 @@ public class Util {
dir = dir.resolve(dirString); dir = dir.resolve(dirString);
} }
Stream<Path> songFiles = listFilesSilently(dir); Stream<Path> songFiles;
if (songFiles == null) return null; try {
songFiles = Files.list(dir);
} catch (IOException e) {
return null;
}
ArrayList<String> suggestions = new ArrayList<>(); int clipStart;
if (arg.contains(" ")) {
clipStart = arg.lastIndexOf(" ") + 1;
}
else {
clipStart = 0;
}
ArrayList<String> suggestionsList = new ArrayList<>();
for (Path path : songFiles.collect(Collectors.toList())) { for (Path path : songFiles.collect(Collectors.toList())) {
if (Files.isRegularFile(path)) { if (Files.isRegularFile(path)) {
suggestions.add(dirString + path.getFileName().toString()); suggestionsList.add(dirString + path.getFileName().toString());
} }
else if (Files.isDirectory(path)) { else if (Files.isDirectory(path)) {
suggestions.add(dirString + path.getFileName().toString() + "/"); suggestionsList.add(dirString + path.getFileName().toString() + "/");
} }
} }
Stream<String> suggestions = suggestionsList.stream()
.filter(str -> str.startsWith(arg))
.map(str -> str.substring(clipStart));
return CommandSource.suggestMatching(suggestions, suggestionsBuilder); return CommandSource.suggestMatching(suggestions, suggestionsBuilder);
} }
public static CompletableFuture<Suggestions> givePlaylistSuggestions(SuggestionsBuilder suggestionsBuilder) { public static CompletableFuture<Suggestions> givePlaylistSuggestions(SuggestionsBuilder suggestionsBuilder) {
if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return null; if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return null;
return CommandSource.suggestMatching( try {
listFilesSilently(SongPlayer.PLAYLISTS_DIR) return CommandSource.suggestMatching(
.filter(Files::isDirectory) Files.list(SongPlayer.PLAYLISTS_DIR)
.map(Path::getFileName) .filter(Files::isDirectory)
.map(Path::toString), .map(Path::getFileName)
suggestionsBuilder); .map(Path::toString),
suggestionsBuilder);
} catch (IOException e) {
return null;
}
} }
public static CompletableFuture<Suggestions> giveSongDirectorySuggestions(String arg, SuggestionsBuilder suggestionsBuilder) { public static CompletableFuture<Suggestions> giveSongDirectorySuggestions(String arg, SuggestionsBuilder suggestionsBuilder) {
@ -126,13 +136,26 @@ public class Util {
dirString = ""; dirString = "";
} }
Stream<Path> songFiles = listFilesSilently(dir); Stream<Path> songFiles;
if (songFiles == null) return null; try {
songFiles = Files.list(dir);
} catch (IOException e) {
return null;
}
List<String> suggestions = songFiles int clipStart;
if (arg.contains(" ")) {
clipStart = arg.lastIndexOf(" ") + 1;
}
else {
clipStart = 0;
}
Stream<String> suggestions = songFiles
.filter(Files::isDirectory) .filter(Files::isDirectory)
.map(path -> dirString + path.getFileName().toString() + "/") .map(path -> dirString + path.getFileName().toString() + "/")
.collect(Collectors.toList()); .filter(str -> str.startsWith(arg))
.map(str -> str.substring(clipStart));
return CommandSource.suggestMatching(suggestions, suggestionsBuilder); return CommandSource.suggestMatching(suggestions, suggestionsBuilder);
} }

View file

@ -156,11 +156,13 @@ public class Playlist {
} }
public static Stream<Path> getSongFiles(Path directory) { public static Stream<Path> getSongFiles(Path directory) {
Stream<Path> files = Util.listFilesSilently(directory); try {
if (files == null) { Stream<Path> files = Files.list(directory);
return files.filter(file -> !file.getFileName().toString().equals(INDEX_FILE_NAME));
}
catch (IOException e) {
return null; return null;
} }
return files.filter(file -> !file.getFileName().toString().equals(INDEX_FILE_NAME));
} }
private static List<String> loadIndex(Path directory) throws IOException { private static List<String> loadIndex(Path directory) throws IOException {
@ -242,14 +244,16 @@ public class Playlist {
.forEach(File::delete); .forEach(File::delete);
} }
public static void renameSong(Path directory, String oldName, String newName) throws IOException { // Returns old name
public static String renameSong(Path directory, int pos, String newName) throws IOException {
List<String> index = validateAndLoadIndex(directory); List<String> index = validateAndLoadIndex(directory);
int pos = index.indexOf(oldName); if (pos < 0 || pos >= index.size()) {
if (pos < 0) { throw new IOException("Index out of bounds");
throw new IOException("Song not found in playlist");
} }
Files.move(directory.resolve(oldName), directory.resolve(newName)); Path oldPath = directory.resolve(index.get(pos));
Files.move(oldPath, directory.resolve(newName));
index.set(pos, newName); index.set(pos, newName);
saveIndex(directory, index); saveIndex(directory, index);
return oldPath.getFileName().toString();
} }
} }