diff --git a/pom.xml b/pom.xml index f54d694..9f6e1a3 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.biligoldenwater MidiPlayer - v1.1.1-beta + v1.1.2-beta jar MidiPlayer diff --git a/src/main/java/io/github/biligoldenwater/midiplayer/MidiPlayer.java b/src/main/java/io/github/biligoldenwater/midiplayer/MidiPlayer.java index 3a29fc7..9f8b17e 100644 --- a/src/main/java/io/github/biligoldenwater/midiplayer/MidiPlayer.java +++ b/src/main/java/io/github/biligoldenwater/midiplayer/MidiPlayer.java @@ -7,18 +7,57 @@ import io.github.biligoldenwater.midiplayer.listener.OnPlayerQuitEvent; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; + public final class MidiPlayer extends JavaPlugin { private static MidiPlayer instance; - private static String musicsPathName; + private static File musicsPath; @Override public void onEnable() { // Plugin startup logic // Init plugin - saveDefaultConfig(); + saveDefaultConfig(); // 创建默认配置文件 instance = this; - musicsPathName = getDataFolder().getPath() + "\\musics"; + + File[] filesInDataFolder = getDataFolder().listFiles(); // 获取数据文件夹内所有目录和文件 + if(filesInDataFolder != null){ // 如果数据文件夹不为空 + boolean isMusicsPathExists = false; + + for (File file : filesInDataFolder){ + if (file.getName().equalsIgnoreCase("musics") && file.isDirectory()){ // 如果名字为music并且为一个目录则 + musicsPath = file; + isMusicsPathExists = true; + break; + + } else if (file.getName().equalsIgnoreCase("musics") && !file.isDirectory()){ // 如果名字为music并且不为一个目录则 + if (file.delete()){ // 删除该文件 + if(file.mkdir()){ // 创建目录 + musicsPath = file; + isMusicsPathExists = true; + break; + + } else { + getLogger().warning("Fail to create musics folder."); // 创建失败 + + } + + } else { + getLogger().warning("Fail to delete a same name file for musics folder."); // 删除失败 + + } + } + } + if (!isMusicsPathExists){ + musicsPath = new File(getDataFolder().getPath() + "/musics"); + if(!musicsPath.mkdirs()){ + getLogger().warning("Fail to create musics folder."); + musicsPath = null; + } + } + } + getServer().getPluginManager().registerEvents(new OnPlayerQuitEvent(),this); CommandMidiPlayer.registerCommandMidiPlayer(); TabMidiPlayer.registerTabMidiPlayer(); @@ -55,8 +94,8 @@ public void onDisable() { getLogger().info("Disabled");//输出已禁用消息到日志 } - public static String getMusicsPathName(){ - return musicsPathName; + public static File getMusicsPath(){ + return musicsPath; } public static MidiPlayer getInstance(){ diff --git a/src/main/java/io/github/biligoldenwater/midiplayer/commands/CommandMidiPlayer.java b/src/main/java/io/github/biligoldenwater/midiplayer/commands/CommandMidiPlayer.java index 7990162..709c6f6 100644 --- a/src/main/java/io/github/biligoldenwater/midiplayer/commands/CommandMidiPlayer.java +++ b/src/main/java/io/github/biligoldenwater/midiplayer/commands/CommandMidiPlayer.java @@ -38,7 +38,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St return true; case "list": if (!CheckPermissions.hasPermissions(sender, "midiplayer.commands.list")) return true; - List midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件 + List midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件 sendMidisListWithMultiplePage(sender,midis,1); // 发送列表 return true; @@ -158,7 +158,7 @@ public boolean onCommand(CommandSender sender, Command command, String label, St case 2: if(args[0].equals("list")){ if (!CheckPermissions.hasPermissions(sender, "midiplayer.commands.list")) return true; - List midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件 + List midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件 int page; @@ -303,6 +303,7 @@ private static void sendMidiIndexAndName(CommandSender sender, int i, List String fileName = midis.get(i).getName(); // 获取文件名 fileName = fileName.replaceAll(".mid", ""); // 删除后缀 fileName = fileName.replace(".midi", ""); + //fileName = SplitString.splitString("\\",fileName)[SplitString.splitString("\\",fileName).length-1]; JsonMessage message = new JsonMessage("Index: " + i + " Name: " + fileName + " ["); @@ -373,7 +374,7 @@ private static void playMidi(JavaPlugin plugin, CommandSender sender, int index, sender.sendMessage("Stopped."); } - List midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件 + List midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件 if(midis.size()-1 >= index){ // 如果索引不大于总midi数量则 FileConfiguration config = plugin.getConfig(); diff --git a/src/main/java/io/github/biligoldenwater/midiplayer/commands/TabMidiPlayer.java b/src/main/java/io/github/biligoldenwater/midiplayer/commands/TabMidiPlayer.java index 1006e2f..8e9e0c3 100644 --- a/src/main/java/io/github/biligoldenwater/midiplayer/commands/TabMidiPlayer.java +++ b/src/main/java/io/github/biligoldenwater/midiplayer/commands/TabMidiPlayer.java @@ -31,7 +31,7 @@ public List onTabComplete(CommandSender sender, Command command, String return completions; case 2: if(args[0].equals("play")) { - for (int i = 0; i < GetMidis.getMidis(MidiPlayer.getMusicsPathName()).size(); ++i) { + for (int i = 0; i < GetMidis.getMidis(MidiPlayer.getMusicsPath()).size(); ++i) { if(String.valueOf(i).startsWith(args[1])){ completions.add(String.valueOf(i)); } diff --git a/src/main/java/io/github/biligoldenwater/midiplayer/modules/GetMidis.java b/src/main/java/io/github/biligoldenwater/midiplayer/modules/GetMidis.java index b5bc1c5..d4ad3a2 100644 --- a/src/main/java/io/github/biligoldenwater/midiplayer/modules/GetMidis.java +++ b/src/main/java/io/github/biligoldenwater/midiplayer/modules/GetMidis.java @@ -1,37 +1,22 @@ package io.github.biligoldenwater.midiplayer.modules; -import io.github.biligoldenwater.midiplayer.MidiPlayer; - import java.io.File; import java.util.ArrayList; +import java.util.Collections; import java.util.List; public class GetMidis { - public static List getMidis(String path) { + public static List getMidis(File path) { List midis = new ArrayList<>(); - File musics = new File(path); - - if(musics.exists() && !musics.isDirectory()){ - if (!musics.delete()){ - MidiPlayer.getInstance().getLogger().warning("Fail to delete a same name file for musics folder."); - } - } - if(!musics.exists()){ - if (!musics.mkdir()){ - MidiPlayer.getInstance().getLogger().warning("Fail to create musics folder."); - } - } - - String[] musicNames = musics.list(); + File[] musicNames = path.listFiles(); if (musicNames == null || musicNames.length < 1){ return midis; } - for(String fileName : musicNames){ - midis.add(new File(path+"\\"+fileName)); - } + Collections.addAll(midis, musicNames); + return midis; } }