aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuck <git@lucko.me>2024-07-20 20:58:36 +0100
committerLuck <git@lucko.me>2024-07-20 20:58:36 +0100
commit635800af986351f08dba33aa2e8ec5dd691bbffb (patch)
treefe5512d62e0c45df64a11ac79347010534d90bc4
parentc0ffb19c40b2397d099b960aa0545dafd4b2525d (diff)
downloadspark-635800af986351f08dba33aa2e8ec5dd691bbffb.tar.gz
spark-635800af986351f08dba33aa2e8ec5dd691bbffb.tar.bz2
spark-635800af986351f08dba33aa2e8ec5dd691bbffb.zip
Add placeholder resolver API
-rw-r--r--spark-api/src/main/java/me/lucko/spark/api/Spark.java9
-rw-r--r--spark-api/src/main/java/me/lucko/spark/api/placeholder/PlaceholderResolver.java54
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java17
-rw-r--r--spark-common/src/main/java/me/lucko/spark/common/util/SparkPlaceholder.java9
4 files changed, 89 insertions, 0 deletions
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 af71ab6..a5f20d6 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<String, GarbageCollector> 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 0000000..20834cb
--- /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) <luck@lucko.me>
+ * 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.
+ *
+ * <p>See <a href="https://spark.lucko.me/docs/misc/Placeholders">spark docs</a> for more info.</p>
+ */
+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 16cb259..81b4f70 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 com.google.common.collect.ImmutableMap;
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.SparkPlatform;
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 class SparkApi implements Spark {
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 948d404..b4acc7b 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.cpu.CpuMonitor;
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 enum SparkPlaceholder {
}
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);
+ }
}