diff --git a/spark-api/src/main/java/me/lucko/spark/api/Spark.java b/spark-api/src/main/java/me/lucko/spark/api/Spark.java index af71ab6b..a5f20d6b 100644 --- a/spark-api/src/main/java/me/lucko/spark/api/Spark.java +++ b/spark-api/src/main/java/me/lucko/spark/api/Spark.java @@ -26,6 +26,7 @@ package me.lucko.spark.api; import me.lucko.spark.api.gc.GarbageCollector; +import me.lucko.spark.api.placeholder.PlaceholderResolver; import me.lucko.spark.api.statistic.misc.DoubleAverageInfo; import me.lucko.spark.api.statistic.types.DoubleStatistic; import me.lucko.spark.api.statistic.types.GenericStatistic; @@ -83,4 +84,12 @@ public interface Spark { */ @NonNull @Unmodifiable Map gc(); + /** + * Gets a placeholder resolver. + * + * @return a placeholder resolver + */ + @NonNull + PlaceholderResolver placeholders(); + } diff --git a/spark-api/src/main/java/me/lucko/spark/api/placeholder/PlaceholderResolver.java b/spark-api/src/main/java/me/lucko/spark/api/placeholder/PlaceholderResolver.java new file mode 100644 index 00000000..20834cb7 --- /dev/null +++ b/spark-api/src/main/java/me/lucko/spark/api/placeholder/PlaceholderResolver.java @@ -0,0 +1,54 @@ +/* + * This file is part of spark, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.lucko.spark.api.placeholder; + +import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Resolves spark placeholders. + * + *

See spark docs for more info.

+ */ +public interface PlaceholderResolver { + + /** + * Resolves the given placeholder to a legacy formatted string. + * + * @param placeholder the placeholder to resolve + * @return the resolved placeholder + */ + @Nullable String resolveLegacyFormatting(@NonNull String placeholder); + + /** + * Resolves the given placeholder to a text component serialised to json. + * + * @param placeholder the placeholder to resolve + * @return the resolved placeholder + */ + @Nullable String resolveComponentJson(@NonNull String placeholder); + +} diff --git a/spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java b/spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java index 16cb2598..81b4f70f 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java +++ b/spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java @@ -24,6 +24,7 @@ import me.lucko.spark.api.Spark; import me.lucko.spark.api.SparkProvider; import me.lucko.spark.api.gc.GarbageCollector; +import me.lucko.spark.api.placeholder.PlaceholderResolver; import me.lucko.spark.api.statistic.misc.DoubleAverageInfo; import me.lucko.spark.api.statistic.types.DoubleStatistic; import me.lucko.spark.api.statistic.types.GenericStatistic; @@ -31,6 +32,7 @@ import me.lucko.spark.common.monitor.cpu.CpuMonitor; import me.lucko.spark.common.monitor.memory.GarbageCollectorStatistics; import me.lucko.spark.common.monitor.tick.TickStatistics; +import me.lucko.spark.common.util.SparkPlaceholder; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -172,6 +174,21 @@ public DoubleAverageInfo poll(@NonNull MillisPerTick window) { return ImmutableMap.copyOf(map); } + @Override + public @NonNull PlaceholderResolver placeholders() { + return new PlaceholderResolver() { + @Override + public @Nullable String resolveLegacyFormatting(@NonNull String placeholder) { + return SparkPlaceholder.resolveFormattingCode(SparkApi.this.platform, placeholder); + } + + @Override + public @Nullable String resolveComponentJson(@NonNull String placeholder) { + return SparkPlaceholder.resolveComponentJson(SparkApi.this.platform, placeholder); + } + }; + } + public static void register(Spark spark) { try { SINGLETON_SET_METHOD.invoke(null, spark); diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/SparkPlaceholder.java b/spark-common/src/main/java/me/lucko/spark/common/util/SparkPlaceholder.java index 948d404a..b4acc7be 100644 --- a/spark-common/src/main/java/me/lucko/spark/common/util/SparkPlaceholder.java +++ b/spark-common/src/main/java/me/lucko/spark/common/util/SparkPlaceholder.java @@ -25,6 +25,7 @@ import me.lucko.spark.common.monitor.tick.TickStatistics; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import java.util.Locale; @@ -186,5 +187,13 @@ public static String resolveFormattingCode(SparkPlatform platform, String placeh } return LegacyComponentSerializer.legacySection().serialize(result); } + + public static String resolveComponentJson(SparkPlatform platform, String placeholder) { + TextComponent result = resolveComponent(platform, placeholder); + if (result == null) { + return null; + } + return GsonComponentSerializer.gson().serialize(result); + } }