How to get Chat message?

If I am correct it should be like this. But that looks rather strange :frowning:.

//Wrapper Class for sponge chatevents. (using multiple api's in 1 plugin)
public class SpongeChatMessage extends ChatMessage {

    private PlayerChatEvent event;

    public SpongeChatMessage(PlayerChatEvent event){
        this.event = event;
    }

    @Override
    public String getMessage() {
        //hue not sure here :( ;-; 
        return (String) event.getMessage().getContent();
    }
}

Yeah, It does look strange, This is coming from someone who knows little java

@Override
public String getMessage() {
    return ((Message.Text) event.getMessage()).getContent();
}

I think it may be this. But still their is a chance I will get json crap everywhere :worried:.

I think you meant chance

maybe

//Wrapper Class for sponge chatevents. (using multiple api's in 1 plugin)
public class SpongeChatMessage extends ChatMessage {

    private PlayerChatEvent event;

    public SpongeChatMessage(PlayerChatEvent event){
        this.event = event;
    }

    @Override
    public String getMessage() {
        //hue not sure here :( ;-;
        String output = event.getMessage().getContent();
        return output
    }
}

Hows that different? Also, you missed a = .

Hmmm the garbage collector will like this :stuck_out_tongue:. (pretending that I didn’t see the missing =)

Indeed, whooops.

1 Like

This will change with https://github.com/SpongePowered/SpongeAPI/pull/425

When this PR gets accepted, it will be

String unformattedContent = Texts.toPlain(text);

https://github.com/SpongePowered/SpongeAPI/blob/feature/text/src/main/java/org/spongepowered/api/text/Texts.java

1 Like

used it before. from testing (and memory), getContent() from a Message.Text object returns a String representative of a plain text version of the contained and possibly formatted text.

https://github.com/Xemiru/Warden/commit/16882c8c415abab2225a03c1bb45427bae22e082 – think this was the working verison using that

Using getContent() is usually not a good idea because it only returns the content of the Message object itself, but does not include the plain text content of the children messages.

Assuming we have a Message with content in this simplified form: [BLUE]Hello, [GREEN]Minecrell!
Colors can be only set separately on new message instances so we would have a Message object with color TextColors.BLUE, the content Hello, , as well as a child message with TextColors.GREEN and content Minecrell!:

Message.Text {
    Color: BLUE,
    Content: "Hello, "
    Children: [
        Message.Text {
            Color: GREEN
            Content: "Minecrell!"
        }
    ]
}

Now if you would call getContent() on this message object it would return just Hello, instead of Hello, Minecrell! as expected. Texts.toPlain(text) in Text API 2 would return the correct result.

You should also keep in mind Message.Text is not the only message type, you would get an exception if the message instance would be a Message.Score for example. In these cases Texts.toPlain(text) might also return different results even when called with the same arguments because the Message.Score would be replaced with the actual score of the player instead of a simple plain text message.

5 Likes