Jump to content

[1.11.2] [SOLVED] Get Message From ClientChatReceivedEvent


Draconwolver

Recommended Posts

How do I get the message from a ClientChatReceivedEvent in the form of a TextComponentString such as

new TextComponentString(TextFormatting.AQUA + "I like trains.");

Everything I've tried so far just results in a very long String with data about the message as opposed to just the message itself.

Edited by Draconwolver
Link to comment
Share on other sites

ClientChatReceivedEvent#getMessage returns the message as an ITextComponent. This could be an instance of any class that implements ITextComponent, e.g. TextComponentString or TextComponentTranslation.

 

What are you trying to achieve?

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

On 6/1/2017 at 2:39 AM, Choonster said:

ClientChatReceivedEvent#getMessage returns the message as an ITextComponent. This could be an instance of any class that implements ITextComponent, e.g. TextComponentString or TextComponentTranslation.

 

What are you trying to achieve?

Thank you, I figured it out with that information. I cast the ITextComponent to a TextComponentString and then compared TextComponentString::getFormattedText to my own String with some TextFormatting in it. I also noted that the placement of formatting must match exactly for the text to be considered equal.

Edited by Draconwolver
Link to comment
Share on other sites

3 hours ago, Draconwolver said:

I cast the ITextComponent to a TextComponentString and then compared TextComponentString#getFormattedText to my own String with some TextFormatting in it.

 

This will fail if the ITextComponent isn't a TextComponentStringITextComponent already provides the getFormattedText method, you shouldn't need to cast it.

 

3 hours ago, Draconwolver said:

I also noted that the placement of formatting must match exactly for the text to be considered equal.

 

You can use ITextComponent#getUnformattedComponentText to get the text without the formatting codes.

 

All vanilla ITextComponents also override Object#equals, so you can use this to compare the two messages.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

On 6/2/2017 at 0:23 AM, Choonster said:

You can use ITextComponent#getUnformattedComponentText to get the text without the formatting codes.

For some reason ITextComponent's method didn't work for me, but TextComponentString's did as I expected. Perhaps it is because the message I am listening for contains

On 6/2/2017 at 4:16 AM, diesieben07 said:

formatting codes that are directly included in the text

 

 

On 6/2/2017 at 4:16 AM, diesieben07 said:

You should not concatenate formatting codes directly into the string like that, use the Style object that can be accessed with ITextComponent::getStyle to set the style of the text.

What if the text contains multiple styles that may or may not be complexly strung together? E.g.

String text = TextFormatting.RED + "My favorite color is " + TextFormatting.BOLD + TextFormatting.ITALIC + "red" + TextFormatting.RED + ". " + TextFormatting.YELLOW + "Can you read " + TextFormatting.OBFUSCATED + "this" + TextFormatting.GOLD + "?";

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.