Upload Plugin
This commit is contained in:
76
src/main/java/org/lunamc/uptimemonitor/UptimeMonitor.java
Normal file
76
src/main/java/org/lunamc/uptimemonitor/UptimeMonitor.java
Normal file
@@ -0,0 +1,76 @@
|
||||
package org.lunamc.uptimemonitor;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import javax.lang.model.element.UnknownElementException;
|
||||
import java.net.URI;
|
||||
import java.net.URLEncoder;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public final class UptimeMonitor extends JavaPlugin {
|
||||
FileConfiguration config = getConfig();
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
config.addDefault("uptime-url", "https://status.example.com/api/push/ABCDEFGHIJ");
|
||||
config.addDefault("ping", "player-count");
|
||||
config.options().copyDefaults(true);
|
||||
saveConfig();
|
||||
|
||||
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
getHttpRequest(pingResponse());
|
||||
}
|
||||
}, 0L, 300L);
|
||||
|
||||
getLogger().info(ChatColor.GREEN + "[Uptime Monitor]: successfully enabled!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
getLogger().info(ChatColor.RED + "[Uptime Monitor]: successfully disabled");
|
||||
}
|
||||
|
||||
private void getHttpRequest(String message, String ping) {
|
||||
String encodedMessage = URLEncoder.encode(message, StandardCharsets.UTF_8);
|
||||
String encodedPing = URLEncoder.encode(ping, StandardCharsets.UTF_8);
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.GET()
|
||||
.uri(URI.create(config.getString("uptime-url") + "?msg=" + encodedMessage + "&ping=" + encodedPing))
|
||||
.build();
|
||||
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
|
||||
.join();
|
||||
}
|
||||
|
||||
private String pingResponse() {
|
||||
String pingType = config.getString("ping");
|
||||
if (pingType.equalsIgnoreCase("player-count")) {
|
||||
return String.valueOf(getServer().getOnlinePlayers().size());
|
||||
} else if (pingType.equalsIgnoreCase("port")) {
|
||||
return String.valueOf(getServer().getPort());
|
||||
} else if (pingType.equalsIgnoreCase("tps")) {
|
||||
double[] tps = getServer().getTPS();
|
||||
return String.valueOf(tps[0]);
|
||||
} else if (pingType.equalsIgnoreCase("tick-time")) {
|
||||
long[] tickTime = getServer().getTickTimes();
|
||||
return String.valueOf(tickTime[0] / 1000 / 1000); // Nano / micro / milli
|
||||
} else {
|
||||
return "0";
|
||||
}
|
||||
}
|
||||
|
||||
private String messageResponse() {
|
||||
String messageType = config.getString("ping");
|
||||
if (messageType.equalsIgnoreCase("player-count")) {
|
||||
return "Player count"
|
||||
}
|
||||
}
|
||||
}
|
||||
2
src/main/resources/config.yml
Normal file
2
src/main/resources/config.yml
Normal file
@@ -0,0 +1,2 @@
|
||||
uptime-url: "https://status.example.com/api/push/ABCDEFGHIJ"
|
||||
ping: "player-count"
|
||||
7
src/main/resources/plugin.yml
Normal file
7
src/main/resources/plugin.yml
Normal file
@@ -0,0 +1,7 @@
|
||||
name: UptimeMonitor
|
||||
version: '${version}'
|
||||
main: org.lunamc.uptimemonitor.UptimeMonitor
|
||||
api-version: 1.18
|
||||
authors: [ SteveO718 ]
|
||||
description: Hooks into uptime-kuma instance and relays online players and uptime status
|
||||
website: https://lunamc.org
|
||||
Reference in New Issue
Block a user