Skip to content

Commit

Permalink
Add placeholder resolver API
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 20, 2024
1 parent c0ffb19 commit 635800a
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spark-api/src/main/java/me/lucko/spark/api/Spark.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -83,4 +84,12 @@ public interface Spark {
*/
@NonNull @Unmodifiable Map<String, GarbageCollector> gc();

/**
* Gets a placeholder resolver.
*
* @return a placeholder resolver
*/
@NonNull
PlaceholderResolver placeholders();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* This file is part of spark, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* 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);

}
17 changes: 17 additions & 0 deletions spark-common/src/main/java/me/lucko/spark/common/api/SparkApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
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;
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;

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 635800a

Please sign in to comment.