Solved Getting a player's country?

Discussion in 'Rust Development' started by Daat, Apr 8, 2018.

  1. • How to get a player country ?
    Ex:

    Code:
    //Covalence Plugin
    string country = JsonConvert.DeserializeObject<Response>(response).Country;
    SendReply(player, "The player country is "+ country +"!");
    
    How to make this code in Rust Plugin?
     
    Last edited by a moderator: Apr 8, 2018
  2. Wulf

    Wulf Community Admin

    I'd suggest taking a look at the CountryBlock plugin of mine if you're looking for a more complete example.

    The example you posted is actually for a Rust plugin technically, you'd just need to add it to a command (see Docs or existing plugins).
     
  3. Ok, thanks Wulf
    [DOUBLEPOST=1523153202][/DOUBLEPOST]I have, if anyone else is in doubt, it is easy to solve.
    First add a using-statement:
    -
    Code:
    using Newtonsoft.Json;
    -
    Then create a class with JsonProperty:
    -
    Code:
    private class Response
    {
           [JsonProperty("country")]
           public string Country { get; set; }
    }
    -
    Then add this in your void:
    -

    Code:
    string apiUrl = "http://ip-api.com/json/";
                    webrequest.EnqueueGet(apiUrl + player.displayName, (code, response) =>
                    {
                    if (code != 200 || response == null)
                    {
                        Puts($"WebRequests to {apiUrl} failed!");
                        return;
                    }                    string country = JsonConvert.DeserializeObject<Response>(response).Country;
                    }, this);
    -
    And that's it, use it to your advantage.
     
    Last edited by a moderator: Apr 8, 2018
  4. If you think about it a little you will understand why this won't work