diff options
Diffstat (limited to 'spark-fabric')
3 files changed, 37 insertions, 154 deletions
diff --git a/spark-fabric/build.gradle b/spark-fabric/build.gradle index 9da8e01..31008bb 100644 --- a/spark-fabric/build.gradle +++ b/spark-fabric/build.gradle @@ -45,7 +45,7 @@ dependencies { include(modImplementation('me.lucko:fabric-permissions-api:0.1-SNAPSHOT')) - modImplementation('eu.pb4:placeholder-api:1.1.1+1.17.1') + modImplementation('eu.pb4:placeholder-api:2.0.0-beta.4+1.19') shade project(':spark-common') } 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 index dc2e7d9..69303e3 100644 --- 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 @@ -20,169 +20,48 @@ package me.lucko.spark.fabric.placeholder; -import eu.pb4.placeholders.PlaceholderAPI; -import eu.pb4.placeholders.PlaceholderResult; +import eu.pb4.placeholders.api.PlaceholderContext; +import eu.pb4.placeholders.api.PlaceholderHandler; +import eu.pb4.placeholders.api.PlaceholderResult; +import eu.pb4.placeholders.api.Placeholders; import me.lucko.spark.common.SparkPlatform; -import me.lucko.spark.common.monitor.cpu.CpuMonitor; -import me.lucko.spark.common.monitor.tick.TickStatistics; -import me.lucko.spark.common.util.RollingAverage; -import me.lucko.spark.common.util.StatisticFormatter; +import me.lucko.spark.common.util.SparkPlaceholder; 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; +import org.jetbrains.annotations.Nullable; - public SparkFabricPlaceholderApi(SparkPlatform platform) { - this.platform = platform; +public enum SparkFabricPlaceholderApi { + ; - 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(StatisticFormatter.formatTps(tps))); - } - } else { - return PlaceholderResult.value(toText( - Component.text() - .append(StatisticFormatter.formatTps(tickStatistics.tps5Sec())).append(Component.text(", ")) - .append(StatisticFormatter.formatTps(tickStatistics.tps10Sec())).append(Component.text(", ")) - .append(StatisticFormatter.formatTps(tickStatistics.tps1Min())).append(Component.text(", ")) - .append(StatisticFormatter.formatTps(tickStatistics.tps5Min())).append(Component.text(", ")) - .append(StatisticFormatter.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(StatisticFormatter.formatTickDurations(duration))); - } - } else { - return PlaceholderResult.value(toText( - Component.text() - .append(StatisticFormatter.formatTickDurations(tickStatistics.duration10Sec())).append(Component.text("; ")) - .append(StatisticFormatter.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(StatisticFormatter.formatCpuUsage(usage))); - } - } else { - return PlaceholderResult.value(toText( - Component.text() - .append(StatisticFormatter.formatCpuUsage(CpuMonitor.systemLoad10SecAvg())).append(Component.text(", ")) - .append(StatisticFormatter.formatCpuUsage(CpuMonitor.systemLoad1MinAvg())).append(Component.text(", ")) - .append(StatisticFormatter.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(StatisticFormatter.formatCpuUsage(usage))); - } - } else { - return PlaceholderResult.value(toText( - Component.text() - .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad10SecAvg())).append(Component.text(", ")) - .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad1MinAvg())).append(Component.text(", ")) - .append(StatisticFormatter.formatCpuUsage(CpuMonitor.processLoad15MinAvg())) - .build() - )); - } - } - ); + public static void register(SparkPlatform platform) { + for (SparkPlaceholder placeholder : SparkPlaceholder.values()) { + Placeholders.register( + new Identifier("spark", placeholder.getName()), + new Handler(platform, placeholder) + ); + } } - private Text toText(Component component) { - return Text.Serializer.fromJson(GsonComponentSerializer.gson().serialize(component)); + private record Handler(SparkPlatform platform, SparkPlaceholder placeholder) implements PlaceholderHandler { + @Override + public PlaceholderResult onPlaceholderRequest(PlaceholderContext context, @Nullable String argument) { + return toResult(this.placeholder.resolve(this.platform, argument)); + } + + private static PlaceholderResult toResult(Component component) { + return component == null + ? PlaceholderResult.invalid() + : PlaceholderResult.value(toText(component)); + } + + private static 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 428ac4c..3d1a0e7 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 @@ -74,7 +74,11 @@ public class FabricServerSparkPlugin extends FabricSparkPlugin implements Comman // placeholders if (FabricLoader.getInstance().isModLoaded("placeholder-api")) { - new SparkFabricPlaceholderApi(this.platform); + try { + SparkFabricPlaceholderApi.register(this.platform); + } catch (LinkageError e) { + // ignore + } } } |