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 !
     
  2. Offline

    edocsyl

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    PHP:
    1.  
    2.  public void onPlayerLogin(PlayerLoginEvent e){
    3.  
    4. Player p = e.getPlayer();
    5. String sIp1 = p.getAddress().getHostName();
    6. //or
    7. String sIp2 = p.getAddress().getHostString();
    8.  
    9.  
    10.  
    11.     }
     
  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:
    1. package tboss.SBTLogin;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.player.PlayerListener;
    5. import org.bukkit.event.player.PlayerLoginEvent;
    6.  
    7. public class SBTLoginPlayerListener extends PlayerListener {
    8.      public static SBTLogin plugin;
    9.      public SBTLoginPlayerListener(SBTLogin instance)
    10.      {
    11.          plugin = instance;
    12.      }
    13.  
    14.     public void onPlayerLogin(PlayerLoginEvent event) {
    15.         Player player = event.getPlayer();
    16.         String userparam = "user=".concat(player.getName());
    17.         String ip = plugin.getIp(player);
    18.         String ipparam = "&ip=".concat(ip);
    19.         String parameters = userparam.concat(ipparam);
    20.         String loginURL = "http://www.my-server.com/game/checkserver.php";
    21.         String result = HttpPostSender.sendPostRequest(loginURL, parameters); // On envoie la requête et on récupère la réponse
    22.         if(!result.equals("1")) event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, result);
    23.         System.out.println("[SBTLogin] Params: ".concat(parameters));
    24.     }
    25. }
    Here is the getIp function from the plugin class :
    PHP:
    1.     public String getIp(Player p) {
    2.         String ipAddr = p.getAddress().getHostName();
    3.         return ipAddr;
    4.     }
    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...
     
  7. Offline

    SirTyler

    My BukkitDev Profile
    My Plugins (6)
    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.
     
  9. Offline

    SirTyler

    My BukkitDev Profile
    My Plugins (6)
    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

    My BukkitDev Profile
    My Plugins (0)
    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Minecraft account:
    MCUSERNAME
    Soo... this is the code:
    PHP:
    1.     public void onPlayerJoin(PlayerJoinEvent e){
    2.  
    3.         Player p = e.getPlayer();
    4.         InetSocketAddress IPAdressPlayer = p.getAddress();
    5.         String sfullip = IPAdressPlayer.toString();
    6.         String[] fullip;
    7.         String[] ipandport;
    8.         fullip = sfullip.split("/");
    9.         String sIpandPort = fullip[1];
    10.         ipandport = sIpandPort.split(":");
    11.         String sIp = ipandport[0];
    12.  
    13.         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

    My BukkitDev Profile
    My Plugins (20)
    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 !
     
  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