From 5368331441e052983ef2d03a5a10b70bbff6a822 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Sun, 24 Jul 2022 17:09:58 +0200 Subject: [PATCH] The API is working! --- pom.xml | 6 ++++++ .../the/gr8/playerstats/api/PlayerStats.java | 20 ++++++++++--------- .../gr8/playerstats/msg/MessageBuilder.java | 2 +- .../playerstats/statistic/RequestManager.java | 3 ++- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/pom.xml b/pom.xml index 6e78139..e7aa991 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,12 @@ 4.11.0 + + net.kyori + adventure-text-serializer-plain + 4.11.0 + + me.clip placeholderapi diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java index 8fef0a6..1693830 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/api/PlayerStats.java @@ -2,7 +2,7 @@ package com.gmail.artemis.the.gr8.playerstats.api; import com.gmail.artemis.the.gr8.playerstats.Main; import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; @@ -19,7 +19,9 @@ import org.jetbrains.annotations.NotNull;

The result of the methods in PlayerStats' API are returned in the form of a TextComponent, which can be sent directly to a Minecraft client with the Adventure library, - or turned into a String with {@link #statResultComponentToString(TextComponent)}.

*/ + or turned into a String with {@link #statResultComponentToString(TextComponent)}. + Don't use the Adventure method .content() on your result instead of the method specified in here - + because of the way the TextComponent is built by PlayerStats, you won't be able to get the full content that way.

*/ public interface PlayerStats { /** Returns an instance of the {@link PlayerStatsAPI}. @@ -29,6 +31,13 @@ public interface PlayerStats { return Main.getPlayerStatsAPI(); } + /** Turns a TextComponent into its String representation. If you don't want to work with + Adventure's TextComponents, you can call this method to turn any stat-result into a String. + It will lose all color and style, but it will keep line-breaks.*/ + default String statResultComponentToString(TextComponent statResult) { + return PlainTextComponentSerializer.plainText().serialize(statResult); + } + /** Get a formatted player-statistic of Statistic.Type UNTYPED.*/ TextComponent getPlayerStat(@NotNull Statistic statistic, @NotNull String playerName) throws IllegalArgumentException; @@ -55,11 +64,4 @@ public interface PlayerStats { /** Get a formatted top-statistic of Statistic.Type ENTITY. Not recommended to call this from the main Thread (see class description).*/ TextComponent getTopStats(@NotNull Statistic statistic, @NotNull EntityType entity) throws IllegalArgumentException; - - /** Turns a TextComponent into its String representation. If you don't want to work with - Adventure's TextComponents, you can call this method to turn any stat-result into a String. - It will lose all color and style, but it will keep line-breaks.*/ - default String statResultComponentToString(TextComponent statResult) { - return MiniMessage.miniMessage().serialize(statResult); - } } \ No newline at end of file diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java index 2c9a1c5..d6274f2 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/msg/MessageBuilder.java @@ -352,7 +352,7 @@ public class MessageBuilder { /** Depending on the config settings, return either a TranslatableComponent representing the statName (and potential subStatName), or a TextComponent with capitalized English names.*/ private TextComponent getStatNameComponent(StatRequest request) { - if (config.useTranslatableComponents()) { + if (config.useTranslatableComponents() && !request.isAPIRequest()) { String statKey = languageKeyHandler.getStatKey(request.getStatistic()); String subStatKey = request.getSubStatEntry(); if (subStatKey != null) { diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/RequestManager.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/RequestManager.java index f5e5972..f454d4c 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/RequestManager.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/RequestManager.java @@ -67,8 +67,9 @@ public class RequestManager implements RequestGenerator { return request; } + /** This method will generate a {@link StatRequest} for a stat-request arriving through the API.*/ public StatRequest generateRequest(@NotNull Target selection, @NotNull Statistic statistic, Material material, EntityType entity, String playerName) { - StatRequest request = new StatRequest(Bukkit.getConsoleSender()); + StatRequest request = new StatRequest(Bukkit.getConsoleSender(), true); request.setSelection(selection); request.setStatistic(statistic); switch (statistic.getType()) {