diff --git a/README.md b/README.md
index 676a2d9..1c0e434 100644
--- a/README.md
+++ b/README.md
@@ -4,28 +4,26 @@ In `pom.xml`:
eu.m724
mstats-spigot
+ 0.1.0
```
In `plugin.yml`:
```yaml
libraries:
- - eu.m724:mstats-spigot
+ - eu.m724:mstats-spigot:0.1.0
```
In main class:
```java
-import eu.m724.mstats.MStats;
-import org.bukkit.plugin.java.JavaPlugin;
+import eu.m724.mstats.MStatsPlugin;
-public class MyPlugin extends JavaPlugin {
+public class MyPlugin extends MStatsPlugin {
@Override
public void onEnable() {
- // it's recommended that this is the last line of onEnable
-
- int mStatsId = 1; // replace this of course with your plugin ID
- MStats.init(this, mStatsId);
+ // It's recommended that this is the last line of onEnable
+ mStats(1); // Replace 1 of course with your plugin ID
}
}
```
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 751ac12..c1a3037 100644
--- a/pom.xml
+++ b/pom.xml
@@ -8,6 +8,25 @@
mstats-spigot
0.1.0-SNAPSHOT
+
+
+ m724
+ Minecon724
+
+
+
+
+
+ GPL-3.0-or-later
+ https://www.gnu.org/licenses/gpl-3.0.en.html
+ repo
+ The GNU General Public License is a free, copyleft license for software and other kinds of works.
+
+
+
+ https://git.m724.eu/Minecon724/mstats-spigot
+ mStats client for Spigot
+
21
21
@@ -35,7 +54,72 @@
+
+
+
+ org.sonatype.central
+ central-publishing-maven-plugin
+ 0.6.0
+ true
+
+ central
+
+
+
+
+ org.apache.maven.plugins
+ maven-source-plugin
+ 3.3.1
+
+
+ attach-sources
+
+ jar-no-fork
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-javadoc-plugin
+ 3.11.1
+
+
+ attach-javadocs
+
+ jar
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-gpg-plugin
+ 3.2.7
+
+
+ sign-artifacts
+ verify
+
+ sign
+
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-release-plugin
+ 3.1.1
+
+
+
+
+ scm:git:git@git.m724.eu:Minecon724/mstats-spigot.git
scm:git:git@git.m724.eu:Minecon724/mstats-spigot.git
+ https://git.m724.eu/Minecon724/mstats-spigot
\ No newline at end of file
diff --git a/src/main/java/eu/m724/mstats/MStats.java b/src/main/java/eu/m724/mstats/MStats.java
index dd24c71..e70ff1e 100644
--- a/src/main/java/eu/m724/mstats/MStats.java
+++ b/src/main/java/eu/m724/mstats/MStats.java
@@ -1,76 +1,88 @@
package eu.m724.mstats;
-import com.google.gson.*;
+import com.google.gson.Gson;
+import com.google.gson.JsonArray;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitRunnable;
-import org.bukkit.scheduler.BukkitTask;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.net.URI;
-import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
-import java.util.HashMap;
-import java.util.Map;
+import java.util.*;
import java.util.logging.Logger;
+import java.util.stream.Collectors;
public class MStats extends BukkitRunnable {
- private static final int VERSION = 1;
+ static final int PROT_VERSION = 1;
- private BukkitTask task;
- private final Map registeredPlugins = new HashMap<>();
private final Plugin plugin;
+ private final URI server;
+
+ private final Map registeredPlugins = new HashMap<>();
private final Logger logger = Logger.getLogger("mStats");
private String token = "";
- private static MStats INSTANCE;
-
- public MStats(Plugin plugin) {
+ public MStats(MStatsPlugin plugin) {
this.plugin = plugin;
+ this.server = plugin.getMStatsServer();
}
- public static void init(Plugin plugin, int id) {
- if (INSTANCE != null && INSTANCE.plugin.isEnabled()) {
- INSTANCE.registeredPlugins.put(plugin, id);
- return;
- }
-
- var mStats = new MStats(plugin);
- mStats.registeredPlugins.put(plugin, id);
- mStats.init();
- }
-
- private void init() {
- if (INSTANCE != null) {
- logger.info("Plugin crashed. Restarting mStats.");
- task.cancel();
- // some plugins may have registered before the one that crashed
- this.registeredPlugins.putAll(INSTANCE.registeredPlugins);
- }
-
- INSTANCE = this;
-
+ void init() {
// a scheduler starts running the first tick after everything was loaded
- task = new BukkitRunnable() {
+ new BukkitRunnable() {
@Override
public void run() {
firstRun();
}
}.runTaskLater(plugin, 0);
- logger.info("mStats running as " + plugin.getName());
+ //logger.info("mStats running as " + plugin.getName());
}
private void firstRun() {
- for (Plugin plugin : this.registeredPlugins.keySet()) {
- if (!plugin.isEnabled()) {
- // remove broken plugins
- this.registeredPlugins.remove(plugin);
+ for (Plugin plugin : plugin.getServer().getPluginManager().getPlugins()) {
+ //System.out.println(plugin.getName());
+ if (!plugin.isEnabled()) continue;
+ //System.out.println("a");
+ try {
+ Method method = plugin.getClass().getMethod("getMStatsId");
+ int id = (int) method.invoke(plugin);
+
+ method = plugin.getClass().getMethod("getMStatsServer");
+ URI server = (URI) method.invoke(plugin);
+
+ if (id > 0 && server.equals(this.server))
+ registeredPlugins.put(plugin, id);
+ } catch (NoSuchMethodException e) {
+ // plugin is not a mstats plugin
+ } catch (InvocationTargetException | IllegalAccessException e) {
+ // this should never happen
+ throw new RuntimeException(e);
}
}
- task.cancel();
- task = this.runTaskTimerAsynchronously(plugin, 0, 72000);
+ String first = registeredPlugins.keySet().stream().map(Plugin::getName).sorted().findFirst().get();
+
+ if (!plugin.getName().equals(first)) {
+ //logger.info("I pass control to " + first);
+ return;
+ }
+
+ // basically "PluginName #21, AnotherPlugin* #32" - (plugin name) + (* if stats runs on the plugin) + (# plugin id) joined with ", "
+ logger.info(server.getHost() + " " +
+ registeredPlugins.entrySet().stream().map(
+ e -> e.getKey().getName()
+ + (first.equals(e.getKey().getName()) ? "*" : "")
+ + " #" + e.getValue())
+ .collect(Collectors.joining(", ")
+ )
+ );
+ this.runTaskTimerAsynchronously(plugin, 0, 72000);
}
// TODO don't send this every time
@@ -78,37 +90,34 @@ public class MStats extends BukkitRunnable {
public void run() {
var json = new JsonObject();
var pla = new JsonArray();
- registeredPlugins.forEach((key, value) -> {
+ registeredPlugins.forEach((p, id) -> {
var o1 = new JsonObject();
- o1.addProperty("id", value);
- o1.addProperty("version", key.getDescription().getVersion());
+ o1.addProperty("id", id);
+ o1.addProperty("version", p.getDescription().getVersion());
pla.add(o1);
});
json.add("plugins", pla);
json.addProperty("serverVersion", plugin.getServer().getVersion());
- json.addProperty("statsVersion", VERSION);
+ json.addProperty("statsVersion", PROT_VERSION);
- HttpRequest request;
- try {
- request = HttpRequest
- .newBuilder(new URI("http://localhost:8080/api/server/heartbeat"))
- .POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(json)))
- .header("X-Server-Token", token)
- .build();
- } catch (URISyntaxException e) { throw new RuntimeException(e); }
+ HttpRequest request = HttpRequest
+ .newBuilder(server)
+ .POST(HttpRequest.BodyPublishers.ofString(new Gson().toJson(json)))
+ .header("X-Server-Token", token)
+ .build();
try (HttpClient httpClient = HttpClient.newBuilder().followRedirects(HttpClient.Redirect.NEVER).build()) {
httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenAccept(resp -> {
JsonObject response = JsonParser.parseString(resp.body()).getAsJsonObject();
if (response.has("message")) {
- logger.info("Message from metrics server: " + response.get("message").getAsString());
+ logger.info("Message from mStats server: " + response.get("message").getAsString());
}
if (resp.statusCode() != 200) {
- logger.severe("Unable to contact metrics. It's not known why. " + resp.statusCode());
- logger.severe(resp.body());
+ logger.severe("Unable to contact mStats. It's not known why. " + resp.statusCode());
+ //logger.severe(resp.body());
return;
}
@@ -119,6 +128,9 @@ public class MStats extends BukkitRunnable {
if (response.has("version")) {
// TODO
}
+ }).exceptionally(e -> {
+ logger.severe("Unable to contact mStats. Error. " + e.getMessage());
+ return null;
});
}
}
diff --git a/src/main/java/eu/m724/mstats/MStatsPlugin.java b/src/main/java/eu/m724/mstats/MStatsPlugin.java
new file mode 100644
index 0000000..7f21c20
--- /dev/null
+++ b/src/main/java/eu/m724/mstats/MStatsPlugin.java
@@ -0,0 +1,38 @@
+package eu.m724.mstats;
+
+import org.bukkit.plugin.java.JavaPlugin;
+
+import java.net.URI;
+
+public class MStatsPlugin extends JavaPlugin {
+ private int id = -1;
+ private URI server = URI.create("https://mstats.m724.eu/api/server/heartbeat");
+
+ /**
+ * Enable mStats for this plugin
+ * If you don't call this, mStats will not be enabled
+ *
+ * @param id the mStats plugin ID
+ */
+ public void mStats(int id) {
+ this.id = id;
+ new MStats(this).init();
+ }
+
+ /**
+ * Set the mStats backend server
+ *
+ * @param server the server URL
+ */
+ public void mStatsServer(String server) {
+ this.server = URI.create(server + "/api/server/heartbeat");
+ }
+
+ public int getMStatsId() {
+ return id;
+ }
+
+ public URI getMStatsServer() {
+ return server;
+ }
+}