Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

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.

  • Author
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

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.

  • Author
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 + "?";

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.