Skip to content

Commit c231e5c

Browse files
author
Tamas Henning
committed
Minor clean-up and removed AnalyticsCommands as we don't need them right now...
1 parent 86672e0 commit c231e5c

File tree

8 files changed

+195
-280
lines changed

8 files changed

+195
-280
lines changed

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

+60-63
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
import org.apache.http.impl.client.HttpClientBuilder;
1313

1414
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;
1717
import com.tamashenning.forgeanalytics.models.AnalyticsModel;
1818

1919
import net.minecraft.client.Minecraft;
@@ -23,55 +23,56 @@
2323
import net.minecraftforge.fml.common.ModContainer;
2424

2525
public class AnalyticsClient {
26-
26+
2727
public boolean UploadModel(AnalyticsModel model) throws Exception {
2828

2929
model.Properties.putAll(ForgeAnalyticsConstants.CustomProperties);
30-
30+
3131
Gson g = new Gson();
3232
String json = g.toJson(model);
3333

3434
return this.UploadModel(json);
3535
}
36-
36+
3737
private boolean UploadModel(String json) throws Exception {
38-
38+
3939
// Respect snooper settings...
40-
if(!Minecraft.getMinecraft().isSnooperEnabled()) {
40+
if (!Minecraft.getMinecraft().isSnooperEnabled()) {
4141
return false;
4242
}
43-
43+
4444
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+
6162
return true;
6263
}
63-
64+
6465
public AnalyticsModel CreateClientStartupPing() {
6566
AnalyticsModel am = new AnalyticsModel();
6667
am.Table = ForgeAnalyticsConstants.pingClientTable;
6768
am.Properties = new HashMap<String, String>();
6869
am.PartitionKey = ForgeAnalyticsConstants.pingClientStartCommand;
6970
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
7071
am.Properties.putAll(this.getCommonValues());
71-
72+
7273
return am;
7374
}
74-
75+
7576
public AnalyticsModel CreateServerStartupPing() {
7677
AnalyticsModel am = new AnalyticsModel();
7778
am.Table = ForgeAnalyticsConstants.pingServerTable;
@@ -80,9 +81,9 @@ public AnalyticsModel CreateServerStartupPing() {
8081
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
8182
am.Properties.putAll(this.getCommonValues());
8283
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
83-
84+
8485
MinecraftServer server = MinecraftServer.getServer();
85-
86+
8687
if (MinecraftServer.getServer().isDedicatedServer()) {
8788
// Running dedicated...
8889
try {
@@ -91,17 +92,16 @@ public AnalyticsModel CreateServerStartupPing() {
9192
// TODO Auto-generated catch block
9293
e.printStackTrace();
9394
}
94-
}
95-
else {
95+
} else {
9696
// Running internal...
9797
am.Properties.put("ServerHostHash", "localhost");
9898
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()));
100100
}
101101

102102
return am;
103103
}
104-
104+
105105
public AnalyticsModel CreateServerStoppedPing() {
106106
AnalyticsModel am = new AnalyticsModel();
107107
am.Table = ForgeAnalyticsConstants.pingServerTable;
@@ -110,9 +110,9 @@ public AnalyticsModel CreateServerStoppedPing() {
110110
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
111111
am.Properties.putAll(this.getCommonValues());
112112
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
113-
113+
114114
MinecraftServer server = MinecraftServer.getServer();
115-
115+
116116
if (MinecraftServer.getServer().isDedicatedServer()) {
117117
// Running dedicated...
118118
try {
@@ -121,17 +121,16 @@ public AnalyticsModel CreateServerStoppedPing() {
121121
// TODO Auto-generated catch block
122122
e.printStackTrace();
123123
}
124-
}
125-
else {
124+
} else {
126125
// Running internal...
127126
am.Properties.put("ServerHostHash", "localhost");
128127
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()));
130129
}
131130

132131
return am;
133132
}
134-
133+
135134
public AnalyticsModel CreateClientKeepAlivePing() {
136135
AnalyticsModel am = new AnalyticsModel();
137136
am.Table = ForgeAnalyticsConstants.pingClientTable;
@@ -142,7 +141,7 @@ public AnalyticsModel CreateClientKeepAlivePing() {
142141

143142
return am;
144143
}
145-
144+
146145
public AnalyticsModel CreateServerKeepAlivePing() {
147146
AnalyticsModel am = new AnalyticsModel();
148147
am.Table = ForgeAnalyticsConstants.pingServerTable;
@@ -151,9 +150,9 @@ public AnalyticsModel CreateServerKeepAlivePing() {
151150
am.ClientDateTimeEpoch = System.currentTimeMillis() / 1000L;
152151
am.Properties.putAll(this.getCommonValues());
153152
am.Properties.put("ServerDifficulty", MinecraftServer.getServer().getDifficulty().toString());
154-
153+
155154
MinecraftServer server = MinecraftServer.getServer();
156-
155+
157156
if (MinecraftServer.getServer().isDedicatedServer()) {
158157
// Running dedicated...
159158
try {
@@ -163,24 +162,23 @@ public AnalyticsModel CreateServerKeepAlivePing() {
163162
e.printStackTrace();
164163
}
165164
am.Properties.put("ConnectedUsers", Integer.toString(server.getCurrentPlayerCount()));
166-
}
167-
else {
165+
} else {
168166
// Running internal...
169167
am.Properties.put("ServerHostHash", "localhost");
170168
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()));
172170
}
173171

174172
return am;
175173
}
176174

177-
178175
private Map<String, String> getCommonValues() {
179176
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());
181179
String modListCount = Integer.toString(net.minecraftforge.fml.common.Loader.instance().getModList().size());
182180
String modList = "";
183-
181+
184182
commonValues.put("JavaVersion", System.getProperty("java.version"));
185183
commonValues.put("JavaMaxRAM", Long.toString(Runtime.getRuntime().maxMemory()));
186184
commonValues.put("JavaAllocatedRAM", Long.toString(Runtime.getRuntime().totalMemory()));
@@ -192,35 +190,34 @@ private Map<String, String> getCommonValues() {
192190
commonValues.put("ActiveModCount", activeModListCount);
193191
commonValues.put("ModCount", modListCount);
194192

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() + ";";
197195
}
198196

199197
commonValues.put("ModList", modList);
200198

201-
202199
return commonValues;
203200
}
204-
205-
201+
206202
public String Anonymize(String data) throws NoSuchAlgorithmException {
207203
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
208204
byte[] dataBytes = data.getBytes();
209-
for (int i=0; i< ForgeAnalyticsConstants.HASHCOUNT; i++) {
205+
for (int i = 0; i < ForgeAnalyticsConstants.HASHCOUNT; i++) {
210206
dataBytes = sha256.digest(dataBytes);
211207
}
212-
208+
213209
return this.bytesToHex(dataBytes);
214210
}
215-
211+
216212
final protected char[] hexArray = "0123456789abcdef".toCharArray();
213+
217214
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);
225222
}
226223
}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.tamashenning.forgeanalytics;
22

3-
import com.tamashenning.forgeanalytics.commands.AnalyticsCommands;
43
import com.tamashenning.forgeanalytics.proxies.CommonProxy;
54

65
import net.minecraftforge.fml.common.Mod;
@@ -19,64 +18,64 @@
1918
@Mod(modid = ForgeAnalyticsMod.MODID, name = ForgeAnalyticsMod.MODNAME, version = ForgeAnalyticsMod.VERSION)
2019
public class ForgeAnalyticsMod {
2120

22-
public static final String MODID = "forgeanalytics";
23-
public static final String MODNAME = "Forge Analytics";
24-
public static final String VERSION = "0.0.1";
25-
26-
@SidedProxy(clientSide="com.tamashenning.forgeanalytics.proxies.ClientProxy", serverSide="com.tamashenning.forgeanalytics.proxies.ServerProxy")
27-
public static CommonProxy proxy;
28-
29-
@EventHandler
30-
public void preInit(FMLPreInitializationEvent e) {
31-
proxy.preInit(e);
32-
}
21+
public static final String MODID = "forgeanalytics";
22+
public static final String MODNAME = "Forge Analytics";
23+
public static final String VERSION = "0.0.4";
3324

34-
@EventHandler
35-
public void init(FMLInitializationEvent e) {
36-
proxy.init(e);
37-
}
25+
@SidedProxy(clientSide = "com.tamashenning.forgeanalytics.proxies.ClientProxy", serverSide = "com.tamashenning.forgeanalytics.proxies.ServerProxy")
26+
public static CommonProxy proxy;
3827

39-
@EventHandler
40-
public void postInit(FMLPostInitializationEvent e) {
41-
proxy.postInit(e);
42-
}
43-
44-
@EventHandler
45-
public void serverLoad(FMLServerStartingEvent e) {
46-
e.registerServerCommand(new AnalyticsCommands());
47-
}
48-
49-
@EventHandler
50-
public void serverStarted(FMLServerStartedEvent e) {
51-
AnalyticsClient ac = new AnalyticsClient();
52-
try {
28+
@EventHandler
29+
public void preInit(FMLPreInitializationEvent e) {
30+
proxy.preInit(e);
31+
}
32+
33+
@EventHandler
34+
public void init(FMLInitializationEvent e) {
35+
proxy.init(e);
36+
}
37+
38+
@EventHandler
39+
public void postInit(FMLPostInitializationEvent e) {
40+
proxy.postInit(e);
41+
}
42+
43+
@EventHandler
44+
public void serverLoad(FMLServerStartingEvent e) {
45+
46+
}
47+
48+
@EventHandler
49+
public void serverStarted(FMLServerStartedEvent e) {
50+
AnalyticsClient ac = new AnalyticsClient();
51+
try {
5352
ac.UploadModel(ac.CreateServerStartupPing());
5453
} catch (Exception e1) {
5554
// TODO Auto-generated catch block
5655
e1.printStackTrace();
5756
}
58-
}
59-
60-
@EventHandler
61-
public void serverStopped(FMLServerStoppedEvent e) {
62-
AnalyticsClient ac = new AnalyticsClient();
63-
try {
57+
}
58+
59+
@EventHandler
60+
public void serverStopped(FMLServerStoppedEvent e) {
61+
AnalyticsClient ac = new AnalyticsClient();
62+
try {
6463
ac.UploadModel(ac.CreateServerStoppedPing());
6564
} catch (Exception e1) {
6665
// TODO Auto-generated catch block
6766
e1.printStackTrace();
6867
}
69-
}
70-
71-
@SideOnly(Side.CLIENT)
72-
@EventHandler
73-
public void fmlLoaded(FMLLoadCompleteEvent e){
74-
AnalyticsClient ac = new AnalyticsClient();
75-
try {
68+
}
69+
70+
@SideOnly(Side.CLIENT)
71+
@EventHandler
72+
public void fmlLoaded(FMLLoadCompleteEvent e) {
73+
AnalyticsClient ac = new AnalyticsClient();
74+
try {
7675
ac.UploadModel(ac.CreateClientStartupPing());
7776
} catch (Exception e1) {
7877
// TODO Auto-generated catch block
7978
e1.printStackTrace();
8079
}
81-
}
80+
}
8281
}

0 commit comments

Comments
 (0)