Skip to content

Commit 4d2a852

Browse files
author
Tamas Henning
committed
Updating 1.8.9 version
1 parent 2000c95 commit 4d2a852

File tree

9 files changed

+344
-111
lines changed

9 files changed

+344
-111
lines changed

src/java/com/tamashenning/forgeanalytics/AnalyticsClient.java

+155-93
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,81 @@
22

33
import java.security.MessageDigest;
44
import java.security.NoSuchAlgorithmException;
5+
import java.util.ArrayList;
56
import java.util.HashMap;
7+
import java.util.List;
68
import java.util.Map;
79

810
import org.apache.http.HttpResponse;
11+
import org.apache.http.NameValuePair;
912
import org.apache.http.client.HttpClient;
13+
import org.apache.http.client.entity.UrlEncodedFormEntity;
1014
import org.apache.http.client.methods.HttpPost;
1115
import org.apache.http.entity.StringEntity;
1216
import org.apache.http.impl.client.HttpClientBuilder;
17+
import org.apache.http.message.BasicNameValuePair;
1318

14-
import com.google.gson.Gson;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonPrimitive;
1521
import com.tamashenning.forgeanalytics.client.ForgeAnalyticsConstants;
1622
import com.tamashenning.forgeanalytics.client.ForgeAnalyticsSingleton;
23+
import com.tamashenning.forgeanalytics.events.AnalyticsEvent;
1724
import com.tamashenning.forgeanalytics.models.AnalyticsModel;
1825

1926
import net.minecraft.client.Minecraft;
2027
import net.minecraft.server.MinecraftServer;
21-
import net.minecraft.server.integrated.IntegratedServer;
2228
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;
2431

2532
public class AnalyticsClient {
2633

2734
public boolean UploadModel(AnalyticsModel model, boolean isClient) throws Exception {
2835

36+
if (!FireEvent(isClient)) {
37+
return false;
38+
}
39+
2940
model.Properties.putAll(ForgeAnalyticsConstants.CustomProperties);
3041

31-
Gson g = new Gson();
32-
String json = g.toJson(model);
42+
// Code to send to Tamas backend...
3343

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));
3648

37-
private boolean UploadModel(String json, boolean isClient) throws Exception {
49+
JsonObject propertiesMap = new JsonObject();
3850

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()));
4857
}
4958
}
5059

51-
System.out.println(json);
52-
HttpClient httpClient = HttpClientBuilder.create().build(); // Use this
53-
// instead
60+
data.add("Properties", propertiesMap);
5461

55-
try {
56-
HttpPost request = new HttpPost(ForgeAnalyticsConstants.serverUrl);
62+
String json = data.toString();
5763

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+
}
6776
}
6877

69-
return true;
78+
this.UploadForge(dataForge.toString());
79+
return this.UploadModel(json, isClient);
7080
}
7181

7282
public AnalyticsModel CreateClientStartupPing() {
@@ -87,24 +97,19 @@ public AnalyticsModel CreateServerStartupPing() {
8797
am.PartitionKey = ForgeAnalyticsConstants.pingServerStartCommand;
8898
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
8999
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+
*/
108113

109114
return am;
110115
}
@@ -116,24 +121,19 @@ public AnalyticsModel CreateServerStoppedPing() {
116121
am.PartitionKey = ForgeAnalyticsConstants.pingServerStopCommand;
117122
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
118123
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+
*/
137137

138138
return am;
139139
}
@@ -156,54 +156,116 @@ public AnalyticsModel CreateServerKeepAlivePing() {
156156
am.PartitionKey = ForgeAnalyticsConstants.pingServerKeepAlive;
157157
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
158158
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+
*/
160176

161-
MinecraftServer server = MinecraftServer.getServer();
177+
return am;
178+
}
162179

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;
170185
}
171-
am.Properties.put("ConnectedUsers", Integer.toString(server.getCurrentPlayerCount()));
186+
MinecraftForge.EVENT_BUS.post(new AnalyticsEvent(net.minecraftforge.fml.relauncher.Side.CLIENT));
187+
172188
} 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+
}
178193

179-
return am;
194+
MinecraftForge.EVENT_BUS.post(new AnalyticsEvent(net.minecraftforge.fml.relauncher.Side.SERVER));
195+
}
196+
return true;
180197
}
181198

182199
private Map<String, String> getCommonValues() {
183200
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+
186204
String modListCount = Integer.toString(net.minecraftforge.fml.common.Loader.instance().getModList().size());
187-
String modList = "";
205+
// String modList = "";
188206

189207
commonValues.put("JavaVersion", System.getProperty("java.version"));
190208
commonValues.put("JavaMaxRAM", Long.toString(Runtime.getRuntime().maxMemory()));
191209
commonValues.put("JavaAllocatedRAM", Long.toString(Runtime.getRuntime().totalMemory()));
210+
// TODO: Remove once fully switched over.
192211
commonValues.put("SessionID", ForgeAnalyticsSingleton.getInstance().SessionID);
193212
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());
195216
commonValues.put("ForgeVersion", ForgeVersion.getVersion());
196-
commonValues.put("MCPVersion", ForgeVersion.mcpVersion);
217+
commonValues.put("MCPVersion", net.minecraftforge.fml.common.Loader.instance().getMCPVersionString());
197218
commonValues.put("ActiveModCount", activeModListCount);
198219
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+
199244

200-
for (ModContainer mod : net.minecraftforge.fml.common.Loader.instance().getModList()) {
201-
modList += mod.getModId() + "@" + mod.getVersion() + ";";
245+
} catch (Exception ex) {
246+
ex.printStackTrace();
202247
}
203248

204-
commonValues.put("ModList", modList);
249+
return true;
250+
}
205251

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+
}
207269
}
208270

209271
public String Anonymize(String data) throws NoSuchAlgorithmException {

src/java/com/tamashenning/forgeanalytics/ForgeAnalyticsMod.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
package com.tamashenning.forgeanalytics;
22

3-
import com.tamashenning.forgeanalytics.proxies.CommonProxy;
3+
import java.util.logging.Logger;
44

5+
import com.tamashenning.forgeanalytics.proxies.CommonProxy;
6+
import com.tamashenning.forgeanalytics.client.*;
57
import net.minecraftforge.fml.common.Mod;
68
import net.minecraftforge.fml.common.Mod.EventHandler;
9+
import net.minecraftforge.fml.common.Mod.Instance;
710
import net.minecraftforge.fml.common.SidedProxy;
811
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
9-
import net.minecraftforge.fml.common.event.FMLLoadCompleteEvent;
1012
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
1113
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
1214
import net.minecraftforge.fml.common.event.FMLServerStartedEvent;
1315
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
1416
import net.minecraftforge.fml.common.event.FMLServerStoppedEvent;
15-
import net.minecraftforge.fml.relauncher.Side;
16-
import net.minecraftforge.fml.relauncher.SideOnly;
1717

18-
@Mod(modid = ForgeAnalyticsMod.MODID, name = ForgeAnalyticsMod.MODNAME, version = ForgeAnalyticsMod.VERSION)
18+
@Mod(modid = ForgeAnalyticsMod.MODID, name = ForgeAnalyticsMod.MODNAME, version = ForgeAnalyticsMod.VERSION, guiFactory = ForgeAnalyticsMod.GUIFACTORY)
1919
public class ForgeAnalyticsMod {
2020

2121
public static final String MODID = "forgeanalytics";
2222
public static final String MODNAME = "Forge Analytics";
23-
public static final String VERSION = "0.0.4";
23+
public static final String VERSION = "0.0.0.13";
24+
public static final String GUIFACTORY = "com.tamashenning.forgeanalytics.gui.GuiFactory";
25+
26+
@Instance(ForgeAnalyticsMod.MODID)
27+
public static ForgeAnalyticsMod instance;
2428

2529
@SidedProxy(clientSide = "com.tamashenning.forgeanalytics.proxies.ClientProxy", serverSide = "com.tamashenning.forgeanalytics.proxies.ServerProxy")
2630
public static CommonProxy proxy;
2731

32+
public static Logger logger;
33+
2834
@EventHandler
2935
public void preInit(FMLPreInitializationEvent e) {
36+
// logger = e.getModLog();
3037
proxy.preInit(e);
3138
}
3239

@@ -65,5 +72,7 @@ public void serverStopped(FMLServerStoppedEvent e) {
6572
// TODO Auto-generated catch block
6673
e1.printStackTrace();
6774
}
75+
76+
ForgeAnalyticsSingleton.getInstance().CancelTimer();
6877
}
6978
}

0 commit comments

Comments
 (0)