Switch to CommandAPI & Add auto-complete for songs

This commit is contained in:
lowercasebtw
2024-08-12 06:49:41 -04:00
parent 860ab2e30a
commit 1c3b8523cc
12 changed files with 447 additions and 535 deletions

View File

@@ -1,6 +1,6 @@
plugins {
id 'java'
id "com.github.johnrengelman.shadow" version "7.1.0"
id 'io.github.goooler.shadow' version '8.1.7'
}
sourceCompatibility = JavaLanguageVersion.of(java_version as int)
@@ -38,6 +38,7 @@ dependencies {
// Use this dependency if you don't want to compile bukkit
implementation "io.papermc.paper:paper-api:${bukkit_version}"
implementation "de.maxhenkel.voicechat:voicechat-api:${voicechat_api_version}"
shadow "dev.jorel:commandapi-bukkit-shade:${command_api_version}"
compileOnly group: "com.comphenix.protocol", name: "ProtocolLib", version: "5.1.0";
@@ -55,12 +56,17 @@ repositories {
url = uri("https://papermc.io/repo/repository/maven-public/")
}
maven { url "https://repo.dmulloy2.net/repository/public/" }
maven { url = "https://repo.codemc.org/repository/maven-public/" }
mavenLocal()
}
shadowJar {
configurations = [project.configurations.shadow]
classifier 'shadow-dev'
archiveClassifier.set("shadow-dev")
//relocate 'javazoom', "me.navoei.${mod_id}.javazoom"
//relocate 'org.tritonus', "me.navoei.${mod_id}.tritonus"
// By documentation, it was recommented to relocate to not cause issues with other plugins that shade CommandAPI
relocate("dev.jorel.commandapi", "me.navoei.${mod_id}.commandapi")
}

View File

@@ -10,6 +10,7 @@ mod_id=customdiscsplugin
# Target an older API to make it compatible with older versions of Simple Voice Chat
voicechat_api_version=2.5.0
command_api_version=9.5.2
plugin_version=3.0
maven_group=me.Navoei.customdiscsplugin

View File

@@ -1,5 +1,6 @@
#Sun Aug 11 17:52:06 EDT 2024
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

View File

@@ -8,10 +8,10 @@ import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import de.maxhenkel.voicechat.api.BukkitVoicechatService;
import me.Navoei.customdiscsplugin.command.CommandManager;
import dev.jorel.commandapi.CommandAPI;
import dev.jorel.commandapi.CommandAPIBukkitConfig;
import me.Navoei.customdiscsplugin.command.CustomDiscCommand;
import me.Navoei.customdiscsplugin.event.JukeBox;
import java.util.logging.Logger;
import me.Navoei.customdiscsplugin.language.Lang;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Jukebox;
@@ -23,6 +23,7 @@ import javax.annotation.Nullable;
import java.io.*;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
public final class CustomDiscs extends JavaPlugin {
static CustomDiscs instance;
@@ -36,11 +37,18 @@ public final class CustomDiscs extends JavaPlugin {
public float musicDiscVolume;
@Override
public void onEnable() {
public void onLoad() {
CustomDiscs.instance = this;
CommandAPI.onLoad(new CommandAPIBukkitConfig(this).verboseOutput(true));
new CustomDiscCommand(this).register("customdiscs");
}
@Override
public void onEnable() {
log = getLogger();
CommandAPI.onEnable();
BukkitVoicechatService service = getServer().getServicesManager().load(BukkitVoicechatService.class);
this.saveDefaultConfig();
@@ -61,7 +69,6 @@ public final class CustomDiscs extends JavaPlugin {
getServer().getPluginManager().registerEvents(new JukeBox(), this);
getServer().getPluginManager().registerEvents(new HopperManager(), this);
getCommand("customdisc").setExecutor(new CommandManager());
musicDiscDistance = getConfig().getInt("music-disc-distance");
musicDiscVolume = Float.parseFloat(Objects.requireNonNull(getConfig().getString("music-disc-volume")));
@@ -93,6 +100,7 @@ public final class CustomDiscs extends JavaPlugin {
@Override
public void onDisable() {
CommandAPI.onDisable();
if (voicechatPlugin != null) {
getServer().getServicesManager().unregister(voicechatPlugin);
log.info("Successfully unregistered CustomDiscs plugin");
@@ -105,6 +113,7 @@ public final class CustomDiscs extends JavaPlugin {
/**
* Load the lang.yml file.
*
* @return The lang.yml config.
*/
public void loadLang() {
@@ -120,7 +129,7 @@ public final class CustomDiscs extends JavaPlugin {
defConfig.save(lang);
Lang.setFile(defConfig);
}
} catch(IOException e) {
} catch (IOException e) {
e.printStackTrace(); // So they notice
log.severe("Failed to create lang.yml for MyHomes.");
log.severe("Now disabling...");
@@ -128,7 +137,7 @@ public final class CustomDiscs extends JavaPlugin {
}
}
YamlConfiguration conf = YamlConfiguration.loadConfiguration(lang);
for(Lang item:Lang.values()) {
for (Lang item : Lang.values()) {
if (conf.getString(item.getPath()) == null) {
conf.set(item.getPath(), item.getDefault());
}
@@ -138,7 +147,7 @@ public final class CustomDiscs extends JavaPlugin {
LANG_FILE = lang;
try {
conf.save(getLangFile());
} catch(IOException e) {
} catch (IOException e) {
log.log(Level.WARNING, "Failed to save lang.yml for MyHomes");
log.log(Level.WARNING, "Now disabling...");
e.printStackTrace();
@@ -147,6 +156,7 @@ public final class CustomDiscs extends JavaPlugin {
/**
* Gets the lang.yml config.
*
* @return The lang.yml config.
*/
public YamlConfiguration getLang() {
@@ -155,6 +165,7 @@ public final class CustomDiscs extends JavaPlugin {
/**
* Get the lang.yml file.
*
* @return The lang.yml file.
*/
public File getLangFile() {

View File

@@ -1,77 +0,0 @@
package me.Navoei.customdiscsplugin.command;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.command.SubCommands.CreateCommand;
import me.Navoei.customdiscsplugin.command.SubCommands.DownloadCommand;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public class CommandManager implements CommandExecutor, TabCompleter {
private final ArrayList<SubCommand> subCommands = new ArrayList<>();
public CommandManager() {
subCommands.add(new CreateCommand());
subCommands.add(new DownloadCommand());
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command!");
return true;
}
Player player = (Player) sender;
if (args.length > 0) {
for (int i = 0; i < getSubCommands().size(); i++) {
if (args[0].equalsIgnoreCase(getSubCommands().get(i).getName())) {
getSubCommands().get(i).perform(player, args);
}
}
} else {
FileConfiguration config = CustomDiscs.getInstance().getConfig();
List<String> messagesList = config.getStringList("help");
for (String s : messagesList) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(s);
player.sendMessage(textComponent);
}
return true;
}
return true;
}
public ArrayList<SubCommand> getSubCommands() {
return subCommands;
}
@Override
public @Nullable List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
if (args.length == 1) {
List<String> arguments = new ArrayList<>();
for (int i = 0; i < getSubCommands().size(); i++) {
arguments.add(getSubCommands().get(i).getName());
}
return arguments;
}
return null;
}
}

View File

@@ -0,0 +1,44 @@
package me.Navoei.customdiscsplugin.command;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.executors.CommandArguments;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.command.SubCommands.CreateSubCommand;
import me.Navoei.customdiscsplugin.command.SubCommands.DownloadSubCommand;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
public class CustomDiscCommand extends CommandAPICommand {
private final CustomDiscs plugin;
public CustomDiscCommand(CustomDiscs plugin) {
super("customdisc");
this.plugin = plugin;
this.withAliases("cd");
this.withFullDescription("The custom discs command.");
this.withSubcommand(new CreateSubCommand(plugin));
this.withSubcommand(new DownloadSubCommand(plugin));
this.executesPlayer(this::onCommandPlayer);
this.executesConsole(this::onCommandConsole);
}
private int onCommandPlayer(Player player, CommandArguments arguments) {
FileConfiguration config = this.plugin.getConfig();
for (String message : config.getStringList("help")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(message));
}
return 1;
}
private int onCommandConsole(ConsoleCommandSender executor, CommandArguments arguments) {
executor.sendMessage(ChatColor.RED + "Only players can use this command!");
return 1;
}
}

View File

@@ -1,15 +0,0 @@
package me.Navoei.customdiscsplugin.command;
import org.bukkit.entity.Player;
public abstract class SubCommand {
public abstract String getName();
public abstract String getDescription();
public abstract String getSyntax();
public abstract void perform(Player player, String[] args);
}

View File

@@ -1,178 +0,0 @@
package me.Navoei.customdiscsplugin.command.SubCommands;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.command.SubCommand;
import me.Navoei.customdiscsplugin.language.Lang;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.JukeboxPlayableComponent;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class CreateCommand extends SubCommand {
@Override
public String getName() {
return "create";
}
@Override
public String getDescription() {
return ChatColor.GRAY + "Creates a custom music disc.";
}
@Override
public String getSyntax() {
return "/customdisc create <filename> \"Custom Lore\"";
}
@Override
public void perform(Player player, String[] args) {
if (isMusicDisc(player)) {
if (args.length >= 3) {
if (!player.hasPermission("customdiscs.create")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NO_PERMISSION.toString());
player.sendMessage(textComponent);
return;
}
// /cd create test.mp3 "test"
// [0] [1] [2]
//Find file, if file not there then say "file not there"
String song_name = "";
String filename = args[1];
if (filename.contains("../")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FILENAME.toString());
player.sendMessage(textComponent);
return;
}
if (customName(readQuotes(args)).equalsIgnoreCase("")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NO_DISC_NAME_PROVIDED.toString());
player.sendMessage(textComponent);
return;
}
File getDirectory = new File(CustomDiscs.getInstance().getDataFolder(), "musicdata");
File songFile = new File(getDirectory.getPath(), filename);
if (songFile.exists()) {
if (getFileExtension(filename).equals("wav") || getFileExtension(filename).equals("mp3") || getFileExtension(filename).equals("flac")) {
song_name = args[1];
} else {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FORMAT.toString());
player.sendMessage(textComponent);
return;
}
} else {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.FILE_NOT_FOUND.toString());
player.sendMessage(textComponent);
return;
}
//Sets the lore of the item to the quotes from the command.
ItemStack disc = new ItemStack(player.getInventory().getItemInMainHand());
ItemMeta meta = disc.getItemMeta();
@Nullable List<Component> itemLore = new ArrayList<>();
final TextComponent customLoreSong = Component.text()
.decoration(TextDecoration.ITALIC, false)
.content(customName(readQuotes(args)))
.color(NamedTextColor.GRAY)
.build();
itemLore.add(customLoreSong);
meta.lore(itemLore);
JukeboxPlayableComponent jpc = meta.getJukeboxPlayable();
jpc.setShowInTooltip(false);
meta.setJukeboxPlayable(jpc);
PersistentDataContainer data = meta.getPersistentDataContainer();
data.set(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING, filename);
player.getInventory().getItemInMainHand().setItemMeta(meta);
Component textComponentFileName = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_FILENAME.toString().replace("%filename%", song_name));
Component textComponentCustomName = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_CUSTOM_NAME.toString().replace("%custom_name%", customName(readQuotes(args))));
player.sendMessage(textComponentFileName);
player.sendMessage(textComponentCustomName);
} else {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_ARGUMENTS.toString().replace("%command_syntax", getSyntax()));
player.sendMessage(textComponent);
}
} else {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NOT_HOLDING_DISC.toString());
player.sendMessage(textComponent);
}
}
private String getFileExtension(String s) {
int index = s.lastIndexOf(".");
if (index > 0) {
return s.substring(index + 1);
} else {
return "";
}
}
private ArrayList<String> readQuotes(String[] args) {
ArrayList<String> quotes = new ArrayList<>();
String temp = "";
boolean inQuotes = false;
for (String s : args) {
if (s.startsWith("\"") && s.endsWith("\"")) {
temp += s.substring(1, s.length()-1);
quotes.add(temp);
} else if (s.startsWith("\"")) {
temp += s.substring(1);
quotes.add(temp);
inQuotes = true;
} else if (s.endsWith("\"")) {
temp += s.substring(0, s.length()-1);
quotes.add(temp);
inQuotes = false;
} else if (inQuotes) {
temp += s;
quotes.add(temp);
}
temp = "";
}
return quotes;
}
private String customName(ArrayList<String> q) {
StringBuffer sb = new StringBuffer();
for (String s : q) {
sb.append(s);
sb.append(" ");
}
if (sb.isEmpty()) {
return sb.toString();
} else {
return sb.toString().substring(0, sb.length()-1);
}
}
private boolean isMusicDisc(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("MUSIC_DISC");
}
}

View File

@@ -0,0 +1,132 @@
package me.Navoei.customdiscsplugin.command.SubCommands;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.ArgumentSuggestions;
import dev.jorel.commandapi.arguments.StringArgument;
import dev.jorel.commandapi.arguments.TextArgument;
import dev.jorel.commandapi.executors.CommandArguments;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.language.Lang;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.NamespacedKey;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.components.JukeboxPlayableComponent;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;
import javax.annotation.Nullable;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
public class CreateSubCommand extends CommandAPICommand {
private final CustomDiscs plugin;
public CreateSubCommand(CustomDiscs plugin) {
super("create");
this.plugin = plugin;
this.withFullDescription(ChatColor.GRAY + "Creates a custom music disc.");
this.withUsage("/customdisc create <filename> \"Custom Lore\"");
this.withArguments(new StringArgument("filename").replaceSuggestions(ArgumentSuggestions.stringCollection((sender) -> {
File musicDataFolder = new File(this.plugin.getDataFolder(), "musicdata");
if (!musicDataFolder.isDirectory()) {
return List.of();
}
File[] files = musicDataFolder.listFiles();
if (files == null) {
return List.of();
}
return Arrays.stream(files).filter(file -> !file.isDirectory()).map(File::getName).toList();
})));
this.withArguments(new TextArgument("song_name"));
this.executesPlayer(this::onCommandPlayer);
this.executesConsole(this::onCommandConsole);
}
private int onCommandPlayer(Player player, CommandArguments arguments) {
if (!isMusicDisc(player)) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NOT_HOLDING_DISC.toString()));
return 1;
}
if (!player.hasPermission("customdiscs.create")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NO_PERMISSION.toString()));
return 1;
}
// Find file, if file not there then say "file not there"
String filename = Objects.requireNonNull(arguments.getByClass("filename", String.class));
if (filename.contains("../")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FILENAME.toString()));
return 1;
}
File getDirectory = new File(this.plugin.getDataFolder(), "musicdata");
File songFile = new File(getDirectory.getPath(), filename);
if (songFile.exists()) {
if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") && !getFileExtension(filename).equals("flac")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FORMAT.toString()));
return 1;
}
} else {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.FILE_NOT_FOUND.toString()));
return 1;
}
String song_name = Objects.requireNonNull(arguments.getByClass("song_name", String.class));
// Sets the lore of the item to the quotes from the command.
ItemStack disc = new ItemStack(player.getInventory().getItemInMainHand());
ItemMeta meta = disc.getItemMeta();
@Nullable List<Component> itemLore = new ArrayList<>();
final TextComponent customLoreSong = Component.text().decoration(TextDecoration.ITALIC, false).content(song_name).color(NamedTextColor.GRAY).build();
itemLore.add(customLoreSong);
meta.lore(itemLore);
JukeboxPlayableComponent jpc = meta.getJukeboxPlayable();
jpc.setShowInTooltip(false);
meta.setJukeboxPlayable(jpc);
PersistentDataContainer data = meta.getPersistentDataContainer();
data.set(new NamespacedKey(this.plugin, "customdisc"), PersistentDataType.STRING, filename);
player.getInventory().getItemInMainHand().setItemMeta(meta);
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_FILENAME.toString().replace("%filename%", filename)));
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_CUSTOM_NAME.toString().replace("%custom_name%", song_name)));
return 1;
}
private int onCommandConsole(ConsoleCommandSender executor, CommandArguments arguments) {
executor.sendMessage(ChatColor.RED + "Only players can use this command!");
return 1;
}
private String getFileExtension(String s) {
int index = s.lastIndexOf(".");
if (index > 0) {
return s.substring(index + 1);
} else {
return "";
}
}
private boolean isMusicDisc(Player p) {
return p.getInventory().getItemInMainHand().getType().toString().contains("MUSIC_DISC");
}
}

View File

@@ -1,113 +0,0 @@
package me.Navoei.customdiscsplugin.command.SubCommands;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.command.SubCommand;
import me.Navoei.customdiscsplugin.language.Lang;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
public class DownloadCommand extends SubCommand {
CustomDiscs customDiscs = CustomDiscs.getInstance();
@Override
public String getName() {
return "download";
}
@Override
public String getDescription() {
return ChatColor.GRAY + "Downloads a file from a given URL.";
}
@Override
public String getSyntax() {
return "/customdisc download <url> <filename.extension>";
}
@Override
public void perform(Player player, String[] args) {
// /cd download url filename
// [0] [1] [2]
if (!player.hasPermission("customdiscs.download")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NO_PERMISSION.toString());
player.sendMessage(textComponent);
return;
}
if (args.length!=3) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_ARGUMENTS.toString().replace("%command_syntax%", getSyntax()));
player.sendMessage(textComponent);
return;
}
Bukkit.getScheduler().runTaskAsynchronously(customDiscs, () -> {
try {
URL fileURL = new URL(args[1]);
String filename = args[2];
if (filename.contains("../")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FILENAME.toString());
player.sendMessage(textComponent);
return;
}
System.out.println(filename);
if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") && !getFileExtension(filename).equals("flac")) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FORMAT.toString());
player.sendMessage(textComponent);
return;
}
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.DOWNLOADING_FILE.toString());
player.sendMessage(textComponent);
Path downloadPath = Path.of(customDiscs.getDataFolder().getPath(), "musicdata", filename);
File downloadFile = new File(downloadPath.toUri());
URLConnection connection = fileURL.openConnection();
if (connection != null) {
long size = connection.getContentLengthLong() / 1048576;
if (size > customDiscs.getConfig().getInt("max-download-size", 50)) {
Component textComponent2 = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.FILE_TOO_LARGE.toString().replace("%max_download_size%", String.valueOf(customDiscs.getConfig().getInt("max-download-size", 50))));
player.sendMessage(textComponent2);
return;
}
}
FileUtils.copyURLToFile(fileURL, downloadFile);
Component fileDownloaded = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.SUCCESSFUL_DOWNLOAD.toString().replace("%file_path%", "plugins/CustomDiscs/musicdata/" + filename));
Component createDisc = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_DISC.toString().replace("%filename%", filename));
player.sendMessage(fileDownloaded);
player.sendMessage(createDisc);
} catch (IOException e) {
Component textComponent = LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.DOWNLOAD_ERROR.toString());
player.sendMessage(textComponent);
e.printStackTrace();
}
});
}
private String getFileExtension(String s) {
int index = s.lastIndexOf(".");
if (index > 0) {
return s.substring(index + 1);
} else {
return "";
}
}
}

View File

@@ -0,0 +1,101 @@
package me.Navoei.customdiscsplugin.command.SubCommands;
import dev.jorel.commandapi.CommandAPICommand;
import dev.jorel.commandapi.arguments.StringArgument;
import dev.jorel.commandapi.arguments.TextArgument;
import dev.jorel.commandapi.executors.CommandArguments;
import me.Navoei.customdiscsplugin.CustomDiscs;
import me.Navoei.customdiscsplugin.language.Lang;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
import org.codehaus.plexus.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.Objects;
public class DownloadSubCommand extends CommandAPICommand {
private final CustomDiscs plugin;
public DownloadSubCommand(CustomDiscs plugin) {
super("download");
this.plugin = plugin;
this.withFullDescription(ChatColor.GRAY + "Downloads a file from a given URL.");
this.withUsage("/customdisc download <url> <filename.extension>");
this.withArguments(new TextArgument("url"));
this.withArguments(new StringArgument("filename"));
this.executesPlayer(this::onCommandPlayer);
this.executesConsole(this::onCommandConsole);
}
private int onCommandPlayer(Player player, CommandArguments arguments) {
if (!player.hasPermission("customdiscs.download")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.NO_PERMISSION.toString()));
return 1;
}
Bukkit.getScheduler().runTaskAsynchronously(this.plugin, () -> {
try {
URL fileURL = new URL(Objects.requireNonNull(arguments.getByClass("url", String.class)));
String filename = Objects.requireNonNull(arguments.getByClass("filename", String.class));
if (filename.contains("../")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FILENAME.toString()));
return;
}
System.out.println(filename);
if (!getFileExtension(filename).equals("wav") && !getFileExtension(filename).equals("mp3") && !getFileExtension(filename).equals("flac")) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.INVALID_FORMAT.toString()));
return;
}
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.DOWNLOADING_FILE.toString()));
URLConnection connection = fileURL.openConnection();
if (connection != null) {
long size = connection.getContentLengthLong() / 1048576;
if (size > this.plugin.getConfig().getInt("max-download-size", 50)) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.FILE_TOO_LARGE.toString().replace("%max_download_size%", String.valueOf(this.plugin.getConfig().getInt("max-download-size", 50)))));
return;
}
}
Path downloadPath = Path.of(this.plugin.getDataFolder().getPath(), "musicdata", filename);
File downloadFile = new File(downloadPath.toUri());
FileUtils.copyURLToFile(fileURL, downloadFile);
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.SUCCESSFUL_DOWNLOAD.toString().replace("%file_path%", "plugins/CustomDiscs/musicdata/" + filename)));
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.CREATE_DISC.toString().replace("%filename%", filename)));
} catch (IOException e) {
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(Lang.PREFIX + Lang.DOWNLOAD_ERROR.toString()));
e.printStackTrace();
}
});
return 1;
}
private int onCommandConsole(ConsoleCommandSender executor, CommandArguments arguments) {
executor.sendMessage(ChatColor.RED + "Only players can use this command!");
return 1;
}
private String getFileExtension(String s) {
int index = s.lastIndexOf(".");
if (index > 0) {
return s.substring(index + 1);
} else {
return "";
}
}
}

View File

@@ -7,7 +7,6 @@ public enum Lang {
PREFIX("prefix", "&8[&6CustomDiscs&8]&r"),
NO_PERMISSION("no-permission", "&rYou do not have permission to execute this command."),
INVALID_FILENAME("invalid-filename", "&rThis is an invalid filename!"),
NO_DISC_NAME_PROVIDED("no-disc-name-provided", "&rYou must provide a name for your disc."),
INVALID_FORMAT("invalid-format", "&rFile must be in wav, flac, or mp3 format!"),
FILE_NOT_FOUND("file-not-found", "&rFile not found!"),
INVALID_ARGUMENTS("invalid-arguments", "&rInsufficient arguments. &7(&a%command_syntax%&7)"),