Initial commit
This commit is contained in:
commit
760e926bec
8 changed files with 265 additions and 0 deletions
38
.gitignore
vendored
Normal file
38
.gitignore
vendored
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
3
.idea/.gitignore
vendored
Normal file
3
.idea/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
7
.idea/encodings.xml
Normal file
7
.idea/encodings.xml
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding">
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
|
||||||
|
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
|
||||||
|
</component>
|
||||||
|
</project>
|
14
.idea/misc.xml
Normal file
14
.idea/misc.xml
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ExternalStorageConfigurationManager" enabled="true" />
|
||||||
|
<component name="MavenProjectsManager">
|
||||||
|
<option name="originalFiles">
|
||||||
|
<list>
|
||||||
|
<option value="$PROJECT_DIR$/pom.xml" />
|
||||||
|
</list>
|
||||||
|
</option>
|
||||||
|
</component>
|
||||||
|
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="temurin-21" project-jdk-type="JavaSDK">
|
||||||
|
<output url="file://$PROJECT_DIR$/out" />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
.idea/vcs.xml
Normal file
6
.idea/vcs.xml
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
31
README.md
Normal file
31
README.md
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
### Instructions
|
||||||
|
In `pom.xml`:
|
||||||
|
```xml
|
||||||
|
<dependency>
|
||||||
|
<groupId>eu.m724</groupId>
|
||||||
|
<artifactId>mstats-spigot</artifactId>
|
||||||
|
</dependency>
|
||||||
|
```
|
||||||
|
|
||||||
|
In `plugin.yml`:
|
||||||
|
```yaml
|
||||||
|
libraries:
|
||||||
|
- eu.m724:mstats-spigot
|
||||||
|
```
|
||||||
|
|
||||||
|
In main class:
|
||||||
|
|
||||||
|
```java
|
||||||
|
import eu.m724.mstats.MStats;
|
||||||
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
|
||||||
|
public class MyPlugin extends JavaPlugin {
|
||||||
|
@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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
41
pom.xml
Normal file
41
pom.xml
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>eu.m724</groupId>
|
||||||
|
<artifactId>mstats-spigot</artifactId>
|
||||||
|
<version>0.1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>21</maven.compiler.source>
|
||||||
|
<maven.compiler.target>21</maven.compiler.target>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.spigot.version>1.21.1-R0.1-SNAPSHOT</project.spigot.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<repositories>
|
||||||
|
<repository>
|
||||||
|
<id>spigot-repo</id>
|
||||||
|
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
|
||||||
|
</repository>
|
||||||
|
<repository>
|
||||||
|
<id>m724-repo</id>
|
||||||
|
<url>https://git.m724.eu/api/packages/Minecon724/maven</url>
|
||||||
|
</repository>
|
||||||
|
</repositories>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.spigotmc</groupId>
|
||||||
|
<artifactId>spigot-api</artifactId>
|
||||||
|
<version>${project.spigot.version}</version>
|
||||||
|
<scope>provided</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<developerConnection>scm:git:git@git.m724.eu:Minecon724/mstats-spigot.git</developerConnection>
|
||||||
|
</scm>
|
||||||
|
</project>
|
125
src/main/java/eu/m724/mstats/MStats.java
Normal file
125
src/main/java/eu/m724/mstats/MStats.java
Normal file
|
@ -0,0 +1,125 @@
|
||||||
|
package eu.m724.mstats;
|
||||||
|
|
||||||
|
import com.google.gson.*;
|
||||||
|
import org.bukkit.plugin.Plugin;
|
||||||
|
import org.bukkit.scheduler.BukkitRunnable;
|
||||||
|
import org.bukkit.scheduler.BukkitTask;
|
||||||
|
|
||||||
|
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.logging.Logger;
|
||||||
|
|
||||||
|
public class MStats extends BukkitRunnable {
|
||||||
|
private static final int VERSION = 1;
|
||||||
|
|
||||||
|
private BukkitTask task;
|
||||||
|
private final Map<Plugin, Integer> registeredPlugins = new HashMap<>();
|
||||||
|
private final Plugin plugin;
|
||||||
|
private final Logger logger = Logger.getLogger("mStats");
|
||||||
|
private String token = "";
|
||||||
|
|
||||||
|
private static MStats INSTANCE;
|
||||||
|
|
||||||
|
public MStats(Plugin plugin) {
|
||||||
|
this.plugin = plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
// a scheduler starts running the first tick after everything was loaded
|
||||||
|
task = new BukkitRunnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
firstRun();
|
||||||
|
}
|
||||||
|
}.runTaskLater(plugin, 0);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
task.cancel();
|
||||||
|
task = this.runTaskTimerAsynchronously(plugin, 0, 72000);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO don't send this every time
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
var json = new JsonObject();
|
||||||
|
var pla = new JsonArray();
|
||||||
|
registeredPlugins.forEach((key, value) -> {
|
||||||
|
var o1 = new JsonObject();
|
||||||
|
o1.addProperty("id", value);
|
||||||
|
o1.addProperty("version", key.getDescription().getVersion());
|
||||||
|
pla.add(o1);
|
||||||
|
});
|
||||||
|
|
||||||
|
json.add("plugins", pla);
|
||||||
|
json.addProperty("serverVersion", plugin.getServer().getVersion());
|
||||||
|
json.addProperty("statsVersion", 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); }
|
||||||
|
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resp.statusCode() != 200) {
|
||||||
|
logger.severe("Unable to contact metrics. It's not known why. " + resp.statusCode());
|
||||||
|
logger.severe(resp.body());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.has("token")) {
|
||||||
|
this.token = response.get("token").getAsString();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (response.has("version")) {
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue