[Class] SkIP - Get informations about your players !

Discussion in 'Resources' started by Skyost, Jun 13, 2014.

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

    Skyost

    SkIP
    What is it ?
    This is class is just an alternative to this one. You have the choice, both are differents.​
    Features
    • Get the IP address of your players.
    • Get informations about IPs (weather, geo, ...) in a nice way.
    The class
    You can obtain it from here.​
    Example : how to use it ?
    So, here is an example :​
    Code:java
    1. public class SkIPExample extends JavaPlugin implements Listener {
    2.  
    3. @Override
    4. public final void onEnable() {
    5. Bukkit.getPluginManager().registerEvents(this, this);
    6. }
    7.  
    8. @EventHandler
    9. private final void onPlayerJoin(final PlayerJoinEvent event) {
    10. final Player player = event.getPlayer();
    11. event.setJoinMessage(player.getName() + " is coming from " + SkIP.getIPData(SkIP.getPlayerIP(player)).getCountryName() + " !");
    12. }
    13.  
    14. }

    I think you can understand this code easily ;)
    If you run it, you will get something like that :​
    [​IMG]
    Garris0n, this one is hosted on imgur :p
    That's all
    If you do not like SkIP, check IPUtils by Plo124.​
     
  2. Offline

    macboinc

    Cool! This is much easier than the other tutorial! Great for beginners!
     
    Skyost likes this.
  3. Offline

    Skyost

  4. Love it!

    Suggestion: Add a method of getting the time? I know it gets their timezone, but what about the actual time?
     
    Skyost likes this.
  5. Offline

    Skyost

    KingFaris11 likes this.
  6. Offline

    macboinc

    My code:
    Code:java
    1. if (cmd.getName().equalsIgnoreCase("stocker")) {
    2. if (p.hasPermission("hub.stocker")) {
    3. p.sendMessage(ChatColor.GREEN + "You are in: " + IP.getIPData(IP.getPlayerIP(p)).getCountryName());
    4. p.sendMessage("The weather in your area is: " + IP.getWeatherData(IP.getIPData(IP.getPlayerIP(p)).getCountryName(), IP.getIPData(IP.getPlayerIP(p)).getCountryCode()));
    5. return true;
    6. }
    7. }


    Stack trace:
    Code:
    [08:25:03 INFO]: macboinc1 issued server command: /stocker
    [08:25:03 WARN]: java.lang.NullPointerException
    [08:25:03 WARN]:        at me.macboinc1.hub.extras.IP.getIPData(IP.java:40)
    [08:25:03 WARN]:        at me.macboinc1.hub.Hub.onCommand(Hub.java:62)
    [08:25:03 WARN]:        at org.bukkit.command.PluginCommand.execute(PluginComman
    d.java:44)
    [08:25:03 WARN]:        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCo
    mmandMap.java:180)
    [08:25:03 WARN]:        at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCo
    mmand(CraftServer.java:728)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.PlayerConnection.handleC
    ommand(PlayerConnection.java:985)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.PlayerConnection.a(Playe
    rConnection.java:830)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(Packe
    tPlayInChat.java:28)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(
    PacketPlayInChat.java:65)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.NetworkManager.a(Network
    Manager.java:180)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.ServerConnection.c(Serve
    rConnection.java:81)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.v(Minecr
    aftServer.java:713)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.DedicatedServer.v(Dedica
    tedServer.java:283)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.u(Minecr
    aftServer.java:576)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.MinecraftServer.run(Mine
    craftServer.java:482)
    [08:25:03 WARN]:        at net.minecraft.server.v1_7_R3.ThreadServerApplication.
    run(SourceFile:628)
    [08:25:03 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'stoc
    ker' in plugin Hub v3.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spi
    got.jar:git-Spigot-1470]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:18
    0) ~[spigot.jar:git-Spigot-1470]
            at org.bukkit.craftbukkit.v1_7_R3.CraftServer.dispatchCommand(CraftServe
    r.java:728) ~[spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.PlayerConnection.handleCommand(PlayerCon
    nection.java:985) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :830) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:65) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:180
    ) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.ServerConnection.c(ServerConnection.java
    :81) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.MinecraftServer.v(MinecraftServer.java:7
    13) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.DedicatedServer.v(DedicatedServer.java:2
    83) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.MinecraftServer.u(MinecraftServer.java:5
    76) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.MinecraftServer.run(MinecraftServer.java
    :482) [spigot.jar:git-Spigot-1470]
            at net.minecraft.server.v1_7_R3.ThreadServerApplication.run(SourceFile:6
    28) [spigot.jar:git-Spigot-1470]
    Caused by: java.lang.NullPointerException
            at me.macboinc1.hub.Hub.onCommand(Hub.java:62) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spi
    got.jar:git-Spigot-1470]
            ... 13 more
     
  7. Offline

    Skyost

    macboinc This is SkIP, not IP (but your error may comes from the weather which is not perfectly implemented) :p
     
  8. Offline

    macboinc

    I renamed the SkIP to IP. How would I implement it "perfectly"?

    Don't worry I gave credit to you. :D
     
    GrandmaJam likes this.
  9. Offline

    Skyost

    macboinc This requires some modifications to this class, I will do it soon :)
    Yes I know, just thought the error comes from that ^^"
     
  10. Offline

    Phasesaber

    So amazing! I love the country function! I can see man servers setting this as there join message!
     
    Skyost likes this.
  11. Offline

    Ivan

    This is pretty cool, but I'm worrying about the fact that this may increase the duration of the PlayerLoginEvent by quite a lot since you're polling another website and have to wait for it to respond?
     
    Skyost likes this.
  12. Offline

    Skyost

    Ivan Yes :/
    But if your server has a quiete good connectivity, it will be fast ;)
     
  13. Offline

    Plo124

    Ivan
    Use Async Methods

    thanks, guys
     
  14. Offline

    Phasesaber

    When I do .getCity(), it returns my Zip Code.
     
  15. Offline

    Skyost

    Plo124 Async Method will not work when using setJoinMessage(...) in PlayerLoginEvent
    Phasesaber Yes, there is some few bugs left ^^ But you are free to participate.
     
  16. Offline

    Plo124

    Skyost then broadcast it after the login event. Maybe start getting the data in the AsyncPlayerPreLoginEvent and it might be ready by the time a player joins.
     
    Skyost likes this.
  17. Offline

    Skyost

  18. Offline

    Ivan

    Asynchronous methods should never the Bukkit API. Even broadcasting could cause the server to crash due to concurrent errors etc.
     
  19. Offline

    xTigerRebornx

    Ivan If he used the scheduler, he could schedule an async task that schedules a sync task when it gets the data to access the Bukkit API.
     
    Ivan likes this.
  20. Offline

    Skyost

    Ivan A sync task is safe man ._.
     
  21. Offline

    Ivan

    He totally could :), nice find man.
    A sync =! async :p, reread the post :)
     
    Skyost likes this.
  22. Offline

    mickedplay

    I know, this is very old, but I got a NullPointerException at line 42; I really don't know why.


    Code:
    package play.mickedplay.core;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import org.bukkit.entity.Player;
    import org.json.simple.JSONArray;
    import org.json.simple.JSONObject;
    import org.json.simple.parser.JSONParser;
    public class SkIP
    {
    private static final String OPEN_WEATHER_MAP_URL = "[URL]http://api.openweathermap.org/data/2.5/weather?q=%s,%s[/URL]";
    private static final String IP_INFO_DB_API_KEY = "NONE"; // If you do not setup, IP_INFO_DB will not work !
    public static final String SKIP_VERSION = "0.1";
    private static IPMode mode = IPMode.IP_API;
     
    public static final IPMode getIPMode()
    {
      return mode;
    }
     
    public static final void setIPMode(final IPMode mode)
    {
      SkIP.mode = mode;
    }
     
    public static final String getPlayerIP(final Player player)
    {
      return player.getAddress().getAddress().getHostAddress();
    }
     
    public static final SkIPData getIPData(final String ip)
    {
      try
      {
      final JSONObject json = (JSONObject)new JSONParser().parse(httpGet(mode.getQueryURL(ip)));
      final String[] args = mode.getArguments();
      return new SkIPData(json.get(args[0]).toString(),  // <<<<<<<<<<============== line 42
          json.get(args[1]).toString(),
          json.get(args[2]).toString(),
          json.get(args[3]).toString(),
          json.get(args[4]).toString(),
          Double.valueOf(json.get(args[5]).toString()),
          Double.valueOf(json.get(args[6]).toString()),
          json.get(args[7]).toString());
      }
      catch(final Exception ex){ex.printStackTrace();}
      return null;
    }
     
    public static final SkIPDataWeather getWeatherData(final String city, final String countryCode)
    {
      try
      {
      final JSONObject weather = (JSONObject)((JSONArray)((JSONObject)new JSONParser().parse(httpGet(String.format(OPEN_WEATHER_MAP_URL, city, countryCode)))).get("weather")).get(0);
      return new SkIPDataWeather(weather.get("main").toString(), weather.get("description").toString());
      }
      catch(final Exception ex){ex.printStackTrace();}
      return null;
    }
    private static final String httpGet(final String url) throws IOException
    {
      final HttpURLConnection connection = (HttpURLConnection)new URL(url).openConnection();
      connection.setRequestMethod("GET");
      connection.setRequestProperty("User-Agent", "SkIP");
      final String response = connection.getResponseCode() + " " + connection.getResponseMessage();
      if(!response.startsWith("200")){return null;}
      final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      String line;
      final StringBuilder builder = new StringBuilder();
      while((line = in.readLine()) != null){builder.append(line);}
      in.close();
      return builder.toString();
    }
     
    public static class SkIPData
    {
      private final String countryName;
      private final String countryCode;
      private final String region;
      private final String city;
      private final String zip;
      private final double latitude;
      private final double longitude;
      private final String timezone;
     
      public SkIPData(final String countryName, final String countryCode, final String region,
        final String zip, final String city, final double latitude, final double longitude, final String timezone)
      {
      this.countryName = countryName;
      this.countryCode = countryCode;
      this.region = region;
      this.zip = zip;
      this.city = city;
      this.latitude = latitude;
      this.longitude = longitude;
      this.timezone = timezone;
      }
     
      public final String getCountryName()
      {
      return countryName;
      }
     
      public final String getCountryCode()
      {
      return countryCode;
      }
     
      public final String getRegion()
      {
      return region;
      }
     
      public final String getZip()
      {
      return zip;
      }
     
      public final String getCity()
      {
      return city;
      }
     
      public final double getLatitude()
      {
      return latitude;
      }
     
      public final double getLongitude()
      {
      return longitude;
      }
     
      public final String getTimezone()
      {
      return timezone;
      }
     
    }
     
    public static class SkIPDataWeather
    {
      private final String weatherName;
      private final String weatherDesc;
     
      public SkIPDataWeather(final String weatherName, final String weatherDesc)
      {
      this.weatherName = weatherName;
      this.weatherDesc = weatherDesc;
      }
     
      public final String getWeatherName()
      {
      return weatherName;
      }
     
      public final String getWeatherDescription()
      {
      return weatherDesc;
      }
     
    }
     
    public enum IPMode
    {
      IP_API("[URL]http://ip-api.com/json/%s[/URL]", new String[]{"country", "countryCode", "regionName", "city", "zip", "lat", "lon", "timezone"}),
      IP_INFO_DB("[URL]http://api.ipinfodb.com/v3/ip-city/?key[/URL]=" + IP_INFO_DB_API_KEY + "&ip=%s", new String[]{"countryName", "countryCode", "regionName", "cityName", "zipCode", "latitude", "longitude", "timeZone"});
     
      private final String queryURL;
      private final String[] args;
     
      IPMode(final String queryURL, final String[] args)
      {
      this.queryURL = queryURL;
      this.args = args;
      }
     
      public final String getQueryURL(final String ip)
      {
      return String.format(queryURL, ip);
      }
     
      public final String[] getArguments()
      {
      return args;
      }
    }
    }
    
    PlayerJoinEvent:
    Code:
    public class EventPlayerJoin implements Listener
    {
    @EventHandler
    public void join(PlayerJoinEvent e)
    {
      Player p = e.getPlayer();
      p.getInventory().clear();
      p.getInventory().setArmorContents(null);
      e.setJoinMessage(p.getName() + " kommt aus " + SkIP.getIPData(SkIP.getPlayerIP(p)).getCountryName());
    }
    }
    
    Any help?
    Thanks! :)
     
  23. Offline

    Phasesaber

  24. Offline

    mickedplay

  25. Offline

    Phasesaber

    ChipDev likes this.
  26. Offline

    GrandmaJam

    Very nice :)
     
Thread Status:
Not open for further replies.

Share This Page