From c77026bfda619bd8b0f9ac3ed3123f4c6ab4e5df Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Thu, 20 Jul 2023 20:25:16 -0500 Subject: [PATCH] Finish converting command to MiniMessage --- .../dev/plex/itemizerx/ItemizerXCommand.java | 96 ++++++++---------- .../java/dev/plex/itemizerx/ItemizerXTab.java | 97 ++++++++++++------- 2 files changed, 105 insertions(+), 88 deletions(-) diff --git a/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java b/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java index 1af4816..aa66e73 100644 --- a/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java +++ b/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java @@ -4,12 +4,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import net.kyori.adventure.text.Component; -import net.kyori.adventure.text.format.NamedTextColor; import org.apache.commons.lang3.StringUtils; -import org.bukkit.ChatColor; import org.bukkit.Color; import org.bukkit.Material; import org.bukkit.NamespacedKey; @@ -685,12 +681,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length == 1) { - sender.sendMessage(colorize(""" - &3===============&f[&dEnchant Commands&f]&3=============== - &b/itemizer enchant add <&fname&b> <&flevel&b> &c- &6Add an enchant - &b/itemizer enchant remove <&fname&b> &c- &6Remove an enchant - &b/itemizer enchant list &c- &6List all item's enchants - &b/itemizer enchant listall &c- &6List all available enchants""")); + sender.sendMessage(mm.deserialize(""" + ===============[Enchant Commands]=============== + /itemizer enchant add <name> <level> - Add an enchant + /itemizer enchant remove <name> - Remove an enchant + /itemizer enchant list - List all item's enchants + /itemizer enchant listall - List all available enchants""")); return true; } if (!hasItem) @@ -709,13 +705,14 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length < 4) { - sender.sendMessage(colorize("&3===============&f[&dEnchant Commands&f]&3===============\n" + "&b/itemizer enchant add <&fname&b> <&flevel&b> &c- &6Add an enchant")); + sender.sendMessage(mm.deserialize("===============[Enchant Commands]===============")); + sender.sendMessage(mm.deserialize("/itemizer enchant add <name> <level> - Add an enchant")); return true; } final Enchantment ench = EnchantmentWrapper.getByKey(NamespacedKey.minecraft(args[2].toLowerCase())); if (ench == null) { - sender.sendMessage(colorize("&4The enchantment &f'" + args[2] + "&f' &4does not exist!")); + sender.sendMessage(mm.deserialize("The enchantment '" + args[2] + "' does not exist!")); return true; } Integer level = parseInt(sender, args[3]); @@ -724,7 +721,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase return true; } item.addUnsafeEnchantment(ench, level); - sender.sendMessage(colorize("&2The enchant &f'" + ench.getKey().getKey() + "' &2has been added to your item")); + sender.sendMessage(mm.deserialize("The enchant '" + ench.getKey().getKey() + "' has been added to your item")); return true; } case "remove" -> @@ -736,28 +733,29 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length == 2) { - sender.sendMessage(colorize("&3===============&f[&dEnchant Commands&f]&3===============\n" + "&b/itemizer enchant remove <&fname&b> &c- &6Remove an enchant")); + sender.sendMessage(mm.deserialize("===============[Enchant Commands]===============")); + sender.sendMessage(mm.deserialize("/itemizer enchant remove <name> - Remove an enchant")); return true; } final Enchantment ench = EnchantmentWrapper.getByKey(NamespacedKey.minecraft(args[2].toLowerCase())); if (ench == null) { - sender.sendMessage(colorize("&4The enchantment &f'" + args[2] + "&f' &4does not exist!")); + sender.sendMessage(mm.deserialize("The enchantment '" + args[2] + "' does not exist!")); return true; } assert meta != null; if (Objects.requireNonNull(meta.getEnchants()).isEmpty()) { - sender.sendMessage(colorize("&4This item doesn't hold any enchants")); + sender.sendMessage(mm.deserialize("This item doesn't hold any enchants")); return true; } if (!meta.getEnchants().containsKey(ench)) { - sender.sendMessage(colorize("&4This item doesn't have &f'" + ench.getKey().getKey() + "' &4enchant!")); + sender.sendMessage(mm.deserialize("This item doesn't have enchant!")); return true; } item.removeEnchantment(ench); - sender.sendMessage(colorize("&2The enchant &f'" + ench.getKey().getKey() + "' &2has been removed from your item")); + sender.sendMessage(mm.deserialize("The enchant '" + ench.getKey().getKey() + "' has been removed from your item")); return true; } case "list" -> @@ -770,10 +768,10 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase assert meta != null; if (Objects.requireNonNull(meta.getEnchants()).isEmpty()) { - sender.sendMessage(colorize("&4This item doesn't hold any enchants")); + sender.sendMessage(mm.deserialize("This item doesn't hold any enchants")); return true; } - sender.sendMessage(colorize("&2Item enchants: &e" + StringUtils.join(meta.getEnchants().keySet(), ", "))); + sender.sendMessage(mm.deserialize("Item enchants: " + StringUtils.join(meta.getEnchants().keySet(), ", "))); return true; } case "listall" -> @@ -789,12 +787,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase { sb.append(", ").append(enchantments[i].getKey().getKey()); } - sender.sendMessage(colorize("&2Available item enchants: &e" + sb.toString().replaceFirst(", ", ""))); + sender.sendMessage(mm.deserialize("Available item enchants: " + sb.toString().replaceFirst(", ", ""))); return true; } default -> { - sender.sendMessage(colorize("&bUnknown sub-command. Type &6/itemizer enchant &bfor help.")); + sender.sendMessage(mm.deserialize("Unknown sub-command. Type /itemizer enchant for help.")); return true; } } @@ -808,7 +806,8 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length == 1) { - sender.sendMessage(colorize("&3===============&f[&dTitle Command&f]&3===============\n" + "&b/itemizer title <&fname&b> &c- &6Set the book's title")); + sender.sendMessage(mm.deserialize("===============[Title Command]===============")); + sender.sendMessage(mm.deserialize("/itemizer title <name> - Set the book's title")); return true; } if (!hasBook) @@ -816,12 +815,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("You do not have a Written Book in your hand.")); return true; } - String name = colorize(StringUtils.join(args, " ", 1, args.length)); + Component name = mm.deserialize(StringUtils.join(args, " ", 1, args.length)); final BookMeta bookMeta = (BookMeta)meta; assert bookMeta != null; - bookMeta.setTitle(name); + bookMeta.title(name); item.setItemMeta(bookMeta); - sender.sendMessage(colorize("&2The title of the book has been set to &f'" + name + "&f'")); + sender.sendMessage(mm.deserialize("The title of the book has been set to '")); return true; } case "author" -> @@ -833,7 +832,8 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length == 1) { - sender.sendMessage(colorize("&3===============&f[&dAuthor Command&f]&3===============\n" + "&b/itemizer author <&fname&b> &c- &6Set the book's title")); + sender.sendMessage(mm.deserialize("===============[Author Command]===============")); + sender.sendMessage(mm.deserialize("/itemizer author <name> - Set the book's author")); return true; } if (!hasBook) @@ -841,12 +841,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("You do not have a Written Book in your hand.")); return true; } - String name = colorize(args[1]); + Component name = mm.deserialize(args[1]); final BookMeta bookMeta = (BookMeta)meta; assert bookMeta != null; - bookMeta.setAuthor(name); + bookMeta.author(name); item.setItemMeta(bookMeta); - sender.sendMessage(colorize("&2The author of the book has been set to &f'" + name + "&f'")); + sender.sendMessage(mm.deserialize("The author of the book has been set to '" + name + "'")); return true; } case "head" -> @@ -858,7 +858,8 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length == 1) { - sender.sendMessage(colorize("&3===============&f[&dHead Command&f]&3===============\n" + "&b/itemizer head <&fname&b> &c- &6Set the player of the head")); + sender.sendMessage(mm.deserialize("===============[Head Command]===============")); + sender.sendMessage(mm.deserialize("/itemizer head <name> - Set the player of the head")); return true; } if (item.getType() != Material.PLAYER_HEAD) @@ -875,7 +876,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase assert skullMeta != null; skullMeta.setOwner(name); item.setItemMeta(skullMeta); - sender.sendMessage(colorize("&2The player of the head has been set to &f'" + name + "&f'")); + sender.sendMessage(mm.deserialize("The player of the head has been set to '" + name + "'")); return true; } case "sign" -> @@ -887,13 +888,14 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } if (args.length < 3) { - sender.sendMessage(colorize("&3===============&f[&dSign Command&f]&3===============\n" + "&b/itemizer sign <&fline&b> <&ftext&b> &c- &6Change the line on the sign")); + sender.sendMessage(mm.deserialize("===============[Sign Commands]===============")); + sender.sendMessage(mm.deserialize("/itemizer sign <line> <text> - Change the line on the sign")); return true; } final Block block = player.getTargetBlockExact(20); if (block == null || block.getType() == Material.AIR || !block.getType().toString().contains("SIGN")) { - sender.sendMessage(colorize("&4Please look at a sign!")); + sender.sendMessage(mm.deserialize("Please look at a sign!")); return true; } Integer line = parseInt(sender, args[1]); @@ -903,22 +905,22 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } else if (line > 4) { - sender.sendMessage(colorize("&4There's maximum of 4 lines on a sign")); + sender.sendMessage(mm.deserialize("There's a maximum of 4 lines on a sign")); return true; } - String text = colorize(StringUtils.join(args, " ", 2, args.length)); + Component text = mm.deserialize(StringUtils.join(args, " ", 2, args.length)); if (cpb.getAPI() != null) { cpb.getAPI().logRemoval(player.getName(), block.getLocation(), block.getType(), block.getBlockData()); } Sign sign = (Sign)block.getState(); - sign.setLine(line - 1, text); + sign.line(line - 1, text); sign.update(); if (cpb.getAPI() != null) { cpb.getAPI().logPlacement(player.getName(), sign.getLocation(), sign.getType(), sign.getBlockData()); } - sender.sendMessage(colorize("&2Line &f'" + line + "'&2 has successfully changed to &f'" + text + "&f'")); + sender.sendMessage(mm.deserialize("Line '" + line + "' has successfully changed to '" + text + "'")); return true; } case "clearall" -> @@ -934,29 +936,17 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase return true; } item.setItemMeta(null); - sender.sendMessage(colorize("&2All data cleared from your item")); + sender.sendMessage(mm.deserialize("All data cleared from your item")); return true; } default -> { - sender.sendMessage(colorize("&bUnknown sub-command. Type &6/itemizer help &bfor help.")); + sender.sendMessage(mm.deserialize("Unknown sub-command. Type /itemizer help for help.")); return true; } } } - private String colorize(String string) - { - Matcher matcher = Pattern.compile("&#[a-fA-F0-9]{6}").matcher(string); - while (matcher.find()) - { - String code = matcher.group().replace("&", ""); - string = string.replace("&" + code, net.md_5.bungee.api.ChatColor.of(code) + ""); - } - string = ChatColor.translateAlternateColorCodes('&', string); - return string; - } - private Integer parseInt(CommandSender sender, String string) { try @@ -965,7 +955,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } catch (NumberFormatException ex) { - sender.sendMessage(colorize("&f\"" + string + "&f\"&4 is not a valid number!")); + sender.sendMessage(mm.deserialize("\"" + string + "\" is not a valid number!")); } return null; } diff --git a/src/main/java/dev/plex/itemizerx/ItemizerXTab.java b/src/main/java/dev/plex/itemizerx/ItemizerXTab.java index 0e87ae3..ba747de 100644 --- a/src/main/java/dev/plex/itemizerx/ItemizerXTab.java +++ b/src/main/java/dev/plex/itemizerx/ItemizerXTab.java @@ -1,5 +1,9 @@ package dev.plex.itemizerx; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; import org.bukkit.Material; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; @@ -8,93 +12,116 @@ import org.bukkit.enchantments.Enchantment; import org.bukkit.inventory.ItemFlag; import org.bukkit.potion.PotionEffectType; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; - -public class ItemizerXTab implements TabCompleter { - +public class ItemizerXTab implements TabCompleter +{ @Override - public List onTabComplete(CommandSender sender, Command cmd, String string, String[] args) { - if (args.length == 1) { + public List onTabComplete(CommandSender sender, Command cmd, String string, String[] args) + { + if (args.length == 1) + { return Arrays.asList("help", "id", "lore", "potion", "attr", "flag", "enchant", "title", "author", "head", "sign", "clearall"); } - if (args.length == 2) { - switch (args[0]) { - case "id": { + if (args.length == 2) + { + switch (args[0]) + { + case "id": + { List materials = new ArrayList<>(); - for (Material material : Material.values()) { + for (Material material : Material.values()) + { materials.add(material.name()); } return materials; } - case "lore": { + case "lore": + { return Arrays.asList("add", "remove", "change", "clear"); } - case "potion": { + case "potion": + { return Arrays.asList("add", "remove", "change", "color", "list"); } case "attr": case "flag": - case "enchant": { + case "enchant": + { return Arrays.asList("add", "remove", "list", "listall"); } - default: { + default: + { return Collections.emptyList(); } } } - if (args.length == 3) { - switch (args[0]) { - case "potion": { - switch (args[1]) { + if (args.length == 3) + { + switch (args[0]) + { + case "potion": + { + switch (args[1]) + { case "add": - case "remove": { + case "remove": + { List potions = new ArrayList<>(); - for (PotionEffectType effect : PotionEffectType.values()) { + for (PotionEffectType effect : PotionEffectType.values()) + { potions.add(effect.getName()); } return potions; } } } - case "attr": { - switch (args[1]) { + case "attr": + { + switch (args[1]) + { case "add": - case "remove": { + case "remove": + { return Attributes.getAttributeList(); } } } - case "flag": { - switch (args[1]) { + case "flag": + { + switch (args[1]) + { case "add": - case "remove": { + case "remove": + { List flags = new ArrayList<>(); - for (ItemFlag flag : ItemFlag.values()) { + for (ItemFlag flag : ItemFlag.values()) + { flags.add(flag.name()); } return flags; } } } - case "enchant": { - switch (args[1]) { + case "enchant": + { + switch (args[1]) + { case "add": - case "remove": { + case "remove": + { List enchantments = new ArrayList<>(); - for (Enchantment enchantment : Enchantment.values()) { + for (Enchantment enchantment : Enchantment.values()) + { enchantments.add(enchantment.getKey().getKey()); } return enchantments; } } } - default: { + default: + { return Collections.emptyList(); } }