NullPointerException on Player.kick()

When I try to kick a player when they join I get a NullPointerException which is kinda weird with that whole Optional thing…

Any ideas?

@Listener
public void onPlayerJoin(ClientConnectionEvent.Login e) {
    GameProfile profile = e.getProfile();
    Optional<Player> oPlayer = e.getTargetUser().getPlayer();
    if(oPlayer.isPresent()) {
        oPlayer.get().kick(Text.of("kicked!"));
    }
}

java.lang.NullPointerException: null
    at net.minecraft.entity.player.EntityPlayerMP.kick(SourceFile:3196) ~[oq.class:?]

Not sure what you want …

@Listener
public void join(ClientConnectionEvent.Join event, @Getter("getTargetEntity") Player player){
    player.kick(Text.of("kicked"));
}  

[minecraft/DedicatedServer]: Eric12324 joined the game
[minecraft/NetHandlerPlayServer]: Eric12324 lost connection: kicked
[minecraft/DedicatedServer]: Eric12324 left the game

So the login event is fired just before the player fully joins. The Player object is created yet not fully ready. Therefore (as Eric12324 said) use the Join event

Login is the better place to do it though. All you need to do is cancel the event and, if you want a custom message, use setMessage on the Login event…

Oh right, I can cancel the event, haven’t thought of that, thanks :wink: