aboutsummaryrefslogtreecommitdiff
path: root/spark-fabric/src
diff options
context:
space:
mode:
authorPotatoboy9999 <51728317+PotatoPresident@users.noreply.github.com>2021-10-10 02:00:14 -0700
committerGitHub <noreply@github.com>2021-10-10 10:00:14 +0100
commit8ebf2d39a1d95c409d2151374b8ff1ad9e776d97 (patch)
tree713805b1efe197c191d74b61d17ae94e945b1430 /spark-fabric/src
parent7760d4a9fee5b7e5f6ca9b84129e84a1c373131e (diff)
downloadspark-8ebf2d39a1d95c409d2151374b8ff1ad9e776d97.tar.gz
spark-8ebf2d39a1d95c409d2151374b8ff1ad9e776d97.tar.bz2
spark-8ebf2d39a1d95c409d2151374b8ff1ad9e776d97.zip
Add Fabric placeholder support (#136)
Diffstat (limited to 'spark-fabric/src')
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java166
-rw-r--r--spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java7
2 files changed, 173 insertions, 0 deletions
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java
new file mode 100644
index 0000000..359630d
--- /dev/null
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/placeholder/SparkFabricPlaceholderApi.java
@@ -0,0 +1,166 @@
+package me.lucko.spark.fabric.placeholder;
+
+import eu.pb4.placeholders.PlaceholderAPI;
+import eu.pb4.placeholders.PlaceholderResult;
+import me.lucko.spark.common.SparkPlatform;
+import me.lucko.spark.common.command.modules.HealthModule;
+import me.lucko.spark.common.monitor.cpu.CpuMonitor;
+import me.lucko.spark.common.monitor.tick.TickStatistics;
+import me.lucko.spark.common.util.RollingAverage;
+import net.kyori.adventure.text.Component;
+import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
+import net.minecraft.text.Text;
+import net.minecraft.util.Identifier;
+
+public class SparkFabricPlaceholderApi {
+ private final SparkPlatform platform;
+
+ public SparkFabricPlaceholderApi(SparkPlatform platform) {
+ this.platform = platform;
+
+ PlaceholderAPI.register(
+ new Identifier("spark", "tps"),
+ context -> {
+ TickStatistics tickStatistics = platform.getTickStatistics();
+ if (tickStatistics == null) {
+ return PlaceholderResult.invalid();
+ }
+
+ if (context.hasArgument()) {
+ Double tps = switch (context.getArgument()) {
+ case "5s":
+ yield tickStatistics.tps5Sec();
+ case "10s":
+ yield tickStatistics.tps10Sec();
+ case "1m":
+ yield tickStatistics.tps1Min();
+ case "5m":
+ yield tickStatistics.tps5Min();
+ case "15m":
+ yield tickStatistics.tps15Min();
+ default:
+ yield null;
+ };
+
+ if (tps == null) {
+ return PlaceholderResult.invalid("Invalid argument");
+ } else {
+ return PlaceholderResult.value(toText(HealthModule.formatTps(tps)));
+ }
+ } else {
+ return PlaceholderResult.value(toText(
+ Component.text()
+ .append(HealthModule.formatTps(tickStatistics.tps5Sec())).append(Component.text(", "))
+ .append(HealthModule.formatTps(tickStatistics.tps10Sec())).append(Component.text(", "))
+ .append(HealthModule.formatTps(tickStatistics.tps1Min())).append(Component.text(", "))
+ .append(HealthModule.formatTps(tickStatistics.tps5Min())).append(Component.text(", "))
+ .append(HealthModule.formatTps(tickStatistics.tps15Min()))
+ .build()
+ ));
+ }
+ }
+ );
+
+ PlaceholderAPI.register(
+ new Identifier("spark", "tickduration"),
+ context -> {
+ TickStatistics tickStatistics = platform.getTickStatistics();
+ if (tickStatistics == null || !tickStatistics.isDurationSupported()) {
+ return PlaceholderResult.invalid();
+ }
+
+ if (context.hasArgument()) {
+ RollingAverage duration = switch (context.getArgument()) {
+ case "10s":
+ yield tickStatistics.duration10Sec();
+ case "1m":
+ yield tickStatistics.duration1Min();
+ default:
+ yield null;
+ };
+
+ if (duration == null) {
+ return PlaceholderResult.invalid("Invalid argument");
+ } else {
+ return PlaceholderResult.value(toText(HealthModule.formatTickDurations(duration)));
+ }
+ } else {
+ return PlaceholderResult.value(toText(
+ Component.text()
+ .append(HealthModule.formatTickDurations(tickStatistics.duration10Sec())).append(Component.text("; "))
+ .append(HealthModule.formatTickDurations(tickStatistics.duration1Min()))
+ .build()
+ ));
+ }
+ }
+ );
+
+ PlaceholderAPI.register(
+ new Identifier("spark", "cpu_system"),
+ context -> {
+ if (context.hasArgument()) {
+ Double usage = switch (context.getArgument()) {
+ case "10s":
+ yield CpuMonitor.systemLoad10SecAvg();
+ case "1m":
+ yield CpuMonitor.systemLoad1MinAvg();
+ case "15m":
+ yield CpuMonitor.systemLoad15MinAvg();
+ default:
+ yield null;
+ };
+
+ if (usage == null) {
+ return PlaceholderResult.invalid("Invalid argument");
+ } else {
+ return PlaceholderResult.value(toText(HealthModule.formatCpuUsage(usage)));
+ }
+ } else {
+ return PlaceholderResult.value(toText(
+ Component.text()
+ .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())).append(Component.text(", "))
+ .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())).append(Component.text(", "))
+ .append(HealthModule.formatCpuUsage(CpuMonitor.systemLoad15MinAvg()))
+ .build()
+ ));
+ }
+ }
+ );
+
+ PlaceholderAPI.register(
+ new Identifier("spark", "cpu_process"),
+ context -> {
+ if (context.hasArgument()) {
+ Double usage = switch (context.getArgument()) {
+ case "10s":
+ yield CpuMonitor.processLoad10SecAvg();
+ case "1m":
+ yield CpuMonitor.processLoad1MinAvg();
+ case "15m":
+ yield CpuMonitor.processLoad15MinAvg();
+ default:
+ yield null;
+ };
+
+ if (usage == null) {
+ return PlaceholderResult.invalid("Invalid argument");
+ } else {
+ return PlaceholderResult.value(toText(HealthModule.formatCpuUsage(usage)));
+ }
+ } else {
+ return PlaceholderResult.value(toText(
+ Component.text()
+ .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad10SecAvg())).append(Component.text(", "))
+ .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad1MinAvg())).append(Component.text(", "))
+ .append(HealthModule.formatCpuUsage(CpuMonitor.processLoad15MinAvg()))
+ .build()
+ ));
+ }
+ }
+ );
+ }
+
+ private Text toText(Component component) {
+ return Text.Serializer.fromJson(GsonComponentSerializer.gson().serialize(component));
+ }
+}
diff --git a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
index f0b6574..8d38b1c 100644
--- a/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
+++ b/spark-fabric/src/main/java/me/lucko/spark/fabric/plugin/FabricServerSparkPlugin.java
@@ -37,8 +37,10 @@ import me.lucko.spark.fabric.FabricSparkMod;
import me.lucko.spark.fabric.FabricTickHook;
import me.lucko.spark.fabric.FabricTickReporter;
+import me.lucko.spark.fabric.placeholder.SparkFabricPlaceholderApi;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
+import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandOutput;
@@ -58,6 +60,11 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman
registerCommands(server.getCommandManager().getDispatcher(), plugin, plugin, "spark");
CommandRegistrationCallback.EVENT.register((dispatcher, isDedicated) -> registerCommands(dispatcher, plugin, plugin, "spark"));
+
+ if (FabricLoader.getInstance().isModLoaded("placeholder-api")) {
+ new SparkFabricPlaceholderApi(plugin.platform);
+ }
+
// register shutdown hook
ServerLifecycleEvents.SERVER_STOPPING.register(stoppingServer -> {
if (stoppingServer == plugin.server) {