Getting player IP Address correctly?

Discussion in 'Plugin Development' started by WALK2222, Feb 28, 2013.

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


    Hi there,
    I'm trying to retrieve a player's IP Address and send it back out, but when I do so, I get something that says "IP Address: CraftPlayer{name=WALK2222}"

    All I want it to say is "Ip Address: (ipaddress)"

    Could someone help me fix this?
    My code is below, thanks!

    package com.github.walk2222;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.ChatColor;
    public class PlayerIp {
        public static String getip(Player playerip, CommandSender sender){
            playerip.sendMessage(ChatColor.AQUA + "Ip Address: "+playerip);
            return null;
  2. You're not asigning anything there, playerip.getAddress() returns something, it doesn't change the "playerip" object to anything else, one reason is that it simply can't since it's a Player object and not a NetAddress (or whatever) object, and another reason is that it's returning the value and you're not using "obj = value".

    And I belive it's player.getAddress().getAddress().getHostName(), look through the methods and decide for yourself which one is getting the address.
  3. Offline


    I use
    That will return the player's IP address their currently using without a port.
  4. Offline


    public String getIp(Player p){
        return p.getAddress().getAddress().getHostAddress();
  5. Offline


    String ipWithPort = player.getAdress().getAdress().getHostAdress();
    String[] split = ipWithPort.split("/");
    String ip = split[0];
    String port = split[1];
  6. Code:
    import org.bukkit.entity.Player;
    public class PlayerIP {
        public static String getIP(Player player) {
            String playerIP = player.getAddress().getHostName();
            return playerIP;
        public static int getPort(Player player) {
            int playerPort = player.getAddress().getPort();
            return playerPort;
    You don't always want the port number with the IP so these two methods can be called from anywhere in the plugin using PlayerIP .getIP(player); or PlayerIP .getPort(player); and will return a String.
  7. AlexLeporiday
    Why are you creating a pointer to use it once ?
  8. As an example that you could do more. It should be obvious that in this condition it would be better to just return the value but as the posted showed, they don't know how to store variables.
    It was to emphasize how the methods work.

    Why did you tell them to use player.getAddress().getAddress().getHostName() which gives a Java format internet address rather than a bukkit format? They didn't ask for port as well :p
  9. AlexLeporiday

    Well it should be obvious, if you can return something, it can be stored :p The point was to not give him the impression that it's a necesity to store it before returning, it's only needed to be stored when you're using it more than once, WALK2222.

    And I told him to look through the methods, because if you're just gonna give him the methods without explaining how you find them, he'll just keep on posting about methods and won't be able to find methods, which would severely degrade his coding speed.
  10. Offline


    Thanks for all your help guys!
Thread Status:
Not open for further replies.

Share This Page