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

    tboss

    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

    edocsyl

    PHP:
     public void onPlayerLogin(PlayerLoginEvent e){

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



        }
     
  3. Offline

    tboss

    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

    DrAgonmoray

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

    tboss

    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 :
    PHP:
    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;
         public 
    SBTLoginPlayerListener(SBTLogin instance)
         {
             
    plugin instance;
         }

        public 
    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 "http://www.my-server.com/game/checkserver.php";
            
    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 :
    PHP:
        public String getIp(Player p) {
            
    String ipAddr p.getAddress().getHostName();
            return 
    ipAddr;
        }
    And everything works fine and I get no error if I replace this line
    String ip = plugin.getIp(player);
    by this line
    String ip = "127.0.0.1";

    Anyway I need to get this ip and not to use 127.0.0.1 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

    SirTyler

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

    tboss

    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

    SirTyler

    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

    edocsyl

    Soo... this is the code:
    PHP:
        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

    tboss

    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".
     
  11.  
    WizzleDonker likes this.
  12. Offline

    tboss

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

Share This Page