onPlayerLogin issue

Discussion in 'Plugin Development' started by wouter0100, Jun 7, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    wouter0100

    Hey,

    I have a problem with my OnPlayerLogin event.
    When i try to get the IP adres, i got a nullpointer error.

    I have tryed many things like:
    Code:
    String ip = event.getAddress().toString().substring(1);
    
    Code:
    Player player = event.getPlayer();
    String ip = player.getAddress().toString().substring(1);
    
    But it wont work.

    Error:

    Running on the last Craftbukkit build/bukkit build.

    I'll hope you can help me.
    Thanks,
    Wouter0100
     
  2. Offline

    davejavu

    Well, the way I do it is:
    Code:java
    1.  
    2. String ipfull = evt.getPlayer().getAddress().toString();
    3.  

    That gives you the FULL address, INCLUDING port and the /, so after that, to get JUST the IP, I do these steps:
    Code:java
    1.  
    2. String[] ipsplit = ipfull.split("/");
    3.  

    That gives you the ip in this form: 0.0.0.0:58182 for example, now you need to get rid of the port.
    Code:java
    1.  
    2. String[] ipclean = ipsplit[1].split(":");
    3. String ip = ipclean
    4.  

    I find those steps to work, 5 lines of code :p
     
  3. Offline

    Njol

    or use player.getAddress().getAddress().getHostAddress() - 1 line

    But I think that the problem here is that the player's address is not yet set in PlayerLoginEvent - thus the getAddress() method in the event.
    @op do you get a NPE with event.getAddress().getHostAddress()?
     
  4. Offline

    wouter0100

    This works! Great! Thanks.
     
  5. Offline

    McLuke500

    I ran into this problem awhile back!
    Code:
    @EventHandler
            public void onPlayerLogin(PlayerLoginEvent event) {
                Player player = event.getPlayer();
                String playername = player.getName();
                String ip;
                ip = event.getAddress().getHostAddress();
    Aww I was too slow ;(

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  6. Offline

    wouter0100

    Yes ;p, anyways thanks!
     
  7. Offline

    davejavu

    And now sir, I feel like a derp.
    xD
     
Thread Status:
Not open for further replies.

Share This Page