2
2
3
3
import java .security .MessageDigest ;
4
4
import java .security .NoSuchAlgorithmException ;
5
+ import java .util .ArrayList ;
5
6
import java .util .HashMap ;
7
+ import java .util .List ;
6
8
import java .util .Map ;
7
9
8
10
import org .apache .http .HttpResponse ;
11
+ import org .apache .http .NameValuePair ;
9
12
import org .apache .http .client .HttpClient ;
13
+ import org .apache .http .client .entity .UrlEncodedFormEntity ;
10
14
import org .apache .http .client .methods .HttpPost ;
11
15
import org .apache .http .entity .StringEntity ;
12
16
import org .apache .http .impl .client .HttpClientBuilder ;
17
+ import org .apache .http .message .BasicNameValuePair ;
13
18
14
- import com .google .gson .Gson ;
19
+ import com .google .gson .JsonObject ;
20
+ import com .google .gson .JsonPrimitive ;
15
21
import com .tamashenning .forgeanalytics .client .ForgeAnalyticsConstants ;
16
22
import com .tamashenning .forgeanalytics .client .ForgeAnalyticsSingleton ;
23
+ import com .tamashenning .forgeanalytics .events .AnalyticsEvent ;
17
24
import com .tamashenning .forgeanalytics .models .AnalyticsModel ;
18
25
19
26
import net .minecraft .client .Minecraft ;
20
27
import net .minecraft .server .MinecraftServer ;
21
- import net .minecraft .server .integrated .IntegratedServer ;
22
28
import net .minecraftforge .common .ForgeVersion ;
23
- import net .minecraftforge .fml .common .ModContainer ;
29
+ import net .minecraftforge .common .MinecraftForge ;
30
+ import net .minecraftforge .common .config .Configuration ;
24
31
25
32
public class AnalyticsClient {
26
33
27
34
public boolean UploadModel (AnalyticsModel model , boolean isClient ) throws Exception {
28
35
36
+ if (!FireEvent (isClient )) {
37
+ return false ;
38
+ }
39
+
29
40
model .Properties .putAll (ForgeAnalyticsConstants .CustomProperties );
30
41
31
- Gson g = new Gson ();
32
- String json = g .toJson (model );
42
+ // Code to send to Tamas backend...
33
43
34
- return this .UploadModel (json , isClient );
35
- }
44
+ JsonObject data = new JsonObject ();
45
+ data .add ("PartitionKey" , new JsonPrimitive (model .PartitionKey ));
46
+ data .add ("ClientDateTimeEpoch" , new JsonPrimitive (model .ClientDateTimeEpoch ));
47
+ data .add ("Table" , new JsonPrimitive (model .Table ));
36
48
37
- private boolean UploadModel ( String json , boolean isClient ) throws Exception {
49
+ JsonObject propertiesMap = new JsonObject ();
38
50
39
- if (isClient ) {
40
- // Respect snooper settings...
41
- if (!Minecraft .getMinecraft ().isSnooperEnabled ()) {
42
- return false ;
43
- }
44
- } else {
45
- // Respect snooper settings...
46
- if (!MinecraftServer .getServer ().isSnooperEnabled ()) {
47
- return false ;
51
+ for (Map .Entry <String , String > entry : model .Properties .entrySet ()) {
52
+ // if the user opted out of the property, respect it...
53
+ if (!ForgeAnalyticsConstants .dataConfig
54
+ .get (Configuration .CATEGORY_GENERAL , entry .getKey () + "_OptOut" , false ).getBoolean ()) {
55
+
56
+ propertiesMap .add (entry .getKey (), new JsonPrimitive (entry .getValue ()));
48
57
}
49
58
}
50
59
51
- System .out .println (json );
52
- HttpClient httpClient = HttpClientBuilder .create ().build (); // Use this
53
- // instead
60
+ data .add ("Properties" , propertiesMap );
54
61
55
- try {
56
- HttpPost request = new HttpPost (ForgeAnalyticsConstants .serverUrl );
62
+ String json = data .toString ();
57
63
58
- StringEntity params = new StringEntity (json );
59
- request .addHeader ("content-type" , "application/json" );
60
- request .setEntity (params );
61
- HttpResponse response = httpClient .execute (request );
62
- System .out .println (response .toString ());
63
- // handle response here...
64
- } catch (Exception ex ) {
65
- // handle exception here
66
- ex .printStackTrace ();
64
+ // Code to send to Lex' backend
65
+
66
+ JsonObject dataForge = new JsonObject ();
67
+ dataForge .add ("cmd" , new JsonPrimitive (model .PartitionKey ));
68
+
69
+ for (Map .Entry <String , String > entry : model .Properties .entrySet ()) {
70
+ // if the user opted out of the property, respect it...
71
+ if (!ForgeAnalyticsConstants .dataConfig
72
+ .get (Configuration .CATEGORY_GENERAL , entry .getKey () + "_OptOut" , false ).getBoolean ()) {
73
+
74
+ dataForge .add (entry .getKey (), new JsonPrimitive (entry .getValue ()));
75
+ }
67
76
}
68
77
69
- return true ;
78
+ this .UploadForge (dataForge .toString ());
79
+ return this .UploadModel (json , isClient );
70
80
}
71
81
72
82
public AnalyticsModel CreateClientStartupPing () {
@@ -87,24 +97,19 @@ public AnalyticsModel CreateServerStartupPing() {
87
97
am .PartitionKey = ForgeAnalyticsConstants .pingServerStartCommand ;
88
98
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
89
99
am .Properties .putAll (this .getCommonValues ());
90
- am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
91
-
92
- MinecraftServer server = MinecraftServer .getServer ();
93
-
94
- if (MinecraftServer .getServer ().isDedicatedServer ()) {
95
- // Running dedicated...
96
- try {
97
- am .Properties .put ("ServerHostHash" , this .Anonymize (server .getHostname ()));
98
- } catch (NoSuchAlgorithmException e ) {
99
- // TODO Auto-generated catch block
100
- e .printStackTrace ();
101
- }
102
- } else {
103
- // Running internal...
104
- am .Properties .put ("ServerHostHash" , "localhost" );
105
- am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
106
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
107
- }
100
+ // am.Properties.put("ServerDifficulty",
101
+ // MinecraftServer.getServer().getDifficulty().toString());
102
+
103
+ /*
104
+ * MinecraftServer server = MinecraftServer.getServer();
105
+ *
106
+ * if (MinecraftServer.getServer().isDedicatedServer()) { // Running
107
+ * dedicated... try { am.Properties.put("ServerHostHash",
108
+ * this.Anonymize(server.getHostname())); } catch
109
+ * (NoSuchAlgorithmException e) { // TODO Auto-generated catch block
110
+ * e.printStackTrace(); } } else { // Running internal...
111
+ * am.Properties.put("ServerHostHash", "localhost"); }
112
+ */
108
113
109
114
return am ;
110
115
}
@@ -116,24 +121,19 @@ public AnalyticsModel CreateServerStoppedPing() {
116
121
am .PartitionKey = ForgeAnalyticsConstants .pingServerStopCommand ;
117
122
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
118
123
am .Properties .putAll (this .getCommonValues ());
119
- am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
120
-
121
- MinecraftServer server = MinecraftServer .getServer ();
122
-
123
- if (MinecraftServer .getServer ().isDedicatedServer ()) {
124
- // Running dedicated...
125
- try {
126
- am .Properties .put ("ServerHostHash" , this .Anonymize (server .getHostname ()));
127
- } catch (NoSuchAlgorithmException e ) {
128
- // TODO Auto-generated catch block
129
- e .printStackTrace ();
130
- }
131
- } else {
132
- // Running internal...
133
- am .Properties .put ("ServerHostHash" , "localhost" );
134
- am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
135
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
136
- }
124
+ // am.Properties.put("ServerDifficulty",
125
+ // MinecraftServer.getServer().getDifficulty().toString());
126
+
127
+ /*
128
+ * MinecraftServer server = MinecraftServer.getServer();
129
+ *
130
+ * if (MinecraftServer.getServer().isDedicatedServer()) { // Running
131
+ * dedicated... try { am.Properties.put("ServerHostHash",
132
+ * this.Anonymize(server.getHostname())); } catch
133
+ * (NoSuchAlgorithmException e) { // TODO Auto-generated catch block
134
+ * e.printStackTrace(); } } else { // Running internal...
135
+ * am.Properties.put("ServerHostHash", "localhost"); }
136
+ */
137
137
138
138
return am ;
139
139
}
@@ -156,54 +156,116 @@ public AnalyticsModel CreateServerKeepAlivePing() {
156
156
am .PartitionKey = ForgeAnalyticsConstants .pingServerKeepAlive ;
157
157
am .ClientDateTimeEpoch = System .currentTimeMillis () / 1000L ;
158
158
am .Properties .putAll (this .getCommonValues ());
159
- am .Properties .put ("ServerDifficulty" , MinecraftServer .getServer ().getDifficulty ().toString ());
159
+ // am.Properties.put("ServerDifficulty",
160
+ // MinecraftServer.getServer().getDifficulty().toString());
161
+
162
+ /*
163
+ * Removing this part for now... MinecraftServer server =
164
+ * MinecraftServer.getServer();
165
+ *
166
+ * if (MinecraftServer.getServer().isDedicatedServer()) { // Running
167
+ * dedicated... try { if (server != null) {
168
+ * am.Properties.put("ServerHostHash",
169
+ * this.Anonymize(server.getHostname())); } } catch
170
+ * (NoSuchAlgorithmException e) { // TODO Auto-generated catch block
171
+ * e.printStackTrace(); } am.Properties.put("ConnectedUsers",
172
+ * Integer.toString(server.getCurrentPlayerCount())); } else { //
173
+ * Running internal... am.Properties.put("ServerHostHash", "localhost");
174
+ * }
175
+ */
160
176
161
- MinecraftServer server = MinecraftServer .getServer ();
177
+ return am ;
178
+ }
162
179
163
- if (MinecraftServer .getServer ().isDedicatedServer ()) {
164
- // Running dedicated...
165
- try {
166
- am .Properties .put ("ServerHostHash" , this .Anonymize (server .getHostname ()));
167
- } catch (NoSuchAlgorithmException e ) {
168
- // TODO Auto-generated catch block
169
- e .printStackTrace ();
180
+ public boolean FireEvent (boolean isClient ) {
181
+ if (isClient ) {
182
+ // Respect snooper settings...
183
+ if (!Minecraft .getMinecraft ().isSnooperEnabled ()) {
184
+ return false ;
170
185
}
171
- am .Properties .put ("ConnectedUsers" , Integer .toString (server .getCurrentPlayerCount ()));
186
+ MinecraftForge .EVENT_BUS .post (new AnalyticsEvent (net .minecraftforge .fml .relauncher .Side .CLIENT ));
187
+
172
188
} else {
173
- // Running internal...
174
- am .Properties .put ("ServerHostHash" , "localhost" );
175
- am .Properties .put ("IsDemo" , Boolean .toString (server .isDemo ()));
176
- am .Properties .put ("IsLanMode" , Boolean .toString (((IntegratedServer ) server ).getPublic ()));
177
- }
189
+ // Respect snooper settings...
190
+ if (!MinecraftServer .getServer ().isSnooperEnabled ()) {
191
+ return false ;
192
+ }
178
193
179
- return am ;
194
+ MinecraftForge .EVENT_BUS .post (new AnalyticsEvent (net .minecraftforge .fml .relauncher .Side .SERVER ));
195
+ }
196
+ return true ;
180
197
}
181
198
182
199
private Map <String , String > getCommonValues () {
183
200
Map <String , String > commonValues = new HashMap <String , String >();
184
- String activeModListCount = Integer
185
- .toString (net .minecraftforge .fml .common .Loader .instance ().getActiveModList ().size ());
201
+
202
+ String activeModListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getActiveModList ().size ());
203
+
186
204
String modListCount = Integer .toString (net .minecraftforge .fml .common .Loader .instance ().getModList ().size ());
187
- String modList = "" ;
205
+ // String modList = "";
188
206
189
207
commonValues .put ("JavaVersion" , System .getProperty ("java.version" ));
190
208
commonValues .put ("JavaMaxRAM" , Long .toString (Runtime .getRuntime ().maxMemory ()));
191
209
commonValues .put ("JavaAllocatedRAM" , Long .toString (Runtime .getRuntime ().totalMemory ()));
210
+ // TODO: Remove once fully switched over.
192
211
commonValues .put ("SessionID" , ForgeAnalyticsSingleton .getInstance ().SessionID );
193
212
commonValues .put ("AdID" , ForgeAnalyticsConstants .AdID );
194
- commonValues .put ("MinecraftVersion" , Minecraft .getMinecraft ().getVersion ());
213
+ commonValues .put ("session_id" , ForgeAnalyticsSingleton .getInstance ().SessionUUID .toString ());
214
+ commonValues .put ("instance_id" , ForgeAnalyticsConstants .InstanceUUID .toString ());
215
+ commonValues .put ("MinecraftVersion" , net .minecraftforge .fml .common .Loader .instance ().getMCVersionString ());
195
216
commonValues .put ("ForgeVersion" , ForgeVersion .getVersion ());
196
- commonValues .put ("MCPVersion" , ForgeVersion . mcpVersion );
217
+ commonValues .put ("MCPVersion" , net . minecraftforge . fml . common . Loader . instance (). getMCPVersionString () );
197
218
commonValues .put ("ActiveModCount" , activeModListCount );
198
219
commonValues .put ("ModCount" , modListCount );
220
+ commonValues .put ("ModPack" , ForgeAnalyticsConstants .modPack );
221
+ /*
222
+ * for (ModContainer mod :
223
+ * cpw.mods.fml.common.Loader.instance().getModList()) { modList +=
224
+ * mod.getModId() + "@" + mod.getVersion() + ";"; }
225
+ *
226
+ * commonValues.put("ModList", modList);
227
+ */
228
+
229
+ return commonValues ;
230
+ }
231
+
232
+ private boolean UploadModel (String json , boolean isClient ) throws Exception {
233
+
234
+ HttpClient httpClient = HttpClientBuilder .create ().build ();
235
+
236
+ try {
237
+ HttpPost request = new HttpPost (ForgeAnalyticsConstants .serverUrl );
238
+
239
+ StringEntity params = new StringEntity (json );
240
+ request .addHeader ("content-type" , "application/json" );
241
+ request .setEntity (params );
242
+ HttpResponse response = httpClient .execute (request );
243
+
199
244
200
- for ( ModContainer mod : net . minecraftforge . fml . common . Loader . instance (). getModList () ) {
201
- modList += mod . getModId () + "@" + mod . getVersion () + ";" ;
245
+ } catch ( Exception ex ) {
246
+ ex . printStackTrace () ;
202
247
}
203
248
204
- commonValues .put ("ModList" , modList );
249
+ return true ;
250
+ }
205
251
206
- return commonValues ;
252
+ private void UploadForge (String json ) throws Exception {
253
+
254
+ HttpClient httpClient = HttpClientBuilder .create ().build ();
255
+
256
+ try {
257
+ HttpPost request = new HttpPost (ForgeAnalyticsConstants .forgeServerUrl );
258
+
259
+ List <NameValuePair > nvp = new ArrayList <NameValuePair >();
260
+ nvp .add (new BasicNameValuePair ("stat" , json ));
261
+
262
+ request .setEntity (new UrlEncodedFormEntity (nvp ));
263
+ HttpResponse response = httpClient .execute (request );
264
+
265
+ } catch (Exception ex ) {
266
+ // handle exception here
267
+ ex .printStackTrace ();
268
+ }
207
269
}
208
270
209
271
public String Anonymize (String data ) throws NoSuchAlgorithmException {
0 commit comments