浏览代码

Some kyori/text stuff

nossr50 5 年之前
父节点
当前提交
4e895b7361

+ 26 - 3
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) {
 
     }

+ 1 - 1
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());

+ 1 - 0
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;

+ 106 - 0
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<CommandSender> 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<CommandSender> 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();
+    }
+
+}