Skip to content

Commit 32157ff

Browse files
committed
v0.1.4
1 parent 4312a5c commit 32157ff

5 files changed

Lines changed: 139 additions & 5 deletions

File tree

changelog.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
v0.1.3
1+
v0.1.4
22

3-
- Fixes issue where anything not h2 DB wise will error out.
4-
- Updates Hytale Custom Asset loader.
3+
- Implemented HStats support.

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod_credits = AzureDoom
2626
mod_url = https://modtale.net/mod/classescore
2727
mod_issues = https://github.com/AzureDoom/ClassesCore/issues
2828
mod_sources = https://github.com/AzureDoom/ClassesCore/
29-
version = 0.1.3
29+
version = 0.1.4
3030
includes_pack = true
3131
disabled_by_default = false
3232
patchline = release

src/main/java/com/azuredoom/classescore/ClassesCore.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.azuredoom.classescore.bootstrap.ClassesBootstrap;
1919
import com.azuredoom.classescore.command.ClassSelectionCommand;
2020
import com.azuredoom.classescore.compat.DynamicTooltipsLibCompat;
21+
import com.azuredoom.classescore.compat.HStats;
2122
import com.azuredoom.classescore.compat.placeholderapi.PlaceholderAPICompat;
2223
import com.azuredoom.classescore.config.ClassesCoreConfig;
2324
import com.azuredoom.classescore.data.ClassDefinition;
@@ -126,6 +127,7 @@ protected void setup() {
126127
.map(ClassDefinition::id)
127128
.toList()
128129
);
130+
new HStats("213bd24b-c5bb-4d50-a897-eb8bf7956a5e", "0.1.4", LOGGER);
129131
}
130132

131133
@Override
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package com.azuredoom.classescore.compat;
2+
3+
import com.hypixel.hytale.logger.HytaleLogger;
4+
import com.hypixel.hytale.server.core.HytaleServer;
5+
import com.hypixel.hytale.server.core.universe.Universe;
6+
7+
import java.io.IOException;
8+
import java.net.HttpURLConnection;
9+
import java.net.URI;
10+
import java.net.URLEncoder;
11+
import java.nio.charset.StandardCharsets;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
14+
import java.util.HashMap;
15+
import java.util.Map;
16+
import java.util.StringJoiner;
17+
import java.util.UUID;
18+
import java.util.concurrent.TimeUnit;
19+
20+
public class HStats {
21+
22+
private final String URL_BASE = "https://api.hstats.dev/api/";
23+
24+
private final String modUUID;
25+
26+
private final String modVersion;
27+
28+
private final String serverUUID;
29+
30+
private final HytaleLogger logger;
31+
32+
public HStats(String modUUID, String modVersion, HytaleLogger logger) {
33+
this.modUUID = modUUID;
34+
this.modVersion = modVersion;
35+
this.logger = logger;
36+
37+
this.serverUUID = getServerUUID();
38+
if (this.serverUUID == null) {
39+
System.out.println("[HStats] Metrics are disabled on this server.");
40+
return;
41+
}
42+
43+
logMetrics();
44+
addModToServer();
45+
HytaleServer.SCHEDULED_EXECUTOR.scheduleAtFixedRate(this::logMetrics, 5, 5, TimeUnit.MINUTES);
46+
}
47+
48+
private void logMetrics() {
49+
Map<String, String> arguments = new HashMap<>();
50+
arguments.put("server_uuid", this.serverUUID);
51+
arguments.put("players_online", String.valueOf(getOnlinePlayerCount()));
52+
arguments.put("os_name", System.getProperty("os.name"));
53+
arguments.put("os_version", System.getProperty("os.version"));
54+
arguments.put("java_version", System.getProperty("java.version"));
55+
arguments.put("cores", String.valueOf(Runtime.getRuntime().availableProcessors()));
56+
57+
sendRequest(URL_BASE + "server/update-server", arguments);
58+
}
59+
60+
private void addModToServer() {
61+
logger.atInfo().log("[HStats] Adding mod to server.");
62+
63+
Map<String, String> arguments = new HashMap<>();
64+
arguments.put("server_uuid", this.serverUUID);
65+
arguments.put("plugin_uuid", this.modUUID);
66+
arguments.put("plugin_version", this.modVersion);
67+
68+
sendRequest(URL_BASE + "server/add-plugin", arguments);
69+
}
70+
71+
private String getServerUUID() {
72+
var serverUUIDFile = Paths.get("hstats-server-uuid.txt");
73+
try {
74+
if (Files.exists(serverUUIDFile)) {
75+
var content = Files.readString(serverUUIDFile);
76+
content = content.trim();
77+
var lines = content.split("\n");
78+
if (lines.length < 5)
79+
return null;
80+
var enabled = lines[3].split("=")[1].trim();
81+
if (!enabled.equalsIgnoreCase("true"))
82+
return null;
83+
return lines[4];
84+
} else {
85+
var uuid = UUID.randomUUID().toString();
86+
Files.writeString(
87+
serverUUIDFile,
88+
"HStats - Hytale Mod Metrics (hstats.dev)\nHStats is a simple metrics system for Hytale mods. This file is here because one of your mods/plugins uses it, please do not modify the UUID. HStats will apply little to no effect on your server and analytics are anonymous, however you can still disable it.\n\nenabled=true\n"
89+
+ uuid
90+
);
91+
return uuid;
92+
}
93+
} catch (IOException e) {
94+
return null;
95+
}
96+
}
97+
98+
private void sendRequest(String urlString, Map<String, String> arguments) {
99+
try {
100+
var url = URI.create(urlString).toURL();
101+
var http = (HttpURLConnection) url.openConnection();
102+
103+
http.setRequestMethod("POST");
104+
http.setDoOutput(true);
105+
http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
106+
107+
var sj = new StringJoiner("&");
108+
for (var entry : arguments.entrySet()) {
109+
sj.add(
110+
URLEncoder.encode(entry.getKey(), StandardCharsets.UTF_8)
111+
+ "=" +
112+
URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8)
113+
);
114+
}
115+
116+
var out = sj.toString().getBytes(StandardCharsets.UTF_8);
117+
http.setFixedLengthStreamingMode(out.length);
118+
119+
try (var os = http.getOutputStream()) {
120+
os.write(out);
121+
}
122+
123+
http.disconnect();
124+
} catch (Exception e) {
125+
// pass
126+
}
127+
}
128+
129+
private int getOnlinePlayerCount() {
130+
return Universe.get().getPlayerCount();
131+
}
132+
133+
}

src/main/resources/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Group": "com.azuredoom",
33
"Name": "classescore",
4-
"Version": "0.1.3",
4+
"Version": "0.1.4",
55
"Description": "Classes Module for the LevelingCore mod",
66
"Authors": [
77
{

0 commit comments

Comments
 (0)