Getting the ip of a player

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

  1. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Everything is in the title, I want to get a String of the player's ip from its Player object.
    Thanks in advance !

    This post has been edited 1 time. It was last edited by tboss Jan 8, 2012.
  2. Offline

    edocsyl

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    PHP:

     
    public void onPlayerLogin(PlayerLoginEvent e){

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



        }

    This post has been edited 1 time. It was last edited by edocsyl Jan 8, 2012.
  3. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Are you connecting to localhost when you test that? I've had issues with null IPs when I connect with localhost.
  5. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Hm yes I am... I'll test.
  6. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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...

    This post has been edited 4 times. It was last edited by tboss Jan 11, 2012.
  7. Offline

    SirTyler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    mind if I ask what exactly you need the ip for?
  8. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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.

    This post has been edited 1 time. It was last edited by tboss Jan 10, 2012.
  9. Offline

    SirTyler

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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.
  10. Offline

    edocsyl

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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);
    }
  11. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    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".
  12. Offline

    V10lator

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    WizzleDonker likes this.
  13. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    I asked someone else to connect, and I had the same issue.
    I need a solution, everything else is ready to launch the launcher and the plugin !
    I already finished the launcher and the web pages !

    This post has been edited 2 times. It was last edited by tboss Jan 10, 2012.
  14. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
  15. Offline

    tboss

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME

Share This Page