mirror of
https://github.com/SPAWNRYS-ban/FUCK-CustomDiscs.git
synced 2026-03-26 22:18:13 +05:00
Seemless Update
This commit is contained in:
@@ -0,0 +1,171 @@
|
||||
package me.Navoei.customdiscsplugin.command.SubCommands;
|
||||
|
||||
import me.Navoei.customdiscsplugin.CustomDiscs;
|
||||
import me.Navoei.customdiscsplugin.command.SubCommand;
|
||||
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 org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.NamespacedKey;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
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 ChatColor.GREEN + "/customdisc create <filename> \"Custom Lore\".";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void perform(Player player, String[] args) {
|
||||
if (isMusicDisc(player)) {
|
||||
if (args.length == 3) {
|
||||
|
||||
// /cd create test.mp3 "test"
|
||||
//Find file, if file not there then say "file not there"
|
||||
String songname = "";
|
||||
String filename = args[1];
|
||||
|
||||
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")) {
|
||||
songname = args[1];
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "File is not in wav or mp3 format!");
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "File not found!");
|
||||
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);
|
||||
|
||||
PersistentDataContainer data = meta.getPersistentDataContainer();
|
||||
data.set(new NamespacedKey(CustomDiscs.getInstance(), "customdisc"), PersistentDataType.STRING, filename);
|
||||
|
||||
player.getInventory().getItemInMainHand().setItemMeta(meta);
|
||||
|
||||
player.sendMessage("Your filename is: " + ChatColor.GRAY + songname);
|
||||
player.sendMessage("Your custom name is: " + ChatColor.GRAY + customName(readQuotes(args)));
|
||||
|
||||
} else if (args.length <= 3) {
|
||||
player.sendMessage(ChatColor.RED + "Insufficient arguments! ( /customdisc create <filename> \"Custom Lore\" )");
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "Too many arguments! ( /customdisc create <filename> \"Custom Lore\" )");
|
||||
}
|
||||
} else {
|
||||
player.sendMessage(ChatColor.RED + "You are not holding a music disc in your main hand!");
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
|
||||
if ( p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_13) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_CAT) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_BLOCKS) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_CHIRP) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_FAR) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_MALL) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_MELLOHI) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_STAL) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_STRAD) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_WARD) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_11) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_WAIT) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_OTHERSIDE) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_5) ||
|
||||
p.getInventory().getItemInMainHand().getType().equals(Material.MUSIC_DISC_PIGSTEP)
|
||||
)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user