use of meteordevelopment.meteorclient.mixin.ChatHudAccessor in project meteor-client by MeteorDevelopment.
the class BetterChat method appendAntiSpam.
private Text appendAntiSpam(Text text, int index) {
List<ChatHudLine<OrderedText>> visibleMessages = ((ChatHudAccessor) mc.inGameHud.getChatHud()).getVisibleMessages();
if (visibleMessages.isEmpty() || index < 0 || index > visibleMessages.size() - 1)
return null;
ChatHudLine<OrderedText> visibleMessage = visibleMessages.get(index);
LiteralText parsed = new LiteralText("");
visibleMessage.getText().accept((i, style, codePoint) -> {
parsed.append(new LiteralText(new String(Character.toChars(codePoint))).setStyle(style));
return true;
});
String oldMessage = parsed.getString();
String newMessage = text.getString();
if (oldMessage.equals(newMessage)) {
return parsed.append(new LiteralText(" (2)").formatted(Formatting.GRAY));
} else {
Matcher matcher = Pattern.compile(".*(\\([0-9]+\\)$)").matcher(oldMessage);
if (!matcher.matches())
return null;
String group = matcher.group(matcher.groupCount());
int number = Integer.parseInt(group.substring(1, group.length() - 1));
String counter = " (" + number + ")";
if (oldMessage.substring(0, oldMessage.length() - counter.length()).equals(newMessage)) {
for (int i = 0; i < counter.length(); i++) parsed.getSiblings().remove(parsed.getSiblings().size() - 1);
return parsed.append(new LiteralText(" (" + (number + 1) + ")").formatted(Formatting.GRAY));
}
}
return null;
}
Aggregations