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