Skip to content

Commit

Permalink
Support for linux folder path
Browse files Browse the repository at this point in the history
  • Loading branch information
BillGoldenWater committed Aug 7, 2020
1 parent a3c1efe commit c34d964
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.biligoldenwater</groupId>
<artifactId>MidiPlayer</artifactId>
<version>v1.1.1-beta</version>
<version>v1.1.2-beta</version>
<packaging>jar</packaging>

<name>MidiPlayer</name>
Expand Down
49 changes: 44 additions & 5 deletions src/main/java/io/github/biligoldenwater/midiplayer/MidiPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件
List<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件

sendMidisListWithMultiplePage(sender,midis,1); // 发送列表
return true;
Expand Down Expand Up @@ -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<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件
List<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件

int page;

Expand Down Expand Up @@ -303,6 +303,7 @@ private static void sendMidiIndexAndName(CommandSender sender, int i, List<File>
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 + " [");

Expand Down Expand Up @@ -373,7 +374,7 @@ private static void playMidi(JavaPlugin plugin, CommandSender sender, int index,
sender.sendMessage("Stopped.");
}

List<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPathName()); // 获取所有Midi文件
List<File> midis = GetMidis.getMidis(MidiPlayer.getMusicsPath()); // 获取所有Midi文件

if(midis.size()-1 >= index){ // 如果索引不大于总midi数量则
FileConfiguration config = plugin.getConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public List<String> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<File> getMidis(String path) {
public static List<File> getMidis(File path) {
List<File> 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;
}
}

0 comments on commit c34d964

Please sign in to comment.