Player ip on PlayerLoginEvent

Discussion in 'Plugin Development' started by illusion9, May 13, 2011.

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

    illusion9

    Hey guys
    I am a total newbie in java and bukkit, though not programming, so i am extremely sorry if a RTFM can help me. I've read the javadoc's and all i can get to is a call to getHandle().a.b.b() - i get lost after that, i think that references notch's code?

    I'm trying to improve on AnjoCaido's SimpleBan plugin - make it work like the vanilla banip, instead of players logging in and then getting kicked i want to totally cancel the login event.
    I found what i needed about the kicking itself, but i can't get the ip from the PlayerLoginEvent
    I guess there has to be a way to get the ip before the actual join, since the vanilla banip does that, but it always fails with a null pointer exception:
    PHP:
    java.lang.NullPointerException at org.bukkit.craftbukkit.entity.CraftPlayer.getAddress(CraftPlayer.java
    :48)
    I try to get the ip that way: (event is PlayerLoginEvent event)
    PHP:
    event.getPlayer().getAddress().getHostName()
    Is there actually a way to get the player ip at that point of the login, before the JOIN event has kicked in?
    And if there isn't is there a way to let the user log in the server so the ip will be available but suppress the login message (player joined the game)
     
  2. Offline

    illusion9

    Is it impossible?
     
  3. Offline

    EvilSeph

    Look into using the PlayerPreLoginEvent for this, instead.
     
  4. Offline

    illusion9

    Seems i am still doing something wrong. When does this event get called? Because i can't seem to catch it
    Doing this:
    PHP:
        @Override
        
    public void onPlayerPreLogin(PlayerPreLoginEvent event) {
            
    plugin.preJoinEventHandler(event);
            
    System.out.println("[test] prelogin");
        }
        @
    Override
        
    public void onPlayerLogin(PlayerLoginEvent event) {
            
    plugin.mapPlayerExistance(event.getPlayer().getName());
            
    plugin.joinEventHandler(event);
            
    System.out.println("[test] login");
        } 
    In the listener class (extends PlayerListener) i only get 16:26:49 [INFO] [test] login in the console, not prelogin
    And the ip address variable I set in preJoinEventHandler isn't touched, stays the same as defined in onEnable()
    PHP:
        public void preJoinEventHandler(PlayerPreLoginEvent event) {
            
    this.address event.getAddress();
        }
    So basically it seems this event isn't being called for some reason?
     
  5. Offline

    EvilSeph

    onPlayerPreLogin only works in online mode.

    You can get the ip in onPlayerLogin by setting the result to Allowed and getting the message. Something like this should work:
    Code:
        public void onPlayerLogin(PlayerLoginEvent event) {
            if (event.getResult().equals(Result.ALLOWED)) {
                ip = event.getKickMessage();
            }
        }
    This thread is in the wrong location, so I'm moving it.
     
  6. Offline

    robin0van0der0v

    You can also use:
    Code:
    String ip = event.getPlayer().getAddress().toString().substring(1,event.getPlayer().getAddress().toString().length()).split(":")[0]);
     
  7. Offline

    illusion9

    Thank you so much for the help :)
    I finally got it working. Bukkit has an awesome community.

    Oh, wow, that's a long line of code. Thanks for helping, though i got it working using EvilSeph's way.
     
Thread Status:
Not open for further replies.

Share This Page