From fe4f5db12e2138a0c25ce8af5ee17b6006ea7ba0 Mon Sep 17 00:00:00 2001 From: Artemis-the-gr8 Date: Mon, 25 Jul 2022 17:33:16 +0200 Subject: [PATCH] Started experimenting with serializing my Components into String --- pom.xml | 6 ---- .../the/gr8/playerstats/api/PlayerStats.java | 12 +++++-- .../gr8/playerstats/commands/StatCommand.java | 35 ++++++++++++++----- .../gr8/playerstats/models/StatRequest.java | 6 +++- .../playerstats/statistic/RequestManager.java | 3 ++ .../gr8/playerstats/statistic/StatThread.java | 13 ++++++- 6 files changed, 56 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index e7aa991..6e78139 100644 --- a/pom.xml +++ b/pom.xml @@ -63,12 +63,6 @@ 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 14dda5e..024f1de 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,14 +2,22 @@ package com.gmail.artemis.the.gr8.playerstats.api; import com.gmail.artemis.the.gr8.playerstats.Main; import net.kyori.adventure.platform.bukkit.BukkitAudiences; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentIteratorType; import net.kyori.adventure.text.TextComponent; -import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer; +import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.renderer.TranslatableComponentRenderer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.Material; import org.bukkit.Statistic; import org.bukkit.entity.EntityType; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import java.util.Locale; + +import static net.kyori.adventure.translation.GlobalTranslator.renderer; + /** This is the outgoing API that you can use to access the core functionality of PlayerStats. To work with it, you need to call PlayerStats.{@link #getAPI()} to get an instance of {@link PlayerStatsAPI}. You can then use this object to access any of the further methods. @@ -42,7 +50,7 @@ public interface PlayerStats { Adventure's TextComponents, you can call this method to turn any stat-result into a String. @return a String representation of this TextComponent, without color and style, but with line-breaks*/ default String statResultComponentToString(TextComponent statResult) { - return PlainTextComponentSerializer.plainText().serialize(statResult); + return LegacyComponentSerializer.builder().hexColors().build().serialize(statResult); } /** Get a formatted player-statistic of Statistic.Type UNTYPED. diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java index 8d6b027..71e269e 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/commands/StatCommand.java @@ -4,16 +4,15 @@ import com.gmail.artemis.the.gr8.playerstats.ThreadManager; import com.gmail.artemis.the.gr8.playerstats.msg.OutputManager; import com.gmail.artemis.the.gr8.playerstats.statistic.RequestManager; import com.gmail.artemis.the.gr8.playerstats.models.StatRequest; -import net.md_5.bungee.api.ChatColor; -import net.md_5.bungee.api.chat.TextComponent; -import org.bukkit.Color; +import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; import org.jetbrains.annotations.NotNull; -import java.awt.*; - public class StatCommand implements CommandExecutor { @@ -38,10 +37,28 @@ public class StatCommand implements CommandExecutor { } else if (args[0].equalsIgnoreCase(">:(")) { java.awt.Color color = new java.awt.Color(178, 102, 255); - ChatColor one = ChatColor.of(color); - TextComponent msg = new TextComponent(">:((((("); - msg.setColor(one); - sender.spigot().sendMessage(msg); +// ChatColor one = ChatColor.of(color); +// TextComponent msg = new TextComponent(">:((((("); +// msg.setColor(one); +// sender.spigot().sendMessage(msg); +// sender.sendMessage("regular msg with ChatColor: " + one + ">:(((((("); +// sender.sendMessage("regular msg Component.toLegacyText: " + msg.toLegacyText()); +// sender.sendMessage("regular msg Component.toString: " + msg); + } + else if (args[0].equalsIgnoreCase(">:((")) { + Component msg = MiniMessage.miniMessage().deserialize("fire demon"); + String msgString = LegacyComponentSerializer.builder().hexColors().build().serialize(msg); + sender.sendMessage("LCS.hexColors(): " + msgString); + MyLogger.logMsg(msgString); + + String msgString2 = LegacyComponentSerializer.legacySection().serialize(msg); + sender.sendMessage("LCS.legacySection: " + msgString2); + MyLogger.logMsg(msgString2); + + //only this one works both in-game and in-console + String msgString3 = LegacyComponentSerializer.builder().hexColors().useUnusualXRepeatedCharacterHexFormat().build().serialize(msg); + sender.sendMessage("LCS.hexColors().spigotformat...: " + msgString3); + MyLogger.logMsg(msgString3); } else { StatRequest request = requestManager.generateRequest(sender, args); diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/models/StatRequest.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/models/StatRequest.java index a220a1f..588cf98 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/models/StatRequest.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/models/StatRequest.java @@ -15,7 +15,7 @@ import org.jetbrains.annotations.NotNull; public final class StatRequest { private final CommandSender sender; - private final boolean isAPIRequest; + private boolean isAPIRequest; private Statistic statistic; private String playerName; private Target selection; @@ -38,6 +38,10 @@ public final class StatRequest { playerFlag = false; } + public void setAPIRequest() { + this.isAPIRequest = true; + } + public boolean isAPIRequest() { return isAPIRequest; } 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 f454d4c..990ab42 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 @@ -62,6 +62,9 @@ public class RequestManager implements RequestGenerator { request.setPlayerName(arg); request.setSelection(Target.PLAYER); } + else if (arg.equalsIgnoreCase("api")) { + request.setAPIRequest(); + } } patchRequest(request); return request; diff --git a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java index 05c3292..65c4016 100644 --- a/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java +++ b/src/main/java/com/gmail/artemis/the/gr8/playerstats/statistic/StatThread.java @@ -8,6 +8,8 @@ import com.gmail.artemis.the.gr8.playerstats.reload.ReloadThread; import com.gmail.artemis.the.gr8.playerstats.ThreadManager; import com.gmail.artemis.the.gr8.playerstats.utils.MyLogger; import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; +import net.md_5.bungee.api.chat.BaseComponent; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -63,7 +65,16 @@ public class StatThread extends Thread { case TOP -> outputManager.formatTopStat(request, statManager.getTopStats(request)); case SERVER -> outputManager.formatServerStat(request, statManager.getServerStat(request)); }; - outputManager.sendToCommandSender(request.getCommandSender(), statResult); + if (request.isAPIRequest()) { + String msg = LegacyComponentSerializer.builder().hexColors().build().serialize(statResult); + request.getCommandSender().sendMessage(msg); + + String msg2 = LegacyComponentSerializer.builder().hexColors().useUnusualXRepeatedCharacterHexFormat().build().serialize(statResult); + request.getCommandSender().sendMessage(msg2); + } + else { + outputManager.sendToCommandSender(request.getCommandSender(), statResult); + } } catch (ConcurrentModificationException e) { if (!request.isConsoleSender()) {