-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
StatsRecord.java
96 lines (88 loc) · 3.75 KB
/
StatsRecord.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package org.mcmonkey.sentinel.metrics;
import org.bukkit.Bukkit;
import org.mcmonkey.sentinel.SentinelPlugin;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
public class StatsRecord extends Thread {
public static void trigger() {
StatsRecord recorder = new StatsRecord();
recorder.gather();
recorder.start();
}
public static String hash_md5(byte[] bytes) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(bytes, 0, bytes.length);
return new BigInteger(1, md.digest()).toString(16).substring(0, 16);
}
catch (Throwable ex) {
ex.printStackTrace();
}
return null;
}
public void gather() {
// We don't need the real value of the MOTD / port, but they're useful for differentiating, so use them to generate a hash
String differentiator = hash_md5((Bukkit.getServer().getMotd() + Bukkit.getServer().getPort()).getBytes(StandardCharsets.UTF_8));
String mcVersion = Bukkit.getVersion();
int firstDash = mcVersion.indexOf('-');
int secondDash = firstDash == -1 ? -1 : mcVersion.indexOf('-', firstDash + 1);
int mcPart = mcVersion.indexOf("(MC: ");
int endPart = mcPart == -1 ? -1 : mcVersion.indexOf(")", mcPart);
String platform = secondDash == -1 ? "" : mcVersion.substring(firstDash + 1, secondDash);
mcVersion = (endPart == -1) ? "" : mcVersion.substring(mcPart + "(MC: ".length(), endPart);
content = "postid=pluginstats&plugin=Denizen"
+ "&differentiator=" + differentiator
+ "&pl_plugin_version=" + URLEncoder.encode(SentinelPlugin.instance.getDescription().getVersion())
+ "&pl_platform=" + URLEncoder.encode(platform)
+ "&pl_minecraft_version=" + URLEncoder.encode(mcVersion)
+ "&pl_player_count=" + Bukkit.getOnlinePlayers().size();
}
public String content;
@Override
public void run() {
BufferedReader in = null;
try {
// Open a connection to the stats server
URL url = new URL("https://stats.mcmonkey.org/Stats/Submit"
+ URLEncoder.encode(SentinelPlugin.instance.getDescription().getVersion()));
HttpURLConnection uc = (HttpURLConnection) url.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setConnectTimeout(10000);
uc.connect();
// Safely connected at this point
// Create the final message pack and upload it
uc.getOutputStream().write(content.getBytes(StandardCharsets.UTF_8));
// Wait for a response from the server
in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
// Record the response
String Result = in.readLine();
// TODO: Use return?
// Close the connection
in.close();
}
catch (Exception e) {
if (SentinelPlugin.debugMe) {
System.out.println("Sentinel stat issue: " + e.getMessage());
}
}
finally {
try {
if (in != null) {
in.close();
}
}
catch (Exception e) {
if (SentinelPlugin.debugMe) {
System.out.println("Sentinel stat issue (backup): " + e.getMessage());
}
}
}
}
}