|
| 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 | +} |
0 commit comments