Changed to nio for symlink support
This commit is contained in:
parent
6c225c8bd3
commit
f2f5e4589a
8 changed files with 160 additions and 129 deletions
|
@ -10,6 +10,8 @@ import net.minecraft.command.CommandSource;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
|
@ -62,21 +64,27 @@ public class CommandProcessor {
|
|||
if (c == null) {
|
||||
SongPlayer.addChatMessage("§cUnrecognized command");
|
||||
} else {
|
||||
boolean success = c.processCommand(args);
|
||||
if (!success) {
|
||||
if (c.getSyntax().length == 0) {
|
||||
SongPlayer.addChatMessage("§cSyntax: " + Config.getConfig().prefix + c.getName());
|
||||
}
|
||||
else if (c.getSyntax().length == 1) {
|
||||
SongPlayer.addChatMessage("§cSyntax: " + Config.getConfig().prefix + c.getName() + " " + c.getSyntax()[0]);
|
||||
}
|
||||
else {
|
||||
SongPlayer.addChatMessage("§cSyntax:");
|
||||
for (String syntax : c.getSyntax()) {
|
||||
SongPlayer.addChatMessage("§c " + Config.getConfig().prefix + c.getName() + " " + syntax);
|
||||
try {
|
||||
boolean success = c.processCommand(args);
|
||||
if (!success) {
|
||||
if (c.getSyntax().length == 0) {
|
||||
SongPlayer.addChatMessage("§cSyntax: " + Config.getConfig().prefix + c.getName());
|
||||
}
|
||||
else if (c.getSyntax().length == 1) {
|
||||
SongPlayer.addChatMessage("§cSyntax: " + Config.getConfig().prefix + c.getName() + " " + c.getSyntax()[0]);
|
||||
}
|
||||
else {
|
||||
SongPlayer.addChatMessage("§cSyntax:");
|
||||
for (String syntax : c.getSyntax()) {
|
||||
SongPlayer.addChatMessage("§c " + Config.getConfig().prefix + c.getName() + " " + syntax);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Throwable e) {
|
||||
e.printStackTrace();
|
||||
SongPlayer.addChatMessage("§cAn error occurred while running this command: §4" + e.getMessage());
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
|
@ -405,19 +413,14 @@ public class CommandProcessor {
|
|||
}
|
||||
public boolean processCommand(String args) {
|
||||
if (args.length() == 0) {
|
||||
StringBuilder sb = new StringBuilder("§6");
|
||||
boolean firstItem = true;
|
||||
for (File songFile : SongPlayer.SONG_DIR.listFiles()) {
|
||||
String fileName = songFile.getName();
|
||||
if (firstItem) {
|
||||
firstItem = false;
|
||||
}
|
||||
else {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append(fileName);
|
||||
}
|
||||
SongPlayer.addChatMessage(sb.toString());
|
||||
String message = "§6" + String.join(
|
||||
", ",
|
||||
Util.listFilesSilently(SongPlayer.SONG_DIR)
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toList())
|
||||
);
|
||||
SongPlayer.addChatMessage(message);
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
|
@ -452,14 +455,14 @@ public class CommandProcessor {
|
|||
if (split.length < 1) return false;
|
||||
|
||||
try {
|
||||
File playlistDir = null;
|
||||
Path playlistDir = null;
|
||||
if (split.length >= 2) {
|
||||
playlistDir = new File(SongPlayer.PLAYLISTS_DIR, split[1]);
|
||||
playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]);
|
||||
}
|
||||
switch (split[0].toLowerCase()) {
|
||||
case "play":
|
||||
if (split.length != 2) return false;
|
||||
if (!playlistDir.exists()) {
|
||||
if (!Files.exists(playlistDir)) {
|
||||
SongPlayer.addChatMessage("§cPlaylist does not exist");
|
||||
return true;
|
||||
}
|
||||
|
@ -481,10 +484,11 @@ public class CommandProcessor {
|
|||
return true;
|
||||
case "list":
|
||||
if (split.length == 1) {
|
||||
if (!SongPlayer.PLAYLISTS_DIR.exists()) return true;
|
||||
List<String> playlists = Arrays.stream(SongPlayer.PLAYLISTS_DIR.listFiles())
|
||||
.filter(File::isDirectory)
|
||||
.map(File::getName)
|
||||
if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return true;
|
||||
List<String> playlists = Util.listFilesSilently(SongPlayer.PLAYLISTS_DIR)
|
||||
.filter(Files::isDirectory)
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toList());
|
||||
if (playlists.size() == 0) {
|
||||
SongPlayer.addChatMessage("§6No playlists found");
|
||||
|
@ -504,7 +508,7 @@ public class CommandProcessor {
|
|||
return true;
|
||||
case "addsong":
|
||||
if (split.length != 3) return false;
|
||||
Playlist.addSong(playlistDir, new File(SongPlayer.SONG_DIR, split[2]));
|
||||
Playlist.addSong(playlistDir, SongPlayer.SONG_DIR.resolve(split[2]));
|
||||
SongPlayer.addChatMessage(String.format("§6Added §3%s §6to §3%s", split[2], split[1]));
|
||||
return true;
|
||||
case "removesong":
|
||||
|
@ -592,14 +596,14 @@ public class CommandProcessor {
|
|||
return Util.givePlaylistSuggestions(suggestionsBuilder);
|
||||
}
|
||||
else if (split.length == 3) {
|
||||
File playlistDir = new File(SongPlayer.PLAYLISTS_DIR, split[1]);
|
||||
List<File> playlistFiles = Playlist.getSongFiles(playlistDir);
|
||||
Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(split[1]);
|
||||
Stream<Path> playlistFiles = Playlist.getSongFiles(playlistDir);
|
||||
if (playlistFiles == null) {
|
||||
return null;
|
||||
}
|
||||
return CommandSource.suggestMatching(
|
||||
playlistFiles.stream()
|
||||
.map(File::getName),
|
||||
playlistFiles.map(Path::getFileName)
|
||||
.map(Path::toString),
|
||||
suggestionsBuilder);
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -4,11 +4,13 @@ import com.google.gson.Gson;
|
|||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public class Config {
|
||||
private static Config config = null;
|
||||
|
||||
public static final File CONFIG_FILE = new File(SongPlayer.SONGPLAYER_DIR, "config.json");
|
||||
public static final Path CONFIG_FILE = SongPlayer.SONGPLAYER_DIR.resolve("config.json");
|
||||
private static final Gson gson = new Gson();
|
||||
|
||||
public String prefix = "$";
|
||||
|
@ -22,7 +24,7 @@ public class Config {
|
|||
if (config == null) {
|
||||
config = new Config();
|
||||
try {
|
||||
if (CONFIG_FILE.exists()) {
|
||||
if (Files.exists(CONFIG_FILE)) {
|
||||
loadConfig();
|
||||
}
|
||||
else {
|
||||
|
@ -37,17 +39,13 @@ public class Config {
|
|||
}
|
||||
|
||||
public static void loadConfig() throws IOException {
|
||||
FileInputStream fis = new FileInputStream(CONFIG_FILE);
|
||||
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
BufferedReader reader = Files.newBufferedReader(CONFIG_FILE);
|
||||
config = gson.fromJson(reader, Config.class);
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public static void saveConfig() throws IOException {
|
||||
FileOutputStream fos = new FileOutputStream(CONFIG_FILE);
|
||||
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
||||
BufferedWriter writer = new BufferedWriter(osw);
|
||||
BufferedWriter writer = Files.newBufferedWriter(CONFIG_FILE);
|
||||
writer.write(gson.toJson(config));
|
||||
writer.close();
|
||||
}
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package com.github.hhhzzzsss.songplayer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.LinkOption;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.minecraft.block.Block;
|
||||
|
@ -14,21 +18,21 @@ public class SongPlayer implements ModInitializer {
|
|||
public static final MinecraftClient MC = MinecraftClient.getInstance();
|
||||
public static final int NOTEBLOCK_BASE_ID = Block.getRawIdFromState(Blocks.NOTE_BLOCK.getDefaultState());
|
||||
|
||||
public static final File SONG_DIR = new File("songs");
|
||||
public static final File SONGPLAYER_DIR = new File("SongPlayer");
|
||||
public static final File PLAYLISTS_DIR = new File("SongPlayer/playlists");
|
||||
public static final Path SONG_DIR = Path.of("songs");
|
||||
public static final Path SONGPLAYER_DIR = Path.of("SongPlayer");
|
||||
public static final Path PLAYLISTS_DIR = Path.of("SongPlayer/playlists");
|
||||
public static FakePlayerEntity fakePlayer;
|
||||
|
||||
@Override
|
||||
public void onInitialize() {
|
||||
if (!SONG_DIR.exists()) {
|
||||
SONG_DIR.mkdirs();
|
||||
if (!Files.exists(SONG_DIR)) {
|
||||
Util.createDirectoriesSilently(SONG_DIR);
|
||||
}
|
||||
if (!SONGPLAYER_DIR.exists()) {
|
||||
SONGPLAYER_DIR.mkdirs();
|
||||
if (!Files.exists(SONGPLAYER_DIR)) {
|
||||
Util.createDirectoriesSilently(SONGPLAYER_DIR);
|
||||
}
|
||||
if (!PLAYLISTS_DIR.exists()) {
|
||||
PLAYLISTS_DIR.mkdirs();
|
||||
if (!Files.exists(PLAYLISTS_DIR)) {
|
||||
Util.createDirectoriesSilently(PLAYLISTS_DIR);
|
||||
}
|
||||
|
||||
CommandProcessor.initCommands();
|
||||
|
|
|
@ -1,18 +1,39 @@
|
|||
package com.github.hhhzzzsss.songplayer;
|
||||
|
||||
import com.github.hhhzzzsss.songplayer.song.Song;
|
||||
import com.mojang.brigadier.suggestion.Suggestions;
|
||||
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
|
||||
import net.minecraft.command.CommandSource;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Util {
|
||||
public static void createDirectoriesSilently(Path path) {
|
||||
try {
|
||||
Files.createDirectories(path);
|
||||
}
|
||||
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) {
|
||||
long temp = Math.abs(milliseconds);
|
||||
temp /= 1000;
|
||||
|
@ -57,32 +78,34 @@ public class Util {
|
|||
public static CompletableFuture<Suggestions> giveSongSuggestions(String arg, SuggestionsBuilder suggestionsBuilder) {
|
||||
int lastSlash = arg.lastIndexOf("/");
|
||||
String dirString = "";
|
||||
File dir = SongPlayer.SONG_DIR;
|
||||
Path dir = SongPlayer.SONG_DIR;
|
||||
if (lastSlash >= 0) {
|
||||
dirString = arg.substring(0, lastSlash+1);
|
||||
dir = new File(dir, dirString);
|
||||
dir = dir.resolve(dirString);
|
||||
}
|
||||
|
||||
if (!dir.exists()) return null;
|
||||
Stream<Path> songFiles = listFilesSilently(dir);
|
||||
if (songFiles == null) return null;
|
||||
|
||||
ArrayList<String> suggestions = new ArrayList<>();
|
||||
for (File file : dir.listFiles()) {
|
||||
if (file.isFile()) {
|
||||
suggestions.add(dirString + file.getName());
|
||||
for (Path path : songFiles.collect(Collectors.toList())) {
|
||||
if (Files.isRegularFile(path)) {
|
||||
suggestions.add(dirString + path.getFileName().toString());
|
||||
}
|
||||
else if (file.isDirectory()) {
|
||||
suggestions.add(dirString + file.getName() + "/");
|
||||
else if (Files.isDirectory(path)) {
|
||||
suggestions.add(dirString + path.getFileName().toString() + "/");
|
||||
}
|
||||
}
|
||||
return CommandSource.suggestMatching(suggestions, suggestionsBuilder);
|
||||
}
|
||||
|
||||
public static CompletableFuture<Suggestions> givePlaylistSuggestions(SuggestionsBuilder suggestionsBuilder) {
|
||||
if (!SongPlayer.PLAYLISTS_DIR.exists()) return null;
|
||||
if (!Files.exists(SongPlayer.PLAYLISTS_DIR)) return null;
|
||||
return CommandSource.suggestMatching(
|
||||
Arrays.stream(SongPlayer.PLAYLISTS_DIR.listFiles())
|
||||
.filter(File::isDirectory)
|
||||
.map(File::getName),
|
||||
listFilesSilently(SongPlayer.PLAYLISTS_DIR)
|
||||
.filter(Files::isDirectory)
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString),
|
||||
suggestionsBuilder);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ import net.minecraft.world.GameMode;
|
|||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class SongHandler {
|
||||
|
@ -155,7 +156,7 @@ public class SongHandler {
|
|||
SongPlayer.addChatMessage("§6Added song to queue: §3" + song.name);
|
||||
}
|
||||
|
||||
public void setPlaylist(File playlist) {
|
||||
public void setPlaylist(Path playlist) {
|
||||
if (loaderThread != null || currentSong != null || !songQueue.isEmpty()) {
|
||||
SongPlayer.addChatMessage("§cCannot start playing a playlist while something else is playing");
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.hhhzzzsss.songplayer.song;
|
||||
import java.io.*;
|
||||
import java.net.*;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.KeyManagementException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
@ -21,9 +22,9 @@ public class MidiConverter {
|
|||
return getSong(sequence, Paths.get(url.toURI().getPath()).getFileName().toString());
|
||||
}
|
||||
|
||||
public static Song getSongFromFile(File file) throws InvalidMidiDataException, IOException {
|
||||
Sequence sequence = MidiSystem.getSequence(file);
|
||||
return getSong(sequence, file.getName());
|
||||
public static Song getSongFromFile(Path file) throws InvalidMidiDataException, IOException {
|
||||
Sequence sequence = MidiSystem.getSequence(file.toFile());
|
||||
return getSong(sequence, file.getFileName().toString());
|
||||
}
|
||||
|
||||
public static Song getSongFromBytes(byte[] bytes, String name) throws InvalidMidiDataException, IOException {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package com.github.hhhzzzsss.songplayer.song;
|
||||
|
||||
import com.github.hhhzzzsss.songplayer.SongPlayer;
|
||||
import com.github.hhhzzzsss.songplayer.Util;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
|
@ -11,6 +12,7 @@ import java.nio.file.Files;
|
|||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class Playlist {
|
||||
public static final String INDEX_FILE_NAME = "index.json";
|
||||
|
@ -21,22 +23,22 @@ public class Playlist {
|
|||
public boolean shuffle = false;
|
||||
|
||||
public List<String> index;
|
||||
public List<File> songFiles;
|
||||
public List<Path> songFiles;
|
||||
public List<Song> songs = new ArrayList<>();
|
||||
public List<Integer> ordering = null;
|
||||
public int songNumber = 0;
|
||||
public boolean loaded = false;
|
||||
public ArrayList<String> songsFailedToLoad = new ArrayList<>();
|
||||
|
||||
public Playlist(File directory, boolean loop, boolean shuffle) {
|
||||
this.name = directory.getName();
|
||||
public Playlist(Path directory, boolean loop, boolean shuffle) {
|
||||
this.name = directory.getFileName().toString();
|
||||
this.loop = loop;
|
||||
this.shuffle = shuffle;
|
||||
this.setShuffle(this.shuffle);
|
||||
if (directory.isDirectory()) {
|
||||
if (Files.isDirectory(directory)) {
|
||||
index = validateAndLoadIndex(directory);
|
||||
songFiles = index.stream()
|
||||
.map(name -> new File(directory, name))
|
||||
.map(name -> directory.resolve(name))
|
||||
.collect(Collectors.toList());
|
||||
(new PlaylistLoaderThread()).start();
|
||||
} else {
|
||||
|
@ -48,11 +50,11 @@ public class Playlist {
|
|||
private class PlaylistLoaderThread extends Thread {
|
||||
@Override
|
||||
public void run() {
|
||||
for (File file : songFiles) {
|
||||
for (Path file : songFiles) {
|
||||
SongLoaderThread slt = new SongLoaderThread(file);
|
||||
slt.run();
|
||||
if (slt.exception != null) {
|
||||
songsFailedToLoad.add(file.getName());
|
||||
songsFailedToLoad.add(file.getFileName().toString());
|
||||
} else {
|
||||
songs.add(slt.song);
|
||||
}
|
||||
|
@ -101,11 +103,12 @@ public class Playlist {
|
|||
return songs.get(ordering.get(songNumber++));
|
||||
}
|
||||
|
||||
private static List<String> validateAndLoadIndex(File directory) {
|
||||
List<String> songNames = getSongFiles(directory).stream()
|
||||
.map(File::getName)
|
||||
private static List<String> validateAndLoadIndex(Path directory) {
|
||||
List<String> songNames = getSongFiles(directory)
|
||||
.map(Path::getFileName)
|
||||
.map(Path::toString)
|
||||
.collect(Collectors.toList());
|
||||
if (!getIndexFile(directory).exists()) {
|
||||
if (!Files.exists(getIndexFile(directory))) {
|
||||
saveIndexSilently(directory, songNames);
|
||||
return songNames;
|
||||
}
|
||||
|
@ -146,41 +149,35 @@ public class Playlist {
|
|||
}
|
||||
}
|
||||
|
||||
public static File getIndexFile(File directory) {
|
||||
return new File(directory, INDEX_FILE_NAME);
|
||||
public static Path getIndexFile(Path directory) {
|
||||
return directory.resolve(INDEX_FILE_NAME);
|
||||
}
|
||||
|
||||
public static List<File> getSongFiles(File directory) {
|
||||
File[] files = directory.listFiles();
|
||||
public static Stream<Path> getSongFiles(Path directory) {
|
||||
Stream<Path> files = Util.listFilesSilently(directory);
|
||||
if (files == null) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.stream(files)
|
||||
.filter(file -> !file.getName().equals(INDEX_FILE_NAME))
|
||||
.collect(Collectors.toList());
|
||||
return files.filter(file -> !file.getFileName().toString().equals(INDEX_FILE_NAME));
|
||||
}
|
||||
|
||||
private static List<String> loadIndex(File directory) throws IOException {
|
||||
File indexFile = getIndexFile(directory);
|
||||
FileInputStream fis = new FileInputStream(indexFile);
|
||||
InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
|
||||
BufferedReader reader = new BufferedReader(isr);
|
||||
private static List<String> loadIndex(Path directory) throws IOException {
|
||||
Path indexFile = getIndexFile(directory);
|
||||
BufferedReader reader = Files.newBufferedReader(indexFile);
|
||||
Type type = new TypeToken<ArrayList<String>>(){}.getType();
|
||||
List<String> index = gson.fromJson(reader, type);
|
||||
reader.close();
|
||||
return index;
|
||||
}
|
||||
|
||||
private static void saveIndex(File directory, List<String> index) throws IOException {
|
||||
File indexFile = getIndexFile(directory);
|
||||
FileOutputStream fos = new FileOutputStream(indexFile);
|
||||
OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8);
|
||||
BufferedWriter writer = new BufferedWriter(osw);
|
||||
private static void saveIndex(Path directory, List<String> index) throws IOException {
|
||||
Path indexFile = getIndexFile(directory);
|
||||
BufferedWriter writer = Files.newBufferedWriter(indexFile);
|
||||
writer.write(gson.toJson(index));
|
||||
writer.close();
|
||||
}
|
||||
|
||||
private static void saveIndexSilently(File directory, List<String> index) {
|
||||
private static void saveIndexSilently(Path directory, List<String> index) {
|
||||
try {
|
||||
saveIndex(directory, index);
|
||||
}
|
||||
|
@ -189,67 +186,67 @@ public class Playlist {
|
|||
}
|
||||
}
|
||||
|
||||
public static List<String> listSongs(File directory) throws IOException {
|
||||
if (!directory.exists()) {
|
||||
public static List<String> listSongs(Path directory) throws IOException {
|
||||
if (!Files.exists(directory)) {
|
||||
throw new IOException("Playlist does not exist");
|
||||
}
|
||||
return validateAndLoadIndex(directory);
|
||||
}
|
||||
|
||||
public static void createPlaylist(String playlist) {
|
||||
File playlistDir = new File(SongPlayer.PLAYLISTS_DIR, playlist);
|
||||
playlistDir.mkdir();
|
||||
Path playlistDir = SongPlayer.PLAYLISTS_DIR.resolve(playlist);
|
||||
Util.createDirectoriesSilently(playlistDir);
|
||||
}
|
||||
|
||||
public static void addSong(File directory, File songFile) throws IOException {
|
||||
if (!directory.exists()) {
|
||||
public static void addSong(Path directory, Path songFile) throws IOException {
|
||||
if (!Files.exists(directory)) {
|
||||
throw new IOException("Playlist does not exist");
|
||||
}
|
||||
if (!songFile.exists()) {
|
||||
if (!Files.exists(songFile)) {
|
||||
throw new IOException("Could not find specified song");
|
||||
}
|
||||
|
||||
List<String> index = validateAndLoadIndex(directory);
|
||||
if (index.contains(songFile.getName())) {
|
||||
if (index.contains(songFile.getFileName().toString())) {
|
||||
throw new IOException("Playlist already contains a song by this name");
|
||||
}
|
||||
Files.copy( songFile.toPath(), (new File(directory,songFile.getName())).toPath() );
|
||||
index.add(songFile.getName());
|
||||
Files.copy(songFile, directory.resolve(songFile.getFileName().toString()));
|
||||
index.add(songFile.getFileName().toString());
|
||||
saveIndex(directory, index);
|
||||
}
|
||||
|
||||
public static void removeSong(File directory, String songName) throws IOException {
|
||||
if (!directory.exists()) {
|
||||
public static void removeSong(Path directory, String songName) throws IOException {
|
||||
if (!Files.exists(directory)) {
|
||||
throw new IOException("Playlist does not exist");
|
||||
}
|
||||
File songFile = new File(directory, songName);
|
||||
if (!songFile.exists()) {
|
||||
Path songFile = directory.resolve(songName);
|
||||
if (!Files.exists(songFile)) {
|
||||
throw new IOException("Playlist does not contain a song by this name");
|
||||
}
|
||||
|
||||
List<String> index = validateAndLoadIndex(directory);
|
||||
songFile.delete();
|
||||
Files.delete(songFile);
|
||||
index.remove(songName);
|
||||
saveIndex(directory, index);
|
||||
}
|
||||
|
||||
public static void deletePlaylist(File directory) throws IOException {
|
||||
if (!directory.exists()) {
|
||||
public static void deletePlaylist(Path directory) throws IOException {
|
||||
if (!Files.exists(directory)) {
|
||||
throw new IOException("Playlist does not exist");
|
||||
}
|
||||
Files.walk(directory.toPath())
|
||||
Files.walk(directory)
|
||||
.map(Path::toFile)
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.forEach(File::delete);
|
||||
}
|
||||
|
||||
public static void renameSong(File directory, String oldName, String newName) throws IOException {
|
||||
public static void renameSong(Path directory, String oldName, String newName) throws IOException {
|
||||
List<String> index = validateAndLoadIndex(directory);
|
||||
int pos = index.indexOf(oldName);
|
||||
if (pos < 0) {
|
||||
throw new IOException("Song not found in playlist");
|
||||
}
|
||||
(new File(directory, oldName)).renameTo(new File(directory, newName));
|
||||
Files.move(directory.resolve(oldName), directory.resolve(newName));
|
||||
index.set(pos, newName);
|
||||
saveIndex(directory, index);
|
||||
}
|
||||
|
|
|
@ -6,12 +6,13 @@ import java.io.File;
|
|||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
public class SongLoaderThread extends Thread{
|
||||
|
||||
private String location;
|
||||
private File songPath;
|
||||
private Path songPath;
|
||||
private URL songUrl;
|
||||
public Exception exception;
|
||||
public Song song;
|
||||
|
@ -24,16 +25,16 @@ public class SongLoaderThread extends Thread{
|
|||
isUrl = true;
|
||||
songUrl = new URL(location);
|
||||
}
|
||||
else if (getSongFile(location).exists()) {
|
||||
else if (Files.exists(getSongFile(location))) {
|
||||
songPath = getSongFile(location);
|
||||
}
|
||||
else if (getSongFile(location+".mid").exists()) {
|
||||
else if (Files.exists(getSongFile(location+".mid"))) {
|
||||
songPath = getSongFile(location+".mid");
|
||||
}
|
||||
else if (getSongFile(location+".midi").exists()) {
|
||||
else if (Files.exists(getSongFile(location+".midi"))) {
|
||||
songPath = getSongFile(location+".midi");
|
||||
}
|
||||
else if (getSongFile(location+".nbs").exists()) {
|
||||
else if (Files.exists(getSongFile(location+".nbs"))) {
|
||||
songPath = getSongFile(location+".nbs");
|
||||
}
|
||||
else {
|
||||
|
@ -41,7 +42,7 @@ public class SongLoaderThread extends Thread{
|
|||
}
|
||||
}
|
||||
|
||||
public SongLoaderThread(File file) {
|
||||
public SongLoaderThread(Path file) {
|
||||
this.songPath = file;
|
||||
}
|
||||
|
||||
|
@ -54,8 +55,8 @@ public class SongLoaderThread extends Thread{
|
|||
name = Paths.get(songUrl.toURI().getPath()).getFileName().toString();
|
||||
}
|
||||
else {
|
||||
bytes = Files.readAllBytes(songPath.toPath());
|
||||
name = songPath.getName();
|
||||
bytes = Files.readAllBytes(songPath);
|
||||
name = songPath.getFileName().toString();
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -80,7 +81,9 @@ public class SongLoaderThread extends Thread{
|
|||
}
|
||||
}
|
||||
|
||||
private File getSongFile(String name) {
|
||||
return new File(SongPlayer.SONG_DIR, name);
|
||||
private Path getSongFile(String name) {
|
||||
System.out.println(SongPlayer.SONG_DIR.resolve(name));
|
||||
System.out.println(Files.exists(SongPlayer.SONG_DIR.resolve(name)));
|
||||
return SongPlayer.SONG_DIR.resolve(name);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue