diff options
author | Luck <git@lucko.me> | 2022-06-11 21:05:08 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2022-06-11 21:08:29 +0100 |
commit | a551317a0acc0f6ccb2d1bb66e8475b42387e59c (patch) | |
tree | d59ecc067c27a77b0662d5813051cbbb8e3b39a5 /spark-fabric/src/main/java/me/lucko/spark | |
parent | 0ac8713eaaefe7336db2e0369bbe547dc6c0da7d (diff) | |
download | spark-a551317a0acc0f6ccb2d1bb66e8475b42387e59c.tar.gz spark-a551317a0acc0f6ccb2d1bb66e8475b42387e59c.tar.bz2 spark-a551317a0acc0f6ccb2d1bb66e8475b42387e59c.zip |
Tidy up placeholder handling
Co-authored-by: Caden Kriese <armrest.trimmer.00@icloud.com>
Diffstat (limited to 'spark-fabric/src/main/java/me/lucko/spark')
2 files changed, 36 insertions, 153 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 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 + } } } |