12
12
import org .apache .http .impl .client .HttpClientBuilder ;
13
13
14
14
import com .google .gson .Gson ;
15
- import com .tamashenning .forgeanalyitcs .client .ForgeAnalyticsConstants ;
16
- import com .tamashenning .forgeanalyitcs .client .ForgeAnalyticsSingleton ;
15
+ import com .tamashenning .forgeanalytics .client .ForgeAnalyticsConstants ;
16
+ import com .tamashenning .forgeanalytics .client .ForgeAnalyticsSingleton ;
17
17
import com .tamashenning .forgeanalytics .models .AnalyticsModel ;
18
18
19
19
import net .minecraft .client .Minecraft ;
23
23
import net .minecraftforge .fml .common .ModContainer ;
24
24
25
25
public class AnalyticsClient {
26
-
26
+
27
27
public boolean UploadModel (AnalyticsModel model ) throws Exception {
28
28
29
29
model .Properties .putAll (ForgeAnalyticsConstants .CustomProperties );
30
-
30
+
31
31
Gson g = new Gson ();
32
32
String json = g .toJson (model );
33
33
34
34
return this .UploadModel (json );
35
35
}
36
-
36
+
37
37
private boolean UploadModel (String json ) throws Exception {
38
-
38
+
39
39
// Respect snooper settings...
40
- if (!Minecraft .getMinecraft ().isSnooperEnabled ()) {
40
+ if (!Minecraft .getMinecraft ().isSnooperEnabled ()) {
41
41
return false ;
42
42
}
43
-
43
+
44
44
System .out .println (json );
45
- HttpClient httpClient = HttpClientBuilder .create ().build (); //Use this instead
46
-
47
- try {
48
- HttpPost request = new HttpPost (ForgeAnalyticsConstants .serverUrl );
49
-
50
- StringEntity params =new StringEntity (json );
51
- request .addHeader ("content-type" , "application/json" );
52
- request .setEntity (params );
53
- HttpResponse response = httpClient .execute (request );
54
- System .out .println (response .toString ());
55
- // handle response here...
56
- } catch (Exception ex ) {
57
- // handle exception here
58
- ex .printStackTrace ();
59
- }
60
-
45
+ HttpClient httpClient = HttpClientBuilder .create ().build (); // Use this
46
+ // instead
47
+
48
+ try {
49
+ HttpPost request = new HttpPost (ForgeAnalyticsConstants .serverUrl );
50
+
51
+ StringEntity params = new StringEntity (json );
52
+ request .addHeader ("content-type" , "application/json" );
53
+ request .setEntity (params );
54
+ HttpResponse response = httpClient .execute (request );
55
+ System .out .println (response .toString ());
56
+ // handle response here...
57
+ } catch (Exception ex ) {
58
+ // handle exception here
59
+ ex .printStackTrace ();
60
+ }
61
+
61
62
return true ;
62
63
}
63
-
64
+
64
65
public AnalyticsModel CreateClientStartupPing () {
65
66
AnalyticsModel am = new AnalyticsModel ();
66
67
am .Table = ForgeAnalyticsConstants .pingClientTable ;
67
68
am .Properties = new HashMap <String , String >();
68
69
am .PartitionKey = ForgeAnalyticsConstants .pingClientStartCommand ;
69
70
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
70
71
am .Properties .putAll (this .getCommonValues ());
71
-
72
+
72
73
return am ;
73
74
}
74
-
75
+
75
76
public AnalyticsModel CreateServerStartupPing () {
76
77
AnalyticsModel am = new AnalyticsModel ();
77
78
am .Table = ForgeAnalyticsConstants .pingServerTable ;
@@ -80,9 +81,9 @@ public AnalyticsModel CreateServerStartupPing() {
80
81
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
81
82
am .Properties .putAll (this .getCommonValues ());
82
83
am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
83
-
84
+
84
85
MinecraftServer server = MinecraftServer .getServer ();
85
-
86
+
86
87
if (MinecraftServer .getServer ().isDedicatedServer ()) {
87
88
// Running dedicated...
88
89
try {
@@ -91,17 +92,16 @@ public AnalyticsModel CreateServerStartupPing() {
91
92
// TODO Auto-generated catch block
92
93
e .printStackTrace ();
93
94
}
94
- }
95
- else {
95
+ } else {
96
96
// Running internal...
97
97
am .Properties .put ("ServerHostHash" , "localhost" );
98
98
am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
99
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer )server ).getPublic ()));
99
+ am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
100
100
}
101
101
102
102
return am ;
103
103
}
104
-
104
+
105
105
public AnalyticsModel CreateServerStoppedPing () {
106
106
AnalyticsModel am = new AnalyticsModel ();
107
107
am .Table = ForgeAnalyticsConstants .pingServerTable ;
@@ -110,9 +110,9 @@ public AnalyticsModel CreateServerStoppedPing() {
110
110
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
111
111
am .Properties .putAll (this .getCommonValues ());
112
112
am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
113
-
113
+
114
114
MinecraftServer server = MinecraftServer .getServer ();
115
-
115
+
116
116
if (MinecraftServer .getServer ().isDedicatedServer ()) {
117
117
// Running dedicated...
118
118
try {
@@ -121,17 +121,16 @@ public AnalyticsModel CreateServerStoppedPing() {
121
121
// TODO Auto-generated catch block
122
122
e .printStackTrace ();
123
123
}
124
- }
125
- else {
124
+ } else {
126
125
// Running internal...
127
126
am .Properties .put ("ServerHostHash" , "localhost" );
128
127
am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
129
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer )server ).getPublic ()));
128
+ am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
130
129
}
131
130
132
131
return am ;
133
132
}
134
-
133
+
135
134
public AnalyticsModel CreateClientKeepAlivePing () {
136
135
AnalyticsModel am = new AnalyticsModel ();
137
136
am .Table = ForgeAnalyticsConstants .pingClientTable ;
@@ -142,7 +141,7 @@ public AnalyticsModel CreateClientKeepAlivePing() {
142
141
143
142
return am ;
144
143
}
145
-
144
+
146
145
public AnalyticsModel CreateServerKeepAlivePing () {
147
146
AnalyticsModel am = new AnalyticsModel ();
148
147
am .Table = ForgeAnalyticsConstants .pingServerTable ;
@@ -151,9 +150,9 @@ public AnalyticsModel CreateServerKeepAlivePing() {
151
150
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
152
151
am .Properties .putAll (this .getCommonValues ());
153
152
am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
154
-
153
+
155
154
MinecraftServer server = MinecraftServer .getServer ();
156
-
155
+
157
156
if (MinecraftServer .getServer ().isDedicatedServer ()) {
158
157
// Running dedicated...
159
158
try {
@@ -163,24 +162,23 @@ public AnalyticsModel CreateServerKeepAlivePing() {
163
162
e .printStackTrace ();
164
163
}
165
164
am .Properties .put ("ConnectedUsers" , Integer .toString (server .getCurrentPlayerCount ()));
166
- }
167
- else {
165
+ } else {
168
166
// Running internal...
169
167
am .Properties .put ("ServerHostHash" , "localhost" );
170
168
am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
171
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer )server ).getPublic ()));
169
+ am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
172
170
}
173
171
174
172
return am ;
175
173
}
176
174
177
-
178
175
private Map <String , String > getCommonValues () {
179
176
Map <String , String > commonValues = new HashMap <String , String >();
180
- String activeModListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getActiveModList ().size ());
177
+ String activeModListCount = Integer
178
+ .toString (net .minecraftforge .fml .common .Loader .instance ().getActiveModList ().size ());
181
179
String modListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getModList ().size ());
182
180
String modList = "" ;
183
-
181
+
184
182
commonValues .put ("JavaVersion" , System .getProperty ("java.version" ));
185
183
commonValues .put ("JavaMaxRAM" , Long .toString (Runtime .getRuntime ().maxMemory ()));
186
184
commonValues .put ("JavaAllocatedRAM" , Long .toString (Runtime .getRuntime ().totalMemory ()));
@@ -192,35 +190,34 @@ private Map<String, String> getCommonValues() {
192
190
commonValues .put ("ActiveModCount" , activeModListCount );
193
191
commonValues .put ("ModCount" , modListCount );
194
192
195
- for (ModContainer mod : net .minecraftforge .fml .common .Loader .instance ().getModList ()){
196
- modList += mod .getModId ()+ "@" + mod .getVersion ()+ ";" ;
193
+ for (ModContainer mod : net .minecraftforge .fml .common .Loader .instance ().getModList ()) {
194
+ modList += mod .getModId () + "@" + mod .getVersion () + ";" ;
197
195
}
198
196
199
197
commonValues .put ("ModList" , modList );
200
198
201
-
202
199
return commonValues ;
203
200
}
204
-
205
-
201
+
206
202
public String Anonymize (String data ) throws NoSuchAlgorithmException {
207
203
MessageDigest sha256 = MessageDigest .getInstance ("SHA-256" );
208
204
byte [] dataBytes = data .getBytes ();
209
- for (int i = 0 ; i < ForgeAnalyticsConstants .HASHCOUNT ; i ++) {
205
+ for (int i = 0 ; i < ForgeAnalyticsConstants .HASHCOUNT ; i ++) {
210
206
dataBytes = sha256 .digest (dataBytes );
211
207
}
212
-
208
+
213
209
return this .bytesToHex (dataBytes );
214
210
}
215
-
211
+
216
212
final protected char [] hexArray = "0123456789abcdef" .toCharArray ();
213
+
217
214
private String bytesToHex (byte [] bytes ) {
218
- char [] hexChars = new char [bytes .length * 2 ];
219
- for ( int j = 0 ; j < bytes .length ; j ++ ) {
220
- int v = bytes [j ] & 0xFF ;
221
- hexChars [j * 2 ] = hexArray [v >>> 4 ];
222
- hexChars [j * 2 + 1 ] = hexArray [v & 0x0F ];
223
- }
224
- return new String (hexChars );
215
+ char [] hexChars = new char [bytes .length * 2 ];
216
+ for (int j = 0 ; j < bytes .length ; j ++) {
217
+ int v = bytes [j ] & 0xFF ;
218
+ hexChars [j * 2 ] = hexArray [v >>> 4 ];
219
+ hexChars [j * 2 + 1 ] = hexArray [v & 0x0F ];
220
+ }
221
+ return new String (hexChars );
225
222
}
226
223
}
0 commit comments