1.16.5 Player current IP-Address

Discussion in 'Spigot Plugin Development' started by Bosternike, Jul 18, 2021.

Thread Status:
Not open for further replies.
  1. I need to get the player's real Address (something like this: 208.80.152.201), but not like this: 192.168.56.107

    I know, it's possible, but does anyone know HOW?

    I use method:
    Code (Text):
    Player.getAddress().getHostString();
     
  2. If this is on a local machine, then you get the local address.

    Otherwise you need to use #getHostAddress on to get the address of the player.
     
  3. Thanks! Will try
     
  4. By the way, I use this code to get the player Country. But it returns wrong IP (the result is «fail»).

    Code (Text):

    public static String getGeo(String ip, String c) {
            try {
                URL url = new URL("http://ip-api.com/json/" + ip + "?fields=status," + c + ",query&lang=en");
                            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                StringBuilder sb = new StringBuilder();
                String input;
                while((input = br.readLine()) != null) {
                    sb.append(input);
                }
                br.close();
                if(!sb.toString().split("\"status\":\"")[1].split("\",")[0].equals("success")) {
                    Bukkit.getConsoleSender().sendMessage(sb.toString().split("\"status\":\"")[1].split("\",")[0]);
                    return null;
                }
           
                return sb.toString().split("\"{0}\":\"".replace("{0}", c))[1].split("\",")[0];
            } catch (Exception e) {
                return null;
            }
        }
     
    Code (Text):

    getGeo(player.getAddress().getAddress().getHostAddress(), "country");
     
     
  5. I tried, and the same problem. My IP is – 192.168..., and my real IP is quite different.
     
  6. 192.168.***.*** is this is the address within the local network. You probably host the server inside the local network and connect inside it.
     
    • Like Like x 1
  7. So, would everything be okay not on local host?
     
  8. Right
     
    • Like Like x 1
  9. Thanks!
     
Thread Status:
Not open for further replies.