Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix-chat-parse' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Dream-Master committed Jun 11, 2024
2 parents 95ed028 + ac7f305 commit e79b0df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,12 @@ public class ServerUtilitiesServerEventHandler {

public static final ServerUtilitiesServerEventHandler INST = new ServerUtilitiesServerEventHandler();
private static final ResourceLocation AFK_ID = new ResourceLocation(ServerUtilities.MOD_ID, "afk");
private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("\\~\\~(.*?)\\~\\~");
private static final Pattern STRIKETHROUGH_PATTERN = Pattern.compile("~~(.+?)~~");
private static final String STRIKETHROUGH_REPLACE = "&m$1&m";
private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*(.*?)\\*\\*|__(.*?)__");
private static final String BOLD_REPLACE = "&l$1$2&l";
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*(.*?)\\*|_(.*?)_");
private static final String ITALIC_REPLACE = "&o$1$2&o";
private static final Pattern BOLD_PATTERN = Pattern.compile("\\*\\*(.+?)\\*\\*|__(.+?)__");
private static final String BOLD_REPLACE = "&l$1&l";
private static final Pattern ITALIC_PATTERN = Pattern.compile("\\*(.+?)\\*|_(.+?)_");
private static final String ITALIC_REPLACE = "&o$1&o";

@SubscribeEvent
public void onCacheCleared(UniverseClearCacheEvent event) {
Expand Down Expand Up @@ -87,7 +87,7 @@ public void onServerChatEvent(ServerChatEvent event) {
message = message.replace(entry.getValue(), "<emoji:" + entry.getKey() + ">");
}

b = !message.equals(message = STRIKETHROUGH_PATTERN.matcher(message).replaceAll(STRIKETHROUGH_REPLACE)) | b;
b = !message.equals(message = STRIKETHROUGH_PATTERN.matcher(message).replaceAll(STRIKETHROUGH_REPLACE));
b = !message.equals(message = BOLD_PATTERN.matcher(message).replaceAll(BOLD_REPLACE)) | b;
b = !message.equals(message = ITALIC_PATTERN.matcher(message).replaceAll(ITALIC_REPLACE)) | b;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

public class TextComponentParser {

public static final NameMap.ObjectProperties<EnumChatFormatting> TEXT_FORMATTING_OBJECT_PROPERTIES = new NameMap.ObjectProperties<EnumChatFormatting>() {
public static final NameMap.ObjectProperties<EnumChatFormatting> TEXT_FORMATTING_OBJECT_PROPERTIES = new NameMap.ObjectProperties<>() {

@Override
public String getName(EnumChatFormatting value) {
Expand Down Expand Up @@ -63,8 +63,8 @@ public static IChatComponent parse(String text, @Nullable Function<String, IChat
return new TextComponentParser(text, substitutes).parse();
}

private String text;
private Function<String, IChatComponent> substitutes;
private final String text;
private final Function<String, IChatComponent> substitutes;

private IChatComponent component;
private StringBuilder builder;
Expand Down Expand Up @@ -114,57 +114,45 @@ private IChatComponent parse() {
}

if (!escape) {
char prev = c[i];
if (c[i] == '&') {
c[i] = StringUtils.FORMATTING_CHAR;
}

if (c[i] == StringUtils.FORMATTING_CHAR) {
finishPart();

if (end) {
throw new IllegalArgumentException(
"Invalid formatting! Can't end string with & or " + StringUtils.FORMATTING_CHAR + "!");
if (end || i + 1 >= c.length) {
builder.append(prev);
break;
}

i++;

EnumChatFormatting formatting = CODE_TO_FORMATTING.get(c[i]);
EnumChatFormatting formatting = CODE_TO_FORMATTING.get(c[i + 1]);

if (formatting == null) {
throw new IllegalArgumentException(
"Illegal formatting! Unknown color code character: " + c[i] + "!");
builder.append(prev);
continue;
}

finishPart();
i++;

switch (formatting) {
case OBFUSCATED:
style.setObfuscated(!style.getObfuscated());
break;
case BOLD:
style.setBold(!style.getBold());
break;
case STRIKETHROUGH:
style.setStrikethrough(!style.getStrikethrough());
break;
case UNDERLINE:
style.setUnderlined(!style.getUnderlined());
break;
case ITALIC:
style.setItalic(!style.getItalic());
break;
case RESET:
style = new ChatStyle();
break;
default:
style.setColor(formatting);
case OBFUSCATED -> style.setObfuscated(!style.getObfuscated());
case BOLD -> style.setBold(!style.getBold());
case STRIKETHROUGH -> style.setStrikethrough(!style.getStrikethrough());
case UNDERLINE -> style.setUnderlined(!style.getUnderlined());
case ITALIC -> style.setItalic(!style.getItalic());
case RESET -> style = new ChatStyle();
default -> style.setColor(formatting);
}

continue;
} else if (c[i] == '{') {
finishPart();

if (end) {
throw new IllegalArgumentException("Invalid formatting! Can't end string with {!");
builder.append(c[i]);
break;
}
finishPart();

sub = true;
}
Expand All @@ -182,28 +170,24 @@ private IChatComponent parse() {
private void finishPart() {
String string = builder.toString();
builder.setLength(0);
if (string.isEmpty()) return;

if (string.isEmpty()) {
return;
} else if (string.length() < 2 || string.charAt(0) != '{') {
IChatComponent component1 = new ChatComponentText(string);
IChatComponent component1 = new ChatComponentText(string);
if (string.length() < 2 || string.charAt(0) != '{') {
component1.setChatStyle(style.createShallowCopy());
component.appendSibling(component1);
return;
}

IChatComponent component1 = substitutes.apply(string.substring(1));

if (component1 != null) {
ChatStyle style0 = component1.getChatStyle().createShallowCopy();
ChatStyle style1 = style.createShallowCopy();
style1.setChatHoverEvent(style0.getChatHoverEvent());
style1.setChatClickEvent(style0.getChatClickEvent());
component1.setChatStyle(style1);
} else {
throw new IllegalArgumentException("Invalid formatting! Unknown substitute " + string);
if (substitutes != null) {
component1 = substitutes.apply(string.substring(1));
}

ChatStyle style0 = component1.getChatStyle().createShallowCopy();
ChatStyle style1 = style.createShallowCopy();
style1.setChatHoverEvent(style0.getChatHoverEvent());
style1.setChatClickEvent(style0.getChatClickEvent());
component1.setChatStyle(style1);
component.appendSibling(component1);
}
}

0 comments on commit e79b0df

Please sign in to comment.