A one size fits all plugin for vanilla servers running Minecraft Beta 1.7.3
at master 5.6 kB view raw
1package dev.keii.goldenage.statistics; 2 3import dev.keii.goldenage.statistics.listeners.PlayerJoinListener; 4import org.bukkit.Bukkit; 5import org.bukkit.entity.Player; 6import org.bukkit.plugin.PluginManager; 7import org.bukkit.plugin.java.JavaPlugin; 8 9import javax.annotation.Nullable; 10import javax.net.ssl.HttpsURLConnection; 11import java.io.BufferedReader; 12import java.io.InputStream; 13import java.io.InputStreamReader; 14import java.io.OutputStream; 15import java.net.HttpURLConnection; 16import java.net.URI; 17import java.net.URL; 18import java.nio.charset.StandardCharsets; 19import java.util.HashSet; 20import java.util.Set; 21 22public class Statistics { 23 private final JavaPlugin plugin; 24 private final String remote; 25 private final @Nullable Integer serverId; 26 private final @Nullable String serverSecret; 27 public final Set<String> uniquePlayersPerSchedule; 28 29 public Statistics(JavaPlugin plugin, String remote, int serverId, String serverSecret) { 30 this.plugin = plugin; 31 this.remote = remote; 32 this.serverId = serverId; 33 this.serverSecret = serverSecret; 34 this.uniquePlayersPerSchedule = new HashSet<>(); 35 } 36 37 protected Statistics(JavaPlugin plugin, String remote) { 38 this.plugin = plugin; 39 this.remote = remote; 40 this.serverId = null; 41 this.serverSecret = null; 42 this.uniquePlayersPerSchedule = new HashSet<>(); 43 } 44 45 final int ticksInASecond = 20; 46 final int secondsInAnHour = 3600; 47 48 public void beginScheduler() { 49 // Register Events 50 PluginManager pm = Bukkit.getPluginManager(); 51 pm.registerEvents(new PlayerJoinListener(this), this.plugin); 52 53 // Register Scheduler 54 int interval = ticksInASecond * secondsInAnHour; 55 Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, () -> { 56 sendStatistics(); 57 Bukkit.getLogger().info("[GoldenAge] Sent automated statistics"); 58 }, interval, interval); 59 } 60 61 public Integer sendStatistics() { 62 int playerCount = this.uniquePlayersPerSchedule.size(); 63 // Reset unique player count for next schedule 64 this.uniquePlayersPerSchedule.clear(); 65 // Re-add all online players 66 for (Player player : Bukkit.getOnlinePlayers()) { 67 this.uniquePlayersPerSchedule.add(player.getUniqueId().toString()); 68 } 69 70 // String gameVersion = this.plugin.getServer().getGameVersion(); 71 String gameVersion = "b1.7.3"; // getGameVersion() is broken currently waiting for upstream fix 72 String serverEnvironment = this.plugin.getServer().getServerEnvironment(); 73 String operatingSystem = System.getProperty("os.name"); 74 String arch = System.getProperty("os.arch"); 75 String javaVersion = System.getProperty("java.version"); 76 77 try { 78 URL url = (new URI(this.remote + "/api/statistics")).toURL(); 79 HttpURLConnection conn; 80 if (url.getProtocol().equalsIgnoreCase("https")) { 81 conn = (HttpsURLConnection) url.openConnection(); 82 } else { 83 conn = (HttpURLConnection) url.openConnection(); 84 } 85 86 conn.setRequestMethod("POST"); 87 conn.setRequestProperty("Content-Type", "application/json; utf-8"); 88 conn.setRequestProperty("Accept", "application/json"); 89 conn.setDoOutput(true); 90 91 StringBuilder body = new StringBuilder(); 92 93 body.append("{"); 94 if (this.serverId != null) { 95 body.append("\"serverId\":").append(this.serverId).append(","); 96 body.append("\"serverSecret\":\"").append(this.serverSecret).append("\","); 97 } 98 99 body.append("\"playerCount\":").append(playerCount).append(","); 100 body.append("\"gameVersion\":\"").append(gameVersion).append("\","); 101 body.append("\"serverEnvironment\":\"").append(serverEnvironment).append("\","); 102 body.append("\"operatingSystem\":\"").append(operatingSystem).append("\","); 103 body.append("\"arch\":\"").append(arch).append("\","); 104 body.append("\"javaVersion\":\"").append(javaVersion).append("\""); 105 106 body.append("}"); 107 108 String jsonInputString = body.toString(); 109 110 // Send JSON body 111 try (OutputStream os = conn.getOutputStream()) { 112 byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8); 113 os.write(input, 0, input.length); 114 } 115 116 // Handle Error 117 if (conn.getResponseCode() != 201) { 118 InputStream responseStream; 119 120 if (conn.getResponseCode() >= 200 && conn.getResponseCode() < 300) { 121 responseStream = conn.getInputStream(); 122 } else { 123 responseStream = conn.getErrorStream(); 124 } 125 126 BufferedReader in = new BufferedReader(new InputStreamReader(responseStream, 127 StandardCharsets.UTF_8)); 128 StringBuilder response = new StringBuilder(); 129 String line; 130 while ((line = in.readLine()) != null) { 131 response.append(line).append("\n"); 132 } 133 in.close(); 134 135 Bukkit.getLogger().severe( 136 "Error when submitting statistics: " + conn.getResponseMessage() + " " + response.toString()); 137 } 138 139 return conn.getResponseCode(); 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 return null; 144 } 145}