Get player's country

Discussion in 'Plugin Development' started by rcth, Mar 23, 2015.

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

    rcth

    Hello,

    I have an international server and multiple languages (Dutch and English) are allowed on the server. I would like to lookup the country a player is from and show that when (s)he joins.

    I checked the forums and the known methods are old and probably won't even work anymore.

    Does anyone have a new and better solution for this? Thanks.
     
  2. Offline

    ColonelHedgehog

    Eh, you could use geo location, but that's not really accurate. It'd be better to just ask them before they join.
     
  3. Offline

    nverdier

    @ColonelHedgehog Well it's generally accurate enough to find the country...
     
  4. Offline

    ColonelHedgehog

    Not if they're using a VPN.

    Besides, they might not like you using your IP that way. Not to mention, I never had any luck with geolocation.
     
  5. Offline

    rcth

    I don't see how it's a problem to use the IP to get their country. Asking everytime is a lot of work.
     
  6. Offline

    ColonelHedgehog

    You don't have to.

    "Hey, it looks like you haven't been here before! Please use /lang English if you speak English."
    "Hallo, het lijkt erop dat je hier niet eerder geweest zijn! Gebruik /lang Nederlands als je spreekt Nederlands."
     
  7. Offline

    mine-care

    Ehh otherwise you can use a Spigot feature from EntityPlayer to get a field called locate that shows the language of the user, The problem is that is only for spigot. Lets go back to bukkit now. You can intercept packet called PacketPlayInSettings and use field a or invoke method c holding the language. Not sure if this is still present and if it still wotks, i havent player a lot with 1.8
    Hope that helps.
     
  8. Offline

    decontamin4t0R

    Actually, it exists even today! Look at wiki.vg (a minecraft protocol description site) for infos.
     
  9. Offline

    MajorSkillage

    What you could do is AsyncPlayerJoinEvent if !player.hasJoinedBefore() then ask them what language they talk in and base the language around that for them.
     
  10. You can get the language they're playing minecraft (most players play with their language) with Reflections:
    Code:
    public String getLang(Player p) {
        try {
          Object ep = getMethod("getHandle", p.getClass()).invoke(p,
            null);
          Field f = ep.getClass().getDeclaredField("locale");
          f.setAccessible(true);
          return (String) f.get(ep);
        } catch (Exception localException) {}
        return null;
      }
    
    private Method getMethod(String name, Class<?> clazz) {
        Method[] arrayOfMethod;
        int j = (arrayOfMethod = clazz.getDeclaredMethods()).length;
        for (int i = 0; i < j; i++) {
          Method m = arrayOfMethod[i];
          if (m.getName().equals(name))
            return m;
        }
        return null;
      }
    it returns something like de_DE or en_US
     
    ColonelHedgehog likes this.
  11. Offline

    stormneo7

    You can get their Country according to their IP Address.
    Code:
        public static String getCountry(InetSocketAddress ip) throws Exception {
            URL url = new URL("http://ip-api.com/json/" + ip.getHostName());
            BufferedReader stream = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            StringBuilder entirePage = new StringBuilder();
            String inputLine;
            while ((inputLine = stream.readLine()) != null)
                entirePage.append(inputLine);
            stream.close();
            if(!(entirePage.toString().contains("\"country\":\"")))
                return null;
            return entirePage.toString().split("\"country\":\"")[1].split("\",")[0];
        }
    In your code, you would use p.getAddress(); but in this case, I used Wikipedia's IP.
    This would return United States.
    Code:
    public static void main(String args[]) throws Exception {
        System.out.println(getCountry(new InetSocketAddress("208.80.152.201", 0)));
    }
     
  12. Offline

    ColonelHedgehog

    @FisheyLP's method seems like the best approach, however, because presumably if the person is using a language for Minecraft, that's the language they speak.
     
  13. Glad I could help :)
     
Thread Status:
Not open for further replies.

Share This Page