diff --git a/src/com/loohp/interactivechat/Listeners/ChatPackets.java b/src/com/loohp/interactivechat/Listeners/ChatPackets.java index eb86a4c2..2ebf8a75 100644 --- a/src/com/loohp/interactivechat/Listeners/ChatPackets.java +++ b/src/com/loohp/interactivechat/Listeners/ChatPackets.java @@ -155,7 +155,13 @@ public void onPacketSending(PacketEvent event) { debug++; basecomponent = CustomPlaceholderDisplay.process(basecomponent, sender, reciever, rawMessageKey, unix); debug++; - basecomponent = InteractiveChat.FilterUselessColorCodes ? ChatComponentUtils.cleanUpLegacyText(basecomponent, reciever) : ChatComponentUtils.respectClientColorSettingsWithoutCleanUp(basecomponent, reciever); + if (InteractiveChat.version.isPost1_16()) { + if (!sender.isPresent() || (sender.isPresent() && sender.get().hasPermission("interactivechat.customfont.translate"))) { + basecomponent = ChatComponentUtils.translatePluginFontFormatting(basecomponent); + } + } + debug++; + basecomponent = InteractiveChat.FilterUselessColorCodes ? ChatComponentUtils.cleanUpLegacyText(basecomponent, reciever) : ChatComponentUtils.respectClientColorSettingsWithoutCleanUp(basecomponent, reciever); String json = ComponentSerializer.toString(basecomponent); boolean longerThanMaxLength = false; if (((InteractiveChat.version.isLegacy() || InteractiveChat.protocolManager.getProtocolVersion(reciever) < 393) && json.length() > 30000) || (!InteractiveChat.version.isLegacy() && json.length() > 262000)) { diff --git a/src/com/loohp/interactivechat/Utils/ChatColorUtils.java b/src/com/loohp/interactivechat/Utils/ChatColorUtils.java index c2e6babb..965520fe 100644 --- a/src/com/loohp/interactivechat/Utils/ChatColorUtils.java +++ b/src/com/loohp/interactivechat/Utils/ChatColorUtils.java @@ -4,6 +4,8 @@ import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import com.loohp.interactivechat.InteractiveChat; @@ -13,6 +15,7 @@ public class ChatColorUtils { private static Set colors = new HashSet(); + private static Pattern colorFormating = Pattern.compile("(?=(? chatHoverEventClass; private static MethodHandle hoverEventGetValueMethod; + private static Pattern fontFormating = Pattern.compile("(?=(? list = CustomStringUtils.loadExtras(basecomponent); + List newlist = new ArrayList(); + Optional currentFont = Optional.empty(); + + for (BaseComponent each : list) { + if (each.getFontRaw() != null) { + currentFont = Optional.of(each.getFontRaw()); + } + + if (each instanceof TextComponent) { + List textlist = new LinkedList(); + TextComponent textcomponent = (TextComponent) each; + String text = textcomponent.getText(); + + while (true) { + Matcher matcher = fontFormating.matcher(text); + + if (matcher.find()) { + String foramtedFont = matcher.group().toLowerCase(); + + String colorCodesInside = ChatColorUtils.getLastColors(foramtedFont); + String striped = ChatColor.stripColor(foramtedFont); + + int lengthDiff = foramtedFont.length() - striped.length(); + + foramtedFont = striped; + + int start = matcher.start(); + int pos = foramtedFont.indexOf("font"); + int end = matcher.end() - lengthDiff; + + if (pos < 0) { + continue; + } + + int fontLength = foramtedFont.indexOf(",", pos); + if (fontLength < 0) { + fontLength = foramtedFont.indexOf(" ", pos); + } + if (fontLength < 0) { + fontLength = foramtedFont.indexOf("]", pos); + } + fontLength = fontLength - pos; + + StringBuilder sb = new StringBuilder(text); + + sb.delete(start, matcher.end()); + sb.insert(start, foramtedFont); + + int absPos = sb.indexOf("font", start); + + String nextFont = foramtedFont.substring(pos + 5, pos + fontLength); + + sb.delete(absPos, absPos + fontLength); + sb.insert(end - fontLength, colorCodesInside); + + while (sb.charAt(absPos) == ',' || sb.charAt(absPos) == ' ') { + sb.deleteCharAt(absPos); + } + + while (sb.charAt(absPos - 1) == ',' || sb.charAt(absPos - 1) == ' ') { + sb.deleteCharAt(absPos - 1); + absPos--; + } + + if (sb.charAt(absPos) == ']' && sb.charAt(absPos - 1) == '[') { + sb.deleteCharAt(absPos - 1); + sb.deleteCharAt(absPos - 1); + } + + absPos--; + + TextComponent before = new TextComponent(textcomponent); + before.setText(sb.substring(0, absPos)); + if (currentFont.isPresent()) { + before.setFont(currentFont.get()); + } + textlist.add(before); + + text = sb.substring(absPos); + + currentFont = (nextFont.length() == 0 || nextFont.equalsIgnoreCase("null") || nextFont.equalsIgnoreCase("reset")) ? Optional.empty() : Optional.of(nextFont); + } else { + TextComponent before = new TextComponent(textcomponent); + before.setText(text); + if (currentFont.isPresent()) { + before.setFont(currentFont.get()); + } + textlist.add(before); + break; + } + } + newlist.addAll(textlist); + } else { + if (currentFont.isPresent()) { + each.setFont(currentFont.get()); + } + newlist.add(each); + } + } + + TextComponent product = new TextComponent(""); + for (int i = 0; i < newlist.size(); i++) { + BaseComponent each = newlist.get(i); + product.addExtra(each); + } + + return product; + } } diff --git a/src/com/loohp/interactivechat/Utils/CustomStringUtils.java b/src/com/loohp/interactivechat/Utils/CustomStringUtils.java index fb603b1c..99cadb5d 100644 --- a/src/com/loohp/interactivechat/Utils/CustomStringUtils.java +++ b/src/com/loohp/interactivechat/Utils/CustomStringUtils.java @@ -132,7 +132,7 @@ public static List loadExtras(List baseComp) { } else { text.copyFormatting(each); } - list.add(text); + list.add(text); } else { list.add(each); } @@ -150,8 +150,12 @@ public static List loadExtras(List baseComp) { list.add(noExtra); } for (BaseComponent extra : loadExtras(each.getExtra())) { - extra = copyFormattingNoReplace(extra, noExtra); - if (extra instanceof TextComponent && text != null && ChatComponentUtils.areEventsSimilar(extra, text)) { + if (InteractiveChat.version.isLegacy() && !InteractiveChat.version.equals(MCVersion.V1_12)) { + extra = copyFormattingNoReplace(extra, noExtra); + } else { + extra.copyFormatting(noExtra, false); + } + if (extra instanceof TextComponent && text != null && ChatComponentUtils.areEventsSimilar(extra, text) && ChatComponentUtils.areFontsSimilar(extra, text)) { TextComponent extraNoExtra = (TextComponent) extra.duplicate(); if (extraNoExtra.getExtra() != null) { extraNoExtra.getExtra().clear(); @@ -219,6 +223,11 @@ public static BaseComponent copyFormattingNoReplace(BaseComponent set, BaseCompo set.setInsertion(get.getInsertion()); } } + if (InteractiveChat.version.isPost1_16()) { + if (set.getFontRaw() == null) { + set.setFont(get.getFontRaw()); + } + } return set; } @@ -244,6 +253,9 @@ public static BaseComponent copyFormatting(BaseComponent set, BaseComponent get) set.setObfuscated(get.isObfuscated()); set.setStrikethrough(get.isStrikethrough()); set.setUnderlined(get.isUnderlined()); + if (InteractiveChat.version.isPost1_16()) { + set.setFont(get.getFontRaw()); + } return set; } diff --git a/src/com/loohp/interactivechat/Utils/MCVersion.java b/src/com/loohp/interactivechat/Utils/MCVersion.java index a673d714..dd4ba6a0 100644 --- a/src/com/loohp/interactivechat/Utils/MCVersion.java +++ b/src/com/loohp/interactivechat/Utils/MCVersion.java @@ -98,5 +98,9 @@ public boolean isOld() { public boolean isSupported() { return !unsupported; } + + public boolean isPost1_16() { + return this.ordinal() <= MCVersion.V1_16.ordinal(); + } } diff --git a/src/plugin.yml b/src/plugin.yml index 7f0c491c..1a75c914 100644 --- a/src/plugin.yml +++ b/src/plugin.yml @@ -1,6 +1,6 @@ name: InteractiveChat author: loohp -version: 2.5.2 +version: 2.6.0 main: com.loohp.interactivechat.InteractiveChat api-version: 1.13 description: Make the chat interactive @@ -39,6 +39,9 @@ permissions: interactivechat.chatcolor.translate: description: Allows you to use alternate color code default: true + interactivechat.customfont.translate: + description: Allows you to use alternate color code + default: true interactivechat.update: description: Allows you to recieve update notifications default: op \ No newline at end of file