I am trying to find a packet or a method to listen to when a player joins the server your in.
I didn't find any packet or class the nearest think I found was the spawn player packet but this happens when a player enters view distance.
It seems there is multiple ways.
Use the EntityJoinWorldEvent
. Multiple post were talking about this one, not sure it's fully working and can't find documentation about it.
Listen for join message as "XX joined the game", this can be used only if it's for specific servers
Use packet. As you said, the PlayerInfoPacket can answer. To get it, you can you (at join of server for example) :
try {
Channel channel = ReflectionUtils.getFirstWith(player.networkHandler.connection, ClientConnection.class, Channel.class);
if(channel == null) {
getLogger().warn("Failed to load packet");
} else {
// Managing incoming packet
channel.pipeline().addBefore("packet_handler", "packet_handler" + p.getName(), new ChannelHandlerReceive(p));
}
} catch (Exception exc) {
exc.printStackTrace();
}
The "packet_handler"
is a specific key, but the "packet_handler" + p.getName()
can be replaced by what you want, it only will name your own listener.
For the ReflectionUtils.getFirstWith()
, you can check here for the code.
Then, add this class that will be able to read packets :
private class ChannelHandlerReceive extends ChannelInboundHandlerAdapter {
private final Player owner;
public ChannelHandlerReceive(Player player) {
this.owner = player;
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object packet) throws Exception {
// here check what is packet, and if it's the PlayerInfoPacket -> that's it !
super.channelRead(ctx, packet); // don't forget this line !!
}
}
For the full code I'm using, you can check here
PS: Once you have the packet, don't forget to check for the action (remove player, add player, update name etc)