-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
StatsRecord.java
59 lines (55 loc) · 2.22 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
package org.mcmonkey.sentinel.metrics;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.mcmonkey.sentinel.SentinelPlugin;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
public class StatsRecord extends Thread {
@Override
public void run() {
BufferedReader in = null;
try {
// Open a connection to the stats server
URL url = new URL("http://neo.mcmonkey.org/plugins/public_stats?plugin=Sentinel&version="
+ 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(("postid=pluginstats&plugin_st_players=" + Bukkit.getOnlinePlayers().size()
+ "&plugin_st_server_version=" + URLEncoder.encode(Bukkit.getVersion())
+ "&plugin_st_motd=" + URLEncoder.encode(Bukkit.getServer().getMotd().replace(ChatColor.COLOR_CHAR, (char) 0x01)))
.getBytes("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());
}
}
}
}
}