From 6e8d345776efd08a0910628855b5f9adb8609737 Mon Sep 17 00:00:00 2001 From: Navoei Date: Fri, 8 Jul 2022 00:29:20 -0500 Subject: [PATCH] Command Lore Command now sets the lore for the disc. --- .../Navoei/customdiscsplugin/PlayMusic.java | 15 ++- .../customdiscsplugin/command/CustomDisc.java | 118 +++++++++++++++++- 2 files changed, 125 insertions(+), 8 deletions(-) diff --git a/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java b/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java index 3fdcf3b..2b8913f 100644 --- a/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java +++ b/src/main/java/na/Navoei/customdiscsplugin/PlayMusic.java @@ -2,8 +2,13 @@ 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 { /** @@ -20,7 +25,7 @@ public class PlayMusic implements VoicechatPlugin { * @param api the voice chat API */ @Override - public void initialize(VoicechatApi api) { + public void initialize(final VoicechatApi api) { } @@ -30,8 +35,12 @@ public class PlayMusic implements VoicechatPlugin { * @param registration the event registration */ @Override - public void registerEvents(EventRegistration registration) { - // TODO register your events + public void registerEvents(final EventRegistration registration) { + + } + + public void playSoundFile() { + } } diff --git a/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java b/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java index 446a090..31592bb 100644 --- a/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java +++ b/src/main/java/na/Navoei/customdiscsplugin/command/CustomDisc.java @@ -1,13 +1,25 @@ package na.Navoei.customdiscsplugin.command; -import +import na.Navoei.customdiscsplugin.CustomDiscs; +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.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemFlag; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.ArrayList; +import java.util.List; public class CustomDisc implements CommandExecutor { @@ -23,17 +35,113 @@ public class CustomDisc implements CommandExecutor { // /customdisc wewontbealone "We wont be alone" if (command.getName().equalsIgnoreCase("customdisc")) { - if (args.length == 2) { + if (isMusicDisc(p)) { + if (args.length >= 2) { + //Find file, if file not there then say "file not there" + String filename = args[0]; - p.sendMessage("Your filename is:" + args[0]); - p.sendMessage("Your custom name is " + args[1]); + //Reads the command for quotations. + ArrayList 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 = ""; + } + + //Sets the lore of the item to the quotes from the command. + ItemStack disc = new ItemStack(p.getInventory().getItemInMainHand()); + ItemMeta meta = disc.getItemMeta(); + @Nullable List itemLore = new ArrayList<>(); + final TextComponent customLoreSong = Component.text() + .decoration(TextDecoration.ITALIC, false) + .content(customName(quotes)) + .color(NamedTextColor.GRAY) + .build(); + itemLore.add(customLoreSong); + final TextComponent customLoreFile = Component.text() + .content(filename) + .color(NamedTextColor.DARK_GRAY) + .build(); + itemLore.add(customLoreFile); + meta.lore(itemLore); + meta.addItemFlags(ItemFlag.values()); + p.getInventory().getItemInMainHand().setItemMeta(meta); + + + p.sendMessage("Your filename is: " + filename); + p.sendMessage("Your custom name is: " + customName(quotes)); + + return true; + + } else { + p.sendMessage(ChatColor.RED + "Insufficient arguments! ( /customdisc [\"customname\"] )"); + return true; + } } else { - p.sendMessage(ChatColor.RED + "Incorrect arguments! ( /customdisc [customname] )"); + p.sendMessage(ChatColor.RED + "You are not holding a music disc in your main hand!"); } } return false; } + + private String customName(ArrayList 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; + } + }