Posted June 1, 20178 yr 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 June 5, 20178 yr by Draconwolver
June 1, 20178 yr 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.
June 2, 20178 yr 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 June 5, 20178 yr by Draconwolver
June 2, 20178 yr 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 TextComponentString. ITextComponent 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.
June 3, 20178 yr 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.