From 4e895b7361a4d8408266a78e1d6f064a7b8ff5f7 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Thu, 20 Feb 2020 15:23:58 -0800 Subject: [PATCH] Some kyori/text stuff --- .../commands/admin/NBTToolsCommand.java | 29 ++++- .../gmail/nossr50/locale/LocaleManager.java | 2 +- .../main/java/com/gmail/nossr50/mcMMO.java | 1 + .../com/gmail/nossr50/text/TextManager.java | 106 ++++++++++++++++++ 4 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 mcmmo-core/src/main/java/com/gmail/nossr50/text/TextManager.java diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/commands/admin/NBTToolsCommand.java b/mcmmo-core/src/main/java/com/gmail/nossr50/commands/admin/NBTToolsCommand.java index 1b16e9e2e..5db9e8d1e 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/commands/admin/NBTToolsCommand.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/commands/admin/NBTToolsCommand.java @@ -3,6 +3,11 @@ package com.gmail.nossr50.commands.admin; import co.aikar.commands.BaseCommand; import co.aikar.commands.annotation.*; import com.gmail.nossr50.mcMMO; +import com.sk89q.worldedit.bukkit.BukkitAdapter; +import net.kyori.text.TextComponent; +import net.kyori.text.adapter.bukkit.TextAdapter; +import net.kyori.text.format.TextColor; +import net.kyori.text.serializer.gson.GsonComponentSerializer; import org.bukkit.ChatColor; import org.bukkit.entity.Player; @@ -15,15 +20,33 @@ public class NBTToolsCommand extends BaseCommand { private mcMMO plugin; @Default + @CommandPermission("mcmmo.commands.nbttools") public void onCommand(Player player) { + //TODO: Add some help messages player.sendMessage("hi"); } /** * Show the NBT tags of an item in hand */ - @Subcommand("showtags") + @Subcommand("tags show") public void onShowTags(Player player) { + final TextComponent textComponent = TextComponent.builder() + .content(plugin.getLocaleManager().getString("mcMMO.Template.Prefix")) + .append("NBT Tools") + .color(TextColor.GOLD) + .append(" - ") + .append("Showing NBT Tags (") + .append(player.getInventory().getItemInMainHand().getType().getKey().toString()) + .color(TextColor.GREEN) + .append(")") + .color(TextColor.GOLD) + .append(TextComponent.newline()) + .build(); + + String json = GsonComponentSerializer.INSTANCE.serialize(textComponent); + TextAdapter.sendMessage(player, textComponent); + //Show NBT tags to player player.sendMessage(STYLE_TEXT_1 + " NBT TOOLS " + STYLE_TEXT_1); player.sendMessage("NBT Analysis: " + player.getInventory().getItemInMainHand().getType().getKey().toString()); @@ -32,12 +55,12 @@ public class NBTToolsCommand extends BaseCommand { player.sendMessage(ChatColor.GRAY + "NBT Analysis completed!"); } - @Subcommand("add") + @Subcommand("tags add") public void onAddTags(Player player) { } - @Subcommand("remove") + @Subcommand("tags remove") public void onRemoveTags(Player player) { } diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/locale/LocaleManager.java b/mcmmo-core/src/main/java/com/gmail/nossr50/locale/LocaleManager.java index 6b93cc074..aeec612ce 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/locale/LocaleManager.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/locale/LocaleManager.java @@ -124,7 +124,7 @@ public final class LocaleManager { } } - private static String addColors(String input) { + public static String addColors(String input) { input = input.replaceAll("\\Q[[BLACK]]\\E", ChatColor.BLACK.toString()); input = input.replaceAll("\\Q[[DARK_BLUE]]\\E", ChatColor.DARK_BLUE.toString()); input = input.replaceAll("\\Q[[DARK_GREEN]]\\E", ChatColor.DARK_GREEN.toString()); diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java b/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java index 64f0d801f..e03febf7b 100644 --- a/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/mcMMO.java @@ -47,6 +47,7 @@ import com.gmail.nossr50.util.skills.SkillTools; import com.gmail.nossr50.util.sounds.SoundManager; import com.gmail.nossr50.worldguard.WorldGuardManager; import com.gmail.nossr50.worldguard.WorldGuardUtils; +import net.kyori.text.adapter.bukkit.TextAdapter; import net.shatteredlands.shatt.backup.ZipLibrary; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; diff --git a/mcmmo-core/src/main/java/com/gmail/nossr50/text/TextManager.java b/mcmmo-core/src/main/java/com/gmail/nossr50/text/TextManager.java new file mode 100644 index 000000000..cc51662e3 --- /dev/null +++ b/mcmmo-core/src/main/java/com/gmail/nossr50/text/TextManager.java @@ -0,0 +1,106 @@ +package com.gmail.nossr50.text; + +import com.gmail.nossr50.mcMMO; +import net.kyori.text.TextComponent; +import net.kyori.text.adapter.bukkit.TextAdapter; +import net.kyori.text.serializer.gson.GsonComponentSerializer; +import org.bukkit.ChatColor; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +/** + * Handles some boiler plate related to kyori powered text library + */ +public class TextManager { + public static final char COLOR_CHAR = 'ยง'; + private mcMMO pluginRef; + + public TextManager(mcMMO pluginRef) { + this.pluginRef = pluginRef; + } + + /** + * Send a message to multiple recipients + * @param commandSenders target recipients + * @param textComponent the {@link TextComponent} to send + */ + public void sendMessage(List commandSenders, TextComponent textComponent) { + for(CommandSender commandSender : commandSenders) { + sendMessage(commandSender, textComponent); + } + } + + /** + * Serializes and sends a text message to a specific recipient + * @param commandSender target recipient + * @param textComponent the {@link TextComponent} to serialize and send + */ + public void sendMessage(CommandSender commandSender, TextComponent textComponent) { + String json = GsonComponentSerializer.INSTANCE.serialize(textComponent); + TextAdapter.sendMessage(commandSender, textComponent); + } + + /** + * Sends a message to a single recipient with the (mcMMO) watermark at the beginning of the message + * @param commandSender target recipient + * @param textComponent the {@link TextComponent} to watermark and send + */ + public void sendMessageWatermarked(CommandSender commandSender, TextComponent textComponent) { + TextComponent waterMarkedComponent = buildWaterMarked(textComponent); + + sendMessage(commandSender, waterMarkedComponent); + } + + /** + * Sends a message to a list of recipients with the (mcMMO) watermark at the beginning of the message + * @param commandSenders target recipients + * @param textComponent the {@link TextComponent} to watermark and send + */ + public void sendMessageWatermarked(List commandSenders, TextComponent textComponent) { + TextComponent waterMarkedComponent = buildWaterMarked(textComponent); + + for(CommandSender commandSender : commandSenders) { + sendMessage(commandSender, waterMarkedComponent); + } + } + + /** + * Builds a watermarked version of a text component + * @param textComponent target component to watermark + * @return a new {@link TextComponent} with the (mcMMO) watermark at the beginning and the contents of {@link TextComponent} appended afterwards + */ + @NotNull + private TextComponent buildWaterMarked(TextComponent textComponent) { + return TextComponent.builder().content(pluginRef.getLocaleManager().getString("mcMMO.Template.Prefix")).append(textComponent).build(); + } + + /** + * Dissects a string and builds a {@link TextComponent} out of it. + * Results are cached to avoid needless operations in the future + * @param legacyText target text to transform + */ + private TextComponent transformLegacyTexts(String legacyText) { + //TODO: Cache results + TextComponent.Builder builder = TextComponent.builder(); + + for(int i = 0; i < legacyText.toCharArray().length; i++) { + char c = legacyText.charAt(i); + + //Found color character + if(c == COLOR_CHAR) { + if(i+1 >= legacyText.toCharArray().length) { + //No color code because we're at the end of the string + builder.append(String.valueOf(c)); + } else { + //TODO: finish + } + } else { + //Not a color character + } + } + return builder.build(); + } + +}