3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-12-27 00:22:53 +01:00

Merge pull request #84 from Gerrygames/removeUnusedColor

remove unused colors from team prefix/suffix
Dieser Commit ist enthalten in:
Myles 2019-01-23 13:39:20 +00:00 committet von GitHub
Commit 15db59f439
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23
2 geänderte Dateien mit 162 neuen und 126 gelöschten Zeilen

Datei anzeigen

@ -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<Protocol1_12_2To1_13> {
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);
@ -117,11 +119,13 @@ public class PlayerPacket1_13 extends Rewriter<Protocol1_12_2To1_13> {
//TODO team color/prefix handling changed from 1.12.2 to 1.13 and to 1.13.1 again afaik
String prefix = wrapper.read(Type.STRING);
String suffix = wrapper.read(Type.STRING);
prefix = ChatRewriter.jsonTextToLegacy(prefix);
prefix = prefix == null || prefix.equals("null") ? "" : 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 = suffix == null || suffix.equals("null") ? "" : 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);

Datei anzeigen

@ -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();
}
}