Sponge networking API - get a player on RawDataListener.handlePayload

Sup. I’ve got a piece of code taken from this thread. It’s a channel listener.

channel.get("test").addListener(Platform.Type.SERVER, (buf, con, side) -> {
        logger.info("Message on test channel");
    });

And I need to get the player object. Simple, huh? I’ve googled a lot and couldn’t found anything that could help me.
Please help <:

con is an instance of RemoteConnection and if the connection is from a player then it will be an instance of PlayerConnection, which contains a getPlayer method.

So in your listener do something like this:

(buf, con, side) -> {
    if (con instanceof PlayerConnection) {
        Player player = ((PlayerConnection) con).getPlayer();
        // Do something with player
    }
}
1 Like

Ok, it’s all about casting. Your help is very helpful, thanks!