diff --git a/src/main/java/sh/okx/rankup/Rankup.java b/src/main/java/sh/okx/rankup/Rankup.java index 0da953e..bd3d0da 100644 --- a/src/main/java/sh/okx/rankup/Rankup.java +++ b/src/main/java/sh/okx/rankup/Rankup.java @@ -33,6 +33,7 @@ import sh.okx.rankup.ranks.requirements.RequirementRegistry; import sh.okx.rankup.ranks.requirements.XpLevelRequirement; import java.io.File; +import java.io.IOException; import java.text.DecimalFormat; import java.util.List; import java.util.Map; @@ -111,6 +112,14 @@ public class Rankup extends JavaPlugin { getLogger().severe("You may then copy in your config values from the old config."); getLogger().severe("Check the changelog on the Rankup spigot page to see the changes."); } + + if(config.getBoolean("stats")) { + try { + new Stats().init(this); + } catch (IOException e) { + e.printStackTrace(); + } + } } /** diff --git a/src/main/java/sh/okx/rankup/Stats.java b/src/main/java/sh/okx/rankup/Stats.java new file mode 100644 index 0000000..cc2e0bc --- /dev/null +++ b/src/main/java/sh/okx/rankup/Stats.java @@ -0,0 +1,55 @@ +package sh.okx.rankup; + +import org.bukkit.Bukkit; + +import java.io.File; +import java.io.IOException; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.InetAddress; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.util.Base64; +import java.util.HashMap; +import java.util.Map; +import java.util.StringJoiner; + +public class Stats { + public void init(Rankup plugin) throws IOException { + URL url = new URL("http://rankup.okx.sh/api"); + URLConnection con = url.openConnection(); + HttpURLConnection http = (HttpURLConnection) con; + http.setRequestMethod("POST"); + http.setDoOutput(true); + Map arguments = new HashMap<>(); + arguments.put("ip", InetAddress.getLocalHost().getHostAddress() + ":" + Bukkit.getPort()); + arguments.put("spigot", Bukkit.getVersion()); + arguments.put("version", plugin.getDescription().getVersion()); + Base64.Encoder base64 = Base64.getEncoder(); + File data = plugin.getDataFolder(); + arguments.put("config", base64.encodeToString(Files.readAllBytes(data.toPath().resolve("config.yml")))); + arguments.put("messages", base64.encodeToString(Files.readAllBytes(data.toPath().resolve("messages.yml")))); + arguments.put("rankups", base64.encodeToString(Files.readAllBytes(data.toPath().resolve("rankups.yml")))); + StringJoiner sj = new StringJoiner("&"); + for (Map.Entry entry : arguments.entrySet()) { + try { + sj.add(URLEncoder.encode(entry.getKey(), "UTF-8") + "=" + + URLEncoder.encode(entry.getValue(), "UTF-8")); + } catch (UnsupportedEncodingException e) { + throw new AssertionError("UTF-8 not found."); + } + } + byte[] out = sj.toString().getBytes(StandardCharsets.UTF_8); + int length = out.length; + http.setFixedLengthStreamingMode(length); + http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + http.connect(); + try (OutputStream os = http.getOutputStream()) { + os.write(out); + } + } +} diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index efb012a..3e92d8b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -1,6 +1,10 @@ # this is used for letting you know that you need to update/change your config file version: 0 +# as well as bStats (which can be disabled in the plugins/bStats folder), +# this will send more detailed stats privately, to help see what features are being used. +stats: true + # whether /ranks should be enabled (true) or disabled (false) # /rankup3 reload will not do anything if this is changed, # you will have to restart your server.