Getting the ip of a player

Discussion in 'Plugin Development' started by tboss, Jan 8, 2012.

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


    Everything is in the title, I want to get a String of the player's ip from its Player object.
    Thanks in advance !
  2. Offline


     public void onPlayerLogin(PlayerLoginEvent e){

    Player p e.getPlayer();
    String sIp1 p.getAddress().getHostName();
    String sIp2 p.getAddress().getHostString();

  3. Offline


    I don't see the getHostString method, but getHostName exists.
    Anyway, it writes in my server console "Could not pass event PLAYER_LOGIN to SBTLogin , java.lang.NullPointerException, at the line I've written this : String ipAddr = p.getAddress().getHostName();
  4. Offline


    Are you connecting to localhost when you test that? I've had issues with null IPs when I connect with localhost.
  5. Offline


    Hm yes I am... I'll test.

    Same error when I connect to my internet ip or local ip.

    Here is the source of my playerListener class :
    package tboss.SBTLogin;

    import org.bukkit.entity.Player;
    import org.bukkit.event.player.PlayerListener;
    import org.bukkit.event.player.PlayerLoginEvent;

    public class 
    SBTLoginPlayerListener extends PlayerListener {
         public static 
    SBTLogin plugin;
    SBTLoginPlayerListener(SBTLogin instance)
    plugin instance;

    void onPlayerLogin(PlayerLoginEvent event) {
    Player player event.getPlayer();
    String userparam "user=".concat(player.getName());
    String ip plugin.getIp(player);
    String ipparam "&ip=".concat(ip);
    String parameters userparam.concat(ipparam);
    String loginURL "";
    String result HttpPostSender.sendPostRequest(loginURLparameters); // On envoie la requête et on récupère la réponse
    if(!result.equals("1")) event.disallow(PlayerLoginEvent.Result.KICK_WHITELISTresult);
    System.out.println("[SBTLogin] Params: ".concat(parameters));
    Here is the getIp function from the plugin class :
        public String getIp(Player p) {
    String ipAddr p.getAddress().getHostName();
    And everything works fine and I get no error if I replace this line
    String ip = plugin.getIp(player);
    by this line
    String ip = "";

    Anyway I need to get this ip and not to use anytime...

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


    mind if I ask what exactly you need the ip for?
  7. Offline


    Any pseudo corresponds to an ip. I want to verify if the person witch tries to connect has the correct ip. It uses for this a php script located on the internet.
  8. Offline


    Try having someone else to log onto your server, it could just be an issue with you logging in from your local machine or on the same network.
  9. Offline


    Soo... this is the code:
        public void onPlayerJoin(PlayerJoinEvent e){

    Player p e.getPlayer();
    InetSocketAddress IPAdressPlayer p.getAddress();
    String sfullip IPAdressPlayer.toString();
    String[] fullip;
    String[] ipandport;
    fullip sfullip.split("/");
    String sIpandPort fullip[1];
    ipandport sIpandPort.split(":");
    String sIp ipandport[0];
    p.sendMessage("You logged in with the ip: " sIp);
  10. Offline


    Why not getHostName() ?
    Anyway, it does a NullPointerException at this line : String sfullip = IPAdressPlayer.toString();
    It seems not to be able to use getAddress() while the player is still logging in. Any other way to get the ip ?
    I don't use the function onPlayerJoin(PlayerJoinEvent e), I use onPlayerLogin(PlayerLoginEvent event) because I need not to accept the player to join if the php script returns something else than "1".
    WizzleDonker likes this.
  12. Offline


    Last edited by a moderator: May 23, 2016
Thread Status:
Not open for further replies.

Share This Page