From dfeaf2033dffc5f44c5d6879568845c4aa510025 Mon Sep 17 00:00:00 2001 From: Focusvity Date: Mon, 25 Sep 2023 16:14:43 +1000 Subject: [PATCH] Support legacy chat codes and minor fixes --- .../dev/plex/itemizerx/ItemizerXCommand.java | 59 +++++++++++++++---- 1 file changed, 49 insertions(+), 10 deletions(-) diff --git a/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java b/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java index 60db0e8..7f336de 100644 --- a/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java +++ b/src/main/java/dev/plex/itemizerx/ItemizerXCommand.java @@ -1,5 +1,9 @@ package dev.plex.itemizerx; +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.minimessage.MiniMessage; import org.apache.commons.lang3.StringUtils; @@ -31,6 +35,30 @@ import java.util.Objects; public class ItemizerXCommand implements CommandExecutor, ItemizerXBase { final List POTIONS = Arrays.asList(Material.POTION, Material.LINGERING_POTION, Material.SPLASH_POTION); + final Map COLOR_TRANSLATION = new HashMap<>(){{ + put("&a", ""); + put("&b", ""); + put("&c", ""); + put("&d", ""); + put("&e", ""); + put("&f", ""); + put("&k", ""); + put("&l", ""); + put("&m", ""); + put("&n", ""); + put("&o", ""); + put("&r", ""); + put("&1", ""); + put("&2", ""); + put("&3", ""); + put("&4", ""); + put("&5", ""); + put("&6", ""); + put("&7", ""); + put("&8", ""); + put("&9", ""); + put("&0", ""); + }}; CoreProtectBridge cpb = new CoreProtectBridge(); MiniMessage mm = MiniMessage.miniMessage(); @@ -101,7 +129,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("You do not have an item in your hand.")); return true; } - Component name = mm.deserialize(StringUtils.join(args, " ", 1, args.length)); + Component name = colorize(StringUtils.join(args, " ", 1, args.length)); assert meta != null; meta.displayName(name); item.setItemMeta(meta); @@ -176,7 +204,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("/itemizer lore add <text> - Add a line of text to your item's lore")); return true; } - Component lore = mm.deserialize(StringUtils.join(args, " ", 2, args.length)); + Component lore = colorize(StringUtils.join(args, " ", 2, args.length)); assert meta != null; List lores = new ArrayList<>(); if (meta.lore() != null) @@ -247,7 +275,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase { return true; } - Component lore = mm.deserialize(StringUtils.join(args, " ", 3, args.length)); + Component lore = colorize(StringUtils.join(args, " ", 3, args.length)); assert meta != null; List lores; if (meta.lore() != null) @@ -267,7 +295,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase lores.set(index - 1, lore); meta.lore(lores); item.setItemMeta(meta); - sender.sendMessage(mm.deserialize("Line '" + index + "' has been changed to '" + lore + "'")); + sender.sendMessage(mm.deserialize("Line '" + index + "' has been changed to '" + mm.serialize(lore) + "'")); return true; } case "clear" -> @@ -818,12 +846,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("You do not have a Written Book in your hand.")); return true; } - Component name = mm.deserialize(StringUtils.join(args, " ", 1, args.length)); + Component name = colorize(StringUtils.join(args, " ", 1, args.length)); final BookMeta bookMeta = (BookMeta) meta; assert bookMeta != null; bookMeta.title(name); item.setItemMeta(bookMeta); - sender.sendMessage(mm.deserialize("The title of the book has been set to '")); + sender.sendMessage(mm.deserialize("The title of the book has been set to '" + mm.serialize(name) + "'")); return true; } case "author" -> @@ -844,12 +872,12 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("You do not have a Written Book in your hand.")); return true; } - Component name = mm.deserialize(args[1]); + Component name = colorize(args[1]); final BookMeta bookMeta = (BookMeta) meta; assert bookMeta != null; bookMeta.author(name); item.setItemMeta(bookMeta); - sender.sendMessage(mm.deserialize("The author of the book has been set to '" + name + "'")); + sender.sendMessage(mm.deserialize("The author of the book has been set to '" + mm.serialize(name) + "'")); return true; } case "head" -> @@ -911,7 +939,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase sender.sendMessage(mm.deserialize("There's a maximum of 4 lines on a sign")); return true; } - Component text = mm.deserialize(StringUtils.join(args, " ", 2, args.length)); + Component text = colorize(StringUtils.join(args, " ", 2, args.length)); if (cpb.getAPI() != null) { cpb.getAPI().logRemoval(player.getName(), block.getLocation(), block.getType(), block.getBlockData()); @@ -923,7 +951,7 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase { cpb.getAPI().logPlacement(player.getName(), sign.getLocation(), sign.getType(), sign.getBlockData()); } - sender.sendMessage(mm.deserialize("Line '" + line + "' has successfully changed to '" + text + "'")); + sender.sendMessage(mm.deserialize("Line '" + line + "' has successfully changed to '" + mm.serialize(text) + "'")); return true; } case "clearall" -> @@ -950,6 +978,17 @@ public class ItemizerXCommand implements CommandExecutor, ItemizerXBase } } + private Component colorize(String string) + { + Matcher matcher = Pattern.compile("&[a-fk-or0-9]").matcher(string); + while (matcher.find()) + { + String color = matcher.group(); + string = string.replace(color, COLOR_TRANSLATION.getOrDefault(color, color)); + } + return mm.deserialize(string); + } + private Integer parseInt(CommandSender sender, String string) { try