From b71430c74a7eba7eec0ae74506d0f140df92a4a7 Mon Sep 17 00:00:00 2001 From: LOOHP Date: Wed, 6 May 2020 12:09:46 +0800 Subject: [PATCH] 2.1.9 Update --- src/com/loohp/interactivechat/Commands.java | 1 - .../Listeners/ChatPackets.java | 2 +- .../interactivechat/Listeners/Events.java | 2 +- .../Modules/EnderchestDisplay.java | 37 +++++++------- .../Modules/InventoryDisplay.java | 37 +++++++------- .../interactivechat/Modules/ItemDisplay.java | 39 +++++++-------- .../Modules/PlayernameDisplay.java | 44 ++++++++-------- .../ObjectHolders/ReplaceTextBundle.java | 9 +++- .../interactivechat/Updater/Updater.java | 7 +-- .../Utils/ChatColorFilter.java | 2 +- .../interactivechat/Utils/ChatColorUtils.java | 50 +++++++++++++++---- .../Utils/CustomStringUtils.java | 16 +++++- .../interactivechat/Utils/ItemNBTUtils.java | 17 +++---- src/plugin.yml | 2 +- 14 files changed, 149 insertions(+), 116 deletions(-) diff --git a/src/com/loohp/interactivechat/Commands.java b/src/com/loohp/interactivechat/Commands.java index dd2fd586..c09721db 100644 --- a/src/com/loohp/interactivechat/Commands.java +++ b/src/com/loohp/interactivechat/Commands.java @@ -34,7 +34,6 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String if (args[0].equalsIgnoreCase("reload")) { if (sender.hasPermission("interactivechat.reload")) { ConfigManager.reloadConfig(); - ConfigManager.loadConfig(); MaterialUtils.reloadLang(); sender.sendMessage(InteractiveChat.ReloadPlugin); } else { diff --git a/src/com/loohp/interactivechat/Listeners/ChatPackets.java b/src/com/loohp/interactivechat/Listeners/ChatPackets.java index 9673728d..a8c5e316 100644 --- a/src/com/loohp/interactivechat/Listeners/ChatPackets.java +++ b/src/com/loohp/interactivechat/Listeners/ChatPackets.java @@ -90,7 +90,7 @@ public void onPacketSending(PacketEvent event) { } if (InteractiveChat.usePlayerName) { - basecomponent = PlayernameDisplay.process(basecomponent, rawMessageKey, unix); + basecomponent = PlayernameDisplay.process(basecomponent, rawMessageKey, sender, unix); } if (InteractiveChat.AllowMention && sender.isPresent()) { diff --git a/src/com/loohp/interactivechat/Listeners/Events.java b/src/com/loohp/interactivechat/Listeners/Events.java index 753cbc45..ae4bffb3 100644 --- a/src/com/loohp/interactivechat/Listeners/Events.java +++ b/src/com/loohp/interactivechat/Listeners/Events.java @@ -127,7 +127,7 @@ public void checkChatforChatManager(AsyncPlayerChatEvent event) { InteractiveChat.messages.put(ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', event.getMessage())), event.getPlayer().getUniqueId()); } - @EventHandler(priority=EventPriority.LOWEST) + @EventHandler(priority=EventPriority.LOW) public void onMention(AsyncPlayerChatEvent event) { String message = event.getMessage(); Player sender = event.getPlayer(); diff --git a/src/com/loohp/interactivechat/Modules/EnderchestDisplay.java b/src/com/loohp/interactivechat/Modules/EnderchestDisplay.java index 2b4adede..85216507 100644 --- a/src/com/loohp/interactivechat/Modules/EnderchestDisplay.java +++ b/src/com/loohp/interactivechat/Modules/EnderchestDisplay.java @@ -29,15 +29,12 @@ public class EnderchestDisplay { - private static String placeholder = InteractiveChat.enderPlaceholder; - private static boolean casesensitive = InteractiveChat.enderCaseSensitive; private static HashMap> placeholderCooldowns = InteractiveChat.placeholderCooldowns; private static HashMap universalCooldowns = InteractiveChat.universalCooldowns; - private static long cooldown = ConfigManager.getConfig().getLong("ItemDisplay.EnderChest.Cooldown") * 1000; public static BaseComponent process(BaseComponent basecomponent, Optional optplayer, String messageKey, long unix) { - boolean contain = (casesensitive) ? (basecomponent.toPlainText().contains(placeholder)) : (basecomponent.toPlainText().toLowerCase().contains(placeholder.toLowerCase())); - if (!InteractiveChat.cooldownbypass.get(unix).contains(placeholder) && contain) { + boolean contain = (InteractiveChat.enderCaseSensitive) ? (basecomponent.toPlainText().contains(InteractiveChat.enderPlaceholder)) : (basecomponent.toPlainText().toLowerCase().contains(InteractiveChat.enderPlaceholder.toLowerCase())); + if (!InteractiveChat.cooldownbypass.get(unix).contains(InteractiveChat.enderPlaceholder) && contain) { if (optplayer.isPresent()) { Player player = optplayer.get(); Long uc = universalCooldowns.get(player); @@ -51,16 +48,16 @@ public static BaseComponent process(BaseComponent basecomponent, Optional()); } HashMap spmap = placeholderCooldowns.get(player); - if (spmap.containsKey(placeholder)) { - if (spmap.get(placeholder) > unix) { + if (spmap.containsKey(InteractiveChat.enderPlaceholder)) { + if (spmap.get(InteractiveChat.enderPlaceholder) > unix) { if (!player.hasPermission("interactivechat.cooldown.bypass")) { return basecomponent; } } } - spmap.put(placeholder, unix + cooldown); + spmap.put(InteractiveChat.enderPlaceholder, unix + ConfigManager.getConfig().getLong("ItemDisplay.EnderChest.Cooldown") * 1000); } - InteractiveChat.cooldownbypass.get(unix).add(placeholder); + InteractiveChat.cooldownbypass.get(unix).add(InteractiveChat.enderPlaceholder); InteractiveChat.cooldownbypass.put(unix, InteractiveChat.cooldownbypass.get(unix)); } @@ -72,19 +69,19 @@ public static BaseComponent process(BaseComponent basecomponent, Optional trim = new LinkedList(Arrays.asList(text.split(regex, -1))); if (trim.get(trim.size() - 1).equals("")) { trim.remove(trim.size() - 1); @@ -94,11 +91,11 @@ public static BaseComponent process(BaseComponent basecomponent, Optional i || endwith) { if (trim.get(i).endsWith("\\") && !trim.get(i).endsWith("\\\\")) { String color = ChatColorUtils.getLastColors(newlist.get(newlist.size() - 1).toLegacyText()); - TextComponent message = new TextComponent(placeholder); + TextComponent message = new TextComponent(InteractiveChat.enderPlaceholder); message = (TextComponent) ChatColorUtils.applyColor(message, color); ((TextComponent) newlist.get(newlist.size() - 1)).setText(trim.get(i).substring(0, trim.get(i).length() - 1)); newlist.add(message); @@ -146,22 +143,22 @@ public static BaseComponent process(BaseComponent basecomponent, Optional> placeholderCooldowns = InteractiveChat.placeholderCooldowns; private static HashMap universalCooldowns = InteractiveChat.universalCooldowns; - private static long cooldown = ConfigManager.getConfig().getLong("ItemDisplay.Inventory.Cooldown") * 1000; public static BaseComponent process(BaseComponent basecomponent, Optional optplayer, String messageKey, long unix) { - boolean contain = (casesensitive) ? (basecomponent.toPlainText().contains(placeholder)) : (basecomponent.toPlainText().toLowerCase().contains(placeholder.toLowerCase())); - if (!InteractiveChat.cooldownbypass.get(unix).contains(placeholder) && contain) { + boolean contain = (InteractiveChat.invCaseSensitive) ? (basecomponent.toPlainText().contains(InteractiveChat.invPlaceholder)) : (basecomponent.toPlainText().toLowerCase().contains(InteractiveChat.invPlaceholder.toLowerCase())); + if (!InteractiveChat.cooldownbypass.get(unix).contains(InteractiveChat.invPlaceholder) && contain) { if (optplayer.isPresent()) { Player player = optplayer.get(); Long uc = universalCooldowns.get(player); @@ -51,16 +48,16 @@ public static BaseComponent process(BaseComponent basecomponent, Optional()); } HashMap spmap = placeholderCooldowns.get(player); - if (spmap.containsKey(placeholder)) { - if (spmap.get(placeholder) > unix) { + if (spmap.containsKey(InteractiveChat.invPlaceholder)) { + if (spmap.get(InteractiveChat.invPlaceholder) > unix) { if (!player.hasPermission("interactivechat.cooldown.bypass")) { return basecomponent; } } } - spmap.put(placeholder, unix + cooldown); + spmap.put(InteractiveChat.invPlaceholder, unix + ConfigManager.getConfig().getLong("ItemDisplay.Inventory.Cooldown") * 1000); } - InteractiveChat.cooldownbypass.get(unix).add(placeholder); + InteractiveChat.cooldownbypass.get(unix).add(InteractiveChat.invPlaceholder); InteractiveChat.cooldownbypass.put(unix, InteractiveChat.cooldownbypass.get(unix)); } @@ -72,19 +69,19 @@ public static BaseComponent process(BaseComponent basecomponent, Optional trim = new LinkedList(Arrays.asList(text.split(regex, -1))); if (trim.get(trim.size() - 1).equals("")) { trim.remove(trim.size() - 1); @@ -94,11 +91,11 @@ public static BaseComponent process(BaseComponent basecomponent, Optional i || endwith) { if (trim.get(i).endsWith("\\") && !trim.get(i).endsWith("\\\\")) { String color = ChatColorUtils.getLastColors(newlist.get(newlist.size() - 1).toLegacyText()); - TextComponent message = new TextComponent(placeholder); + TextComponent message = new TextComponent(InteractiveChat.invPlaceholder); message = (TextComponent) ChatColorUtils.applyColor(message, color); ((TextComponent) newlist.get(newlist.size() - 1)).setText(trim.get(i).substring(0, trim.get(i).length() - 1)); newlist.add(message); @@ -147,22 +144,22 @@ public static BaseComponent process(BaseComponent basecomponent, Optional> placeholderCooldowns = InteractiveChat.placeholderCooldowns; private static HashMap universalCooldowns = InteractiveChat.universalCooldowns; - private static long cooldown = ConfigManager.getConfig().getLong("ItemDisplay.Item.Cooldown") * 1000; @SuppressWarnings("deprecation") public static BaseComponent process(BaseComponent basecomponent, Optional optplayer, String messageKey, long unix) { - boolean contain = (casesensitive) ? (basecomponent.toPlainText().contains(placeholder)) : (basecomponent.toPlainText().toLowerCase().contains(placeholder.toLowerCase())); - if (!InteractiveChat.cooldownbypass.get(unix).contains(placeholder) && contain) { + boolean contain = (InteractiveChat.itemCaseSensitive) ? (basecomponent.toPlainText().contains(InteractiveChat.itemPlaceholder)) : (basecomponent.toPlainText().toLowerCase().contains(InteractiveChat.itemPlaceholder.toLowerCase())); + if (!InteractiveChat.cooldownbypass.get(unix).contains(InteractiveChat.itemPlaceholder) && contain) { if (optplayer.isPresent()) { Player player = optplayer.get(); Long uc = universalCooldowns.get(player); @@ -59,16 +56,16 @@ public static BaseComponent process(BaseComponent basecomponent, Optional()); } HashMap spmap = placeholderCooldowns.get(player); - if (spmap.containsKey(placeholder)) { - if (spmap.get(placeholder) > unix) { + if (spmap.containsKey(InteractiveChat.itemPlaceholder)) { + if (spmap.get(InteractiveChat.itemPlaceholder) > unix) { if (!player.hasPermission("interactivechat.cooldown.bypass")) { return basecomponent; } } } - spmap.put(placeholder, unix + cooldown); + spmap.put(InteractiveChat.itemPlaceholder, unix + ConfigManager.getConfig().getLong("ItemDisplay.Item.Cooldown") * 1000); } - InteractiveChat.cooldownbypass.get(unix).add(placeholder); + InteractiveChat.cooldownbypass.get(unix).add(InteractiveChat.itemPlaceholder); InteractiveChat.cooldownbypass.put(unix, InteractiveChat.cooldownbypass.get(unix)); } @@ -80,33 +77,34 @@ public static BaseComponent process(BaseComponent basecomponent, Optional trim = new LinkedList(Arrays.asList(text.split(regex, -1))); if (trim.get(trim.size() - 1).equals("")) { trim.remove(trim.size() - 1); } + for (int i = 0; i < trim.size(); i++) { TextComponent before = (TextComponent) textcomponent.duplicate(); before.setText(trim.get(i)); newlist.add(before); - boolean endwith = casesensitive ? text.endsWith(placeholder) : text.toLowerCase().endsWith(placeholder.toLowerCase()); + boolean endwith = InteractiveChat.itemCaseSensitive ? text.endsWith(InteractiveChat.itemPlaceholder) : text.toLowerCase().endsWith(InteractiveChat.itemPlaceholder.toLowerCase()); if ((trim.size() - 1) > i || endwith) { if (trim.get(i).endsWith("\\") && !trim.get(i).endsWith("\\\\")) { String color = ChatColorUtils.getLastColors(newlist.get(newlist.size() - 1).toLegacyText()); - TextComponent message = new TextComponent(placeholder); + TextComponent message = new TextComponent(InteractiveChat.itemPlaceholder); message = (TextComponent) ChatColorUtils.applyColor(message, color); ((TextComponent) newlist.get(newlist.size() - 1)).setText(trim.get(i).substring(0, trim.get(i).length() - 1)); newlist.add(message); @@ -287,24 +285,23 @@ public static BaseComponent process(BaseComponent basecomponent, Optional names = new ArrayList(); + public static BaseComponent process(BaseComponent basecomponent, String messageKey, Optional sender, long unix) { + List names = new ArrayList(); Bukkit.getOnlinePlayers().forEach((each) -> { names.add(new ReplaceTextBundle(ChatColor.stripColor(each.getName()), each, each.getName())); if (!ChatColor.stripColor(each.getName()).equals(ChatColor.stripColor(each.getDisplayName()))) { @@ -40,31 +36,38 @@ public static BaseComponent process(BaseComponent basecomponent, String messageK }); if (InteractiveChat.EssentialsHook) { InteractiveChat.essenNick.forEach((player, name) -> names.add(new ReplaceTextBundle(ChatColor.stripColor(name), player, name))); - } + } + Collections.sort(names); + Collections.reverse(names); + + List matched = new ArrayList(); for (ReplaceTextBundle entry : names) { - basecomponent = processPlayer(entry.getPlaceholder(), entry.getPlayer(), entry.getReplaceText(), basecomponent, messageKey, unix); + basecomponent = processPlayer(entry.getPlaceholder(), entry.getPlayer(), entry.getReplaceText(), basecomponent, matched, messageKey, unix); } return basecomponent; } - public static BaseComponent processPlayer(String placeholder, Player player, String replaceText, BaseComponent basecomponent, String messageKey, long unix) { + public static BaseComponent processPlayer(String placeholder, Player player, String replaceText, BaseComponent basecomponent, List matched, String messageKey, long unix) { List basecomponentlist = CustomStringUtils.loadExtras(basecomponent); List newlist = new ArrayList(); for (BaseComponent base : basecomponentlist) { - if (!(base instanceof TextComponent)) { + if (matched.contains(base)) { + newlist.add(base); + } else if (!(base instanceof TextComponent)) { newlist.add(base); } else { TextComponent textcomponent = (TextComponent) base; String text = textcomponent.getText(); - String regex = casesensitive ? CustomStringUtils.getIgnoreColorCodeRegex(CustomStringUtils.escapeMetaCharacters(placeholder)) : "(?i)(" + CustomStringUtils.getIgnoreColorCodeRegex(CustomStringUtils.escapeMetaCharacters(placeholder)) + ")"; + String regex = InteractiveChat.usePlayerNameCaseSensitive ? CustomStringUtils.getIgnoreColorCodeRegex(CustomStringUtils.escapeMetaCharacters(placeholder)) : "(?i)(" + CustomStringUtils.getIgnoreColorCodeRegex(CustomStringUtils.escapeMetaCharacters(placeholder)) + ")"; if (!text.matches(".*" + regex + ".*")) { newlist.add(textcomponent); continue; } + Queue matches = (LinkedList) CustomStringUtils.getAllMatches(regex, text); List trim = new LinkedList(Arrays.asList(text.split(regex, -1))); if (trim.get(trim.size() - 1).equals("")) { trim.remove(trim.size() - 1); @@ -79,19 +82,20 @@ public static BaseComponent processPlayer(String placeholder, Player player, Str String lastColor = ChatColorUtils.getLastColors(sb.toString()); - TextComponent message = new TextComponent(replaceText); + TextComponent message = new TextComponent(matches.isEmpty() ? replaceText : matches.poll()); message = (TextComponent) CustomStringUtils.copyFormatting(message, before); message.setText(lastColor + message.getText()); - if (hoverEnabled) { - String playertext = PlaceholderAPI.setPlaceholders(player, hoverText); + if (InteractiveChat.usePlayerNameHoverEnable) { + String playertext = PlaceholderAPI.setPlaceholders(player, InteractiveChat.usePlayerNameHoverText); message.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(playertext).create())); } - if (clickEnabled) { - String playertext = PlaceholderAPI.setPlaceholders(player, clickValue); - message.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(clickAction), playertext)); + if (InteractiveChat.usePlayerNameClickEnable) { + String playertext = PlaceholderAPI.setPlaceholders(player, InteractiveChat.usePlayerNameClickValue); + message.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(InteractiveChat.usePlayerNameClickAction), playertext)); } + matched.add(message); newlist.add(message); } } diff --git a/src/com/loohp/interactivechat/ObjectHolders/ReplaceTextBundle.java b/src/com/loohp/interactivechat/ObjectHolders/ReplaceTextBundle.java index aac77c55..caa229c1 100644 --- a/src/com/loohp/interactivechat/ObjectHolders/ReplaceTextBundle.java +++ b/src/com/loohp/interactivechat/ObjectHolders/ReplaceTextBundle.java @@ -2,7 +2,7 @@ import org.bukkit.entity.Player; -public class ReplaceTextBundle { +public class ReplaceTextBundle implements Comparable { String placeholder; Player player; @@ -26,4 +26,11 @@ public String getReplaceText() { return replaceText; } + @Override + public int compareTo(ReplaceTextBundle anotherReplaceTextBundle) { + String compareText = anotherReplaceTextBundle.getPlaceholder(); + int res = String.CASE_INSENSITIVE_ORDER.compare(placeholder, compareText); + return (res != 0) ? res : placeholder.compareTo(compareText); + } + } diff --git a/src/com/loohp/interactivechat/Updater/Updater.java b/src/com/loohp/interactivechat/Updater/Updater.java index 562e0ba3..2dc80747 100644 --- a/src/com/loohp/interactivechat/Updater/Updater.java +++ b/src/com/loohp/interactivechat/Updater/Updater.java @@ -29,12 +29,7 @@ public void onJoin(PlayerJoinEvent event) { Player player = event.getPlayer(); if (player.hasPermission("interactivechat.update")) { String version = Updater.checkUpdate(); - if (version.equals("latest")) { - TextComponent text = new TextComponent(ChatColor.GREEN + "[InteractiveChat] You are running the latest version: " + InteractiveChat.plugin.getDescription().getVersion() + "!"); - text.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatColor.YELLOW + "Link to SpigotMC resource page!").create())); - text.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, "https://www.spigotmc.org/resources/75870")); - player.spigot().sendMessage(text); - } else { + if (!version.equals("latest")) { Updater.sendUpdateMessage(player, version); } } diff --git a/src/com/loohp/interactivechat/Utils/ChatColorFilter.java b/src/com/loohp/interactivechat/Utils/ChatColorFilter.java index 38f12e8d..a25e2363 100644 --- a/src/com/loohp/interactivechat/Utils/ChatColorFilter.java +++ b/src/com/loohp/interactivechat/Utils/ChatColorFilter.java @@ -11,7 +11,7 @@ public class ChatColorFilter { public static String removeUselessColorCodes(String string) { - return string.replaceAll("^(§[0-9,a-f,l-o,r])*(?=§[0-9,a-f,r])", ""); + return string.replaceAll("(§[0-9a-fl-or])*(?=§[0-9a-fr])", ""); } public static String filterIllegalColorCodes(String string) { diff --git a/src/com/loohp/interactivechat/Utils/ChatColorUtils.java b/src/com/loohp/interactivechat/Utils/ChatColorUtils.java index 7a230e61..2ae5643a 100644 --- a/src/com/loohp/interactivechat/Utils/ChatColorUtils.java +++ b/src/com/loohp/interactivechat/Utils/ChatColorUtils.java @@ -30,17 +30,29 @@ public static String getFirstColors(String input) { String result = ""; boolean found = false; - for (int i = 0; i < input.length() - 1; i++) { - if (input.charAt(i) == '§' && input.length() > i + 1) { - String color = String.valueOf(input.charAt(i)) + String.valueOf(input.charAt(i + 1)); - if (isLegal(color)) { - if (found && (isColor(ChatColor.getByChar(input.charAt(i + 1))) || ChatColor.getByChar(input.charAt(i + 1)).equals(ChatColor.RESET))) { - break; - } - result = result + color; - found = true; - } + if (input.length() < 2) { + return ""; + } + + int i = 1; + String color = ""; + while (i < input.length()) { + color = String.valueOf(input.charAt(i - 1)) + String.valueOf(input.charAt(i)); + //Bukkit.getConsoleSender().sendMessage(color.replace("§", "&")); + if (isLegal(color)) { + if (!found) { + found = true; + result = color; + } else if (isColor(ChatColor.getByChar(color.charAt(1)))) { + result = color; + } else { + result = result + color; + } + i++; + } else if (found) { + break; } + i++; } return result; @@ -60,6 +72,9 @@ public static boolean isColor(ChatColor color) { } public static boolean isLegal(String color) { + if (color.charAt(0) != '§') { + return false; + } if (color.matches("§[g-j,p,q,s-z,A-Z,\\-!$%^&*()_+|~=`{}\\[\\]:\\\";'<>?,.\\/\\\\]")) { return false; } @@ -97,5 +112,20 @@ public static BaseComponent applyColor(BaseComponent basecomponent, String color } return basecomponent; } + + public static String addColorToEachWord(String text, String color) { + StringBuilder sb = new StringBuilder(); + text = color + text; + do { + color = getFirstColors(text); + int pos = text.indexOf(" ") + 1; + pos = pos <= 0 ? text.length() : pos; + String before = text.substring(0, pos); + //Bukkit.getConsoleSender().sendMessage(color.replace("§", "&") + " " + text.replace("§", "&") + " " + before.replace("§", "&")); + sb.append(before); + text = color + text.substring(pos); + } while (text.length() > 0 && !text.equals(color)); + return ChatColorFilter.removeUselessColorCodes(sb.toString()); + } } diff --git a/src/com/loohp/interactivechat/Utils/CustomStringUtils.java b/src/com/loohp/interactivechat/Utils/CustomStringUtils.java index 874f3785..f665406b 100644 --- a/src/com/loohp/interactivechat/Utils/CustomStringUtils.java +++ b/src/com/loohp/interactivechat/Utils/CustomStringUtils.java @@ -1,7 +1,10 @@ package com.loohp.interactivechat.Utils; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.loohp.interactivechat.InteractiveChat; @@ -10,6 +13,15 @@ public class CustomStringUtils { + public static List getAllMatches(String regex, String str) { + List allMatches = new LinkedList(); + Matcher m = Pattern.compile(regex).matcher(str); + while (m.find()) { + allMatches.add(m.group()); + } + return allMatches; + } + public static int ordinalIndexOf(String str, String substr, int n) { int pos = str.indexOf(substr); while (--n > 0 && pos != -1) { @@ -126,11 +138,11 @@ public static List loadExtras(List baseComp) { for (BaseComponent extra : loadExtras(each.getExtra())) { extra = copyFormattingNoReplace(extra, noExtra); if (extra instanceof TextComponent && text != null && ChatComponentUtils.areEventsSimilar(extra, text)) { - BaseComponent extraNoExtra = extra.duplicate(); + TextComponent extraNoExtra = (TextComponent) extra.duplicate(); if (extraNoExtra.getExtra() != null) { extraNoExtra.getExtra().clear(); } - text.setText(text.getText() + extraNoExtra.toLegacyText()); + text.setText(text.getText() + ChatColorUtils.addColorToEachWord(extraNoExtra.toLegacyText(), ChatColorUtils.getLastColors(text.getText()))); } else if (!(extra instanceof TextComponent)) { if (text != null) { list.add(text); diff --git a/src/com/loohp/interactivechat/Utils/ItemNBTUtils.java b/src/com/loohp/interactivechat/Utils/ItemNBTUtils.java index e6c678cb..c3c995e5 100644 --- a/src/com/loohp/interactivechat/Utils/ItemNBTUtils.java +++ b/src/com/loohp/interactivechat/Utils/ItemNBTUtils.java @@ -1,9 +1,7 @@ package com.loohp.interactivechat.Utils; import java.lang.reflect.Method; -import java.util.logging.Level; -import org.bukkit.Bukkit; import org.bukkit.inventory.ItemStack; import com.loohp.interactivechat.NMS.ReflectionUtil; @@ -11,29 +9,26 @@ public class ItemNBTUtils { public static String getNMSItemStackJson(ItemStack itemStack) { - // ItemStack methods to get a net.minecraft.server.ItemStack object for serialization + Class craftItemStackClazz = ReflectionUtil.getOBCClass("inventory.CraftItemStack"); Method asNMSCopyMethod = ReflectionUtil.getMethod(craftItemStackClazz, "asNMSCopy", ItemStack.class); - - // NMS Method to serialize a net.minecraft.server.ItemStack to a valid Json string + Class nmsItemStackClazz = ReflectionUtil.getNMSClass("ItemStack"); Class nbtTagCompoundClazz = ReflectionUtil.getNMSClass("NBTTagCompound"); Method saveNmsItemStackMethod = ReflectionUtil.getMethod(nmsItemStackClazz, "save", nbtTagCompoundClazz); - Object nmsNbtTagCompoundObj; // This will just be an empty NBTTagCompound instance to invoke the saveNms method - Object nmsItemStackObj; // This is the net.minecraft.server.ItemStack object received from the asNMSCopy method - Object itemAsJsonObject; // This is the net.minecraft.server.ItemStack after being put through saveNmsItem method + Object nmsNbtTagCompoundObj; + Object nmsItemStackObj; + Object itemAsJsonObject; try { nmsNbtTagCompoundObj = nbtTagCompoundClazz.newInstance(); nmsItemStackObj = asNMSCopyMethod.invoke(null, itemStack); itemAsJsonObject = saveNmsItemStackMethod.invoke(nmsItemStackObj, nmsNbtTagCompoundObj); } catch (Throwable t) { - Bukkit.getLogger().log(Level.SEVERE, "failed to serialize itemstack to nms item", t); - return null; + return "{}"; } - // Return a string representation of the serialized object return itemAsJsonObject.toString(); } diff --git a/src/plugin.yml b/src/plugin.yml index bc45e5a7..9589385c 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: InteractiveChat author: loohp -version: 2.1.8 +version: 2.1.9 main: com.loohp.interactivechat.InteractiveChat api-version: 1.13 description: Make the chat interactive