Open
Issue #20 · created by Nikita Medvedev · ·

ChatUtilities.convertLegacyCodes(chat) seems to duplicate ITextComponent siblings

Hello.

I was working on a mod that uses inbound chat filter. So, I have implemented ChatFilter interface in main mod class. Like this:

@Override
public boolean onChat(ITextComponent chat, String message, LiteLoaderEventBroker.ReturnValue<ITextComponent> newMessage) {
    return Core.getInstance().onChat(chat, newMessage);
}

So, code of Core.onChat (pretty dirty right now because of testing things):

public boolean onChat(ITextComponent chat, LiteLoaderEventBroker.ReturnValue<ITextComponent> newMessage) {
    boolean isPassed = true;

    TextComponentString cmp = new TextComponentString("Word ");
    cmp.appendSibling(new TextComponentString(" another "));
    cmp.appendSibling(new TextComponentString("word"));

    newMessage.set(cmp);

    if(newMessage.isSet())
        System.out.println(newMessage.get().toString());
    else
        System.out.println("Chat is not modified");

    return isPassed;
}

And the result of it is like this:

Word  another word another word

So, siblings are duplicated. I created mixin that overwrites PacketEventsClient.handlePacket method, copy-pasted original code but commented out this line (line 259 in the original source file):

chat = ChatUtilities.convertLegacyCodes(chat);

And it seems that there is no more sibling duplication:

Word  another word

Am I doing something wrong or there is bug in LiteLoader?

2 participants
liteloader/LiteLoader#20
  • Title changed from ChatUtilities.convertLegacyCodes(chat) seems to duplicate ITextComponent siblings to ChatUtilities.convertLegacyCodes(chat) seems to duplicate ITextComponent siblings

  • It's entirely possible that this is a bug in ChatUtilities, I'll take a look into it. Thanks for the detailed report.

  • Nikita Medvedev @Red-Teapot ·

    It seems that I have found the issue.

    textComponent.getFormattedText() on line 97 returns formatted text of itself and its siblings already (See TextComponentBase.getFormattedText(), TextComponentString does not override this method). So, we get formatted siblings' text too (I have noticed that all events, for example, HoverEvent, disappear on the first sibling repetition, but formatting like colors and font effects stays the same).

    Then siblings are added one more time on lines 129 - 132 of ChatUtilities.covertCodesInPlace(). But now they are properly added because events work.

    If I replace String text = textComponent.getFormattedText(); on line 97 in ChatUtilities file with something like this:

    String text = style.getFormattingCode() + component.getUnformattedComponentText() + TextFormatting.RESET;

    Then sibling duplication disappears. But I am not sure that the rest works properly then.

  • I see, it's possible that the behaviour was changed since ChatUtilities was written but nobody has used it so nobody noticed the bug. I will look into rewriting it when I have time, obviously christmas is happening now so that might not be immediately.