From ccf132ff50fd89435e515e9bbdb202a8911b41b7 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Tue, 22 Jan 2019 14:41:50 +0100 Subject: [PATCH] remove unused colors from team prefix/suffix --- .../packets/PlayerPacket1_13.java | 4 +++ .../nl/matsv/viabackwards/utils/ChatUtil.java | 32 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 core/src/main/java/nl/matsv/viabackwards/utils/ChatUtil.java diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java index ea62045a..f2240384 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_12_2to1_13/packets/PlayerPacket1_13.java @@ -3,6 +3,7 @@ package nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.packets; import nl.matsv.viabackwards.ViaBackwards; import nl.matsv.viabackwards.api.rewriters.Rewriter; import nl.matsv.viabackwards.protocol.protocol1_12_2to1_13.Protocol1_12_2To1_13; +import nl.matsv.viabackwards.utils.ChatUtil; import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.api.minecraft.item.Item; @@ -102,6 +103,7 @@ public class PlayerPacket1_13 extends Rewriter { if (action == 0 || action == 2) { String displayName = wrapper.read(Type.STRING); displayName = ChatRewriter.jsonTextToLegacy(displayName); + displayName = ChatUtil.removeUnusedColor(displayName, 'f'); if (displayName.length() > 32) displayName = displayName.substring(0, 32); wrapper.write(Type.STRING, displayName); @@ -119,9 +121,11 @@ public class PlayerPacket1_13 extends Rewriter { String suffix = wrapper.read(Type.STRING); prefix = ChatRewriter.jsonTextToLegacy(prefix); prefix += "§" + (colour > -1 && colour <= 15 ? Integer.toHexString(colour) : "r"); + prefix = ChatUtil.removeUnusedColor(prefix, 'f', true); if (prefix.length() > 16) prefix = prefix.substring(0, 16); if (prefix.endsWith("§")) prefix = prefix.substring(0, prefix.length() - 1); suffix = ChatRewriter.jsonTextToLegacy(suffix); + suffix = ChatUtil.removeUnusedColor(suffix, 'f'); if (suffix.endsWith("§")) suffix = suffix.substring(0, suffix.length() - 1); wrapper.write(Type.STRING, prefix); wrapper.write(Type.STRING, suffix); diff --git a/core/src/main/java/nl/matsv/viabackwards/utils/ChatUtil.java b/core/src/main/java/nl/matsv/viabackwards/utils/ChatUtil.java new file mode 100644 index 00000000..8792469d --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/utils/ChatUtil.java @@ -0,0 +1,32 @@ +package nl.matsv.viabackwards.utils; + +import java.util.regex.Pattern; + +public class ChatUtil { + private static final Pattern UNUSED_COLOR_PATTERN = Pattern.compile("(?>(?>§[0-fk-or])*(§r|\\Z))|(?>(?>§[0-f])*(§[0-f]))"); + private static final Pattern UNUSED_COLOR_PATTERN_PREFIX = Pattern.compile("(?>(?>§[0-fk-or])*(§r))|(?>(?>§[0-f])*(§[0-f]))"); + + public static String removeUnusedColor(String legacy, char defaultColor) { + return removeUnusedColor(legacy, defaultColor, false); + } + + public static String removeUnusedColor(String legacy, char defaultColor, boolean isPrefix) { + if (legacy == null) return null; + Pattern pattern = isPrefix ? UNUSED_COLOR_PATTERN_PREFIX : UNUSED_COLOR_PATTERN; + legacy = pattern.matcher(legacy).replaceAll("$1$2"); + StringBuilder builder = new StringBuilder(); + char last = defaultColor; + for (int i = 0; i < legacy.length(); i++) { + char current = legacy.charAt(i); + if (current != '§' || i == legacy.length() - 1) { + builder.append(current); + continue; + } + current = legacy.charAt(++i); + if (current == last) continue; + builder.append('§').append(current); + last = current; + } + return builder.toString(); + } +}