3
0
Mirror von https://github.com/ViaVersion/ViaBackwards.git synchronisiert 2024-11-20 06:50:10 +01:00

remove unused colors from team prefix/suffix

Dieser Commit ist enthalten in:
Gerrygames 2019-01-22 14:41:50 +01:00
Ursprung 905cd9cf0d
Commit ccf132ff50
2 geänderte Dateien mit 36 neuen und 0 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);
@ -119,9 +121,11 @@ public class PlayerPacket1_13 extends Rewriter<Protocol1_12_2To1_13> {
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);

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