File Reader

Check if the file exists and if it is in.wav format.
This commit is contained in:
Navoei
2022-07-08 11:24:46 -05:00
parent 6e8d345776
commit 3b1ecc9d40
3 changed files with 36 additions and 11 deletions

View File

@@ -8,17 +8,22 @@ import org.bukkit.plugin.java.JavaPlugin;
import javax.annotation.Nullable;
import java.io.File;
import java.net.URI;
public final class CustomDiscs extends JavaPlugin {
public static final String PLUGIN_ID = "CustomDiscs";
public static final Logger LOGGER = LogManager.getLogger(PLUGIN_ID);
static CustomDiscs instance;
@Nullable
private PlayMusic voicechatPlugin;
@Override
public void onEnable() {
CustomDiscs.instance = this;
BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);
CustomDisc command = new CustomDisc();
@@ -41,6 +46,7 @@ public final class CustomDiscs extends JavaPlugin {
LOGGER.info("Failed to register CustomDiscs plugin");
}
getCommand("customdisc").setExecutor(command);
}
@@ -52,4 +58,8 @@ public final class CustomDiscs extends JavaPlugin {
LOGGER.info("Successfully unregistered CustomDiscs plugin");
}
}
public static CustomDiscs getInstance() {
return instance;
}
}

View File

@@ -2,13 +2,8 @@ package na.Navoei.customdiscsplugin;
import de.maxhenkel.voicechat.api.VoicechatApi;
import de.maxhenkel.voicechat.api.VoicechatPlugin;
import de.maxhenkel.voicechat.api.audiochannel.AudioChannel;
import de.maxhenkel.voicechat.api.audiochannel.AudioPlayer;
import de.maxhenkel.voicechat.api.events.Event;
import de.maxhenkel.voicechat.api.events.EventRegistration;
import java.util.function.Consumer;
public class PlayMusic implements VoicechatPlugin {
/**
@@ -39,8 +34,4 @@ public class PlayMusic implements VoicechatPlugin {
}
public void playSoundFile() {
}
}

View File

@@ -1,5 +1,6 @@
package na.Navoei.customdiscsplugin.command;
import com.google.common.io.Files;
import na.Navoei.customdiscsplugin.CustomDiscs;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
@@ -18,6 +19,7 @@ import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -38,10 +40,23 @@ public class CustomDisc implements CommandExecutor {
if (isMusicDisc(p)) {
if (args.length >= 2) {
//Find file, if file not there then say "file not there"
String songname = "";
String filename = args[0];
File file = new File(CustomDiscs.getInstance().getDataFolder() + "\\musicdata\\" + filename);
if (file.exists()) {
if (getFileExtension(filename).equals("wav")) {
songname = args[0];
} else {
p.sendMessage(ChatColor.RED + "File is not in wav format!");
return true;
}
} else {
p.sendMessage(ChatColor.RED + "File not found!");
return true;
}
//Reads the command for quotations.
ArrayList<String> quotes = new ArrayList<>();
String temp = "";
@@ -86,7 +101,7 @@ public class CustomDisc implements CommandExecutor {
p.getInventory().getItemInMainHand().setItemMeta(meta);
p.sendMessage("Your filename is: " + filename);
p.sendMessage("Your filename is: " + songname);
p.sendMessage("Your custom name is: " + customName(quotes));
return true;
@@ -103,6 +118,15 @@ public class CustomDisc implements CommandExecutor {
return false;
}
private String getFileExtension(String s) {
int index = s.lastIndexOf(".");
if (index > 0) {
return s.substring(index + 1);
} else {
return "";
}
}
private String customName(ArrayList<String> q) {
StringBuffer sb = new StringBuffer();