IChatBaseComponent help

Discussion in 'Plugin Development' started by InfamousSheep, Oct 26, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    InfamousSheep

    Hello all,
    I have a method that creates hoverable text however, I'm not sure how to include it into a sentence. For example, it can only currently be printed on it's own like but I wan't it to be like "Hello there, INFO and stuff."
    INFO being the hoverable text.

    Here's my current code:
    Code:
    public static void printHoverable(String playerName, String chatMessage, String clickableMessage, String hoverMessage) {
    IChatBaseComponent chatBase = new ChatMessage(chatMessage);
     
    chatBase.setChatModifier(new ChatModifier());
    chatBase.getChatModifier().setChatClickable(new ChatClickable(EnumClickAction.SUGGEST_COMMAND, clickableMessage));
    chatBase.getChatModifier().a(new ChatHoverable(EnumHoverAction.SHOW_TEXT, new ChatMessage(hoverMessage)));
     
    PlayerList list = MinecraftServer.getServer().getPlayerList();
    list.getPlayer(playerName).sendMessage(chatBase);
    }
    
    Thanks in advance.
     
  2. Offline

    rbrick

    Do this EnumClickAction.SOME_ENUM_VALUE.a() or for EnumHoverAction: EnumHoverAction.SOME_ENUM_VALUE.b()
     
  3. Offline

    InfamousSheep

    That only adds to the hoverMessage or clickableMessage. I want them to be two separate strings, one that is hoverable and one that is not.
     
  4. Offline

    InfamousSheep

  5. Offline

    ResultStatic

    InfamousSheep this is how i do it, i created this class that handles fancy text. this is so much better than a 10 class lib. its pretty self explanatory just send it to a player after you finish your message

    Code:
    public class FancyText {
     
     
        LinkedHashMap<String, MessageComponent> message = new LinkedHashMap<String, MessageComponent>();
     
     
        public FancyText addText(String text){
            this.message.put(text, new MessageComponent(text, null, null));
            return this;
        }
     
        public FancyText addClickableLink(String text,String link){
            this.message.put(link, new MessageComponent(text, link, EnumClickAction.OPEN_URL));
            return null;
     
     
     
        }
     
        public FancyText addRunnableCommand(String text, String command){
     
            this.message.put(text, new MessageComponent(text, command, EnumClickAction.RUN_COMMAND));
            return this;
        }
     
        public FancyText addChatSuggestion(String text,String suggestion){
     
            this.message.put(text, new MessageComponent(text, suggestion, EnumClickAction.SUGGEST_COMMAND));
     
            return this;
            }
     
     
        public FancyText addHoverEvent(String text, String hover){
     
            this.message.put(text, new MessageComponent(text,hover ,EnumHoverAction.SHOW_TEXT));
     
            return this;
            }
     
     
     
     
        public void sendToPlayer(Player player){
            ChatComponentText master = new ChatComponentText("");
            for (String text: message.keySet()){
                for (IChatBaseComponent m: message.get(text).compile()) {
                    master.a(m);
                    }
       
                }
            Nms.getCraftPlayer(player).playerConnection.sendPacket(new PacketPlayOutChat(master));
            }
     
        public void sendToAllPlayers(){
            for (Player player: Bukkit.getOnlinePlayers()){
                this.sendToPlayer(player);
            }
        }
        public class MessageComponent {
     
            Enum<?> e;
            String data;
            String text;
            IChatBaseComponent[] chat;
     
            public MessageComponent(String text, String data, Enum<?> e){
            this.e = e;
            this.text = text;
            this.data = data;
            chat = CraftChatMessage.fromString(text);
            }
     
     
     
     
     
            public IChatBaseComponent[] compile(){
                for (IChatBaseComponent c: chat){
                if (data == null || e == null){
       
                    return chat;
                }
                    if (e instanceof EnumClickAction){
                    c.getChatModifier().setChatClickable(new ChatClickable((EnumClickAction) e, data));
                    }else if (e instanceof EnumHoverAction){
                        c.getChatModifier().a((new ChatHoverable((EnumHoverAction)e, new ChatComponentText(data))));
                    }
                }
       
                return chat;
            }
     
        }
     
     
    }
     
  6. Offline

    InfamousSheep

    Can you give me some example code for this utility?
    Also, on like 68 class "Nms" cannot be resolved.
     
  7. Offline

    ResultStatic

    InfamousSheep thats just a utility i use to get the CraftPlayer from Player.

    u can use
    Code:java
    1. CraftPlayer cp = (CraftPlayer)player;
    2. EntityPlayer ep = cp.getHandle();


    then here is how i use it to create rank prefixs
    Code:java
    1. FancyText commander = new FancyText();
    2. commander.addHoverEvent(ChatColor.DARK_AQUA + "✯ " + ChatColor.AQUA + player.getName() + ": ", ChatColor.DARK_AQUA + "Rank: Commander, " + ChatColor.DARK_AQUA + "RealName:" + " " + player.getName());
    3. commander.sendToPlayer(player);
     
  8. Offline

    InfamousSheep

    It returns nothing.
    Code:
    public void sendToPlayer(Player player) {
    ChatComponentText master = new ChatComponentText("");
    for (String text : message.keySet()){
    for (IChatBaseComponent m : message.get(text).compile()) {
    master.a(m);
    }
    } ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(master));
    }
    
    [​IMG]
     
  9. Offline

    ResultStatic

    InfamousSheep oh sorry i didnt fully read your post, but why do you need to return the string? this directly sends it to the player. i dont think you can convert the fancy messages into regular messages there might be a way, but i dont i know why you would need to.
     
  10. Offline

    InfamousSheep

    That's what I'm doing XD
    commander.sendToPlayer(player);
     
  11. Offline

    ResultStatic

    InfamousSheep im confused about what your asking now could you explain further?
     
  12. Offline

    InfamousSheep

    I need hoverable text to be added in a string because at the moment, it can only be in it's own message.
    For example: "Welcome, INFO" the underlined text is the only text that is hoverable in that string.
     
  13. Offline

    ResultStatic

    InfamousSheep im still a bit confused on what your asking, but you can add plain text with the addText("your text here") then you can add hoverable text with addHoverEvent(String text, String hover); all the text is placed in the message in the order you put it.
    Code:
    FancyText text = new FancyText();
    text.addText("Welcome,");
    text.addHoverableText("Info", "here is the information";
     
  14. Offline

    InfamousSheep

    That's exactly what I did, still returns null :L
     
  15. Offline

    ResultStatic

  16. Offline

    InfamousSheep

    There aren't any errors in console.
    Code:
    FancyText text = new FancyText();
    text.addHoverEvent("Message", "Hover");
    text.sendToPlayer(player);
     
    public FancyText addHoverEvent(String text, String hover) {
    this.message.put(text, new MessageComponent(text, hover, EnumHoverAction.SHOW_TEXT));
    return this;
    }
     
    public void sendToPlayer(Player player) {
    ChatComponentText master = new ChatComponentText("");
    for (String text : message.keySet()){
    for (IChatBaseComponent m : message.get(text).compile()) {
    master.a(m);
    }
    } ((CraftPlayer) player).getHandle().playerConnection.sendPacket(new PacketPlayOutChat(master));
    }
    
     
  17. Offline

    ResultStatic

    you said it returns null. i have no idea what your talking about now whats the problem?

    Wait what your suppose to create the FancyText text = new FancyText(); in another class or where ever you are using it. are you even calling that code from a method???
     
  18. Offline

    InfamousSheep

    It sends me a blank message and yeah, they're in separate classes that's just how I displayed the code.
     
  19. Offline

    ResultStatic

  20. Offline

    CullanP


    Same thing happens to me, Blank space in chat
     
  21. Offline

    97WaterPolo

    ResultStatic
    I tested it, InfamousSheep is right, it is just displaying an empty line.
    Show Spoiler
    Code:
    if (args[0].equalsIgnoreCase("jmsg")){
                        player.sendMessage(ChatColor.RED + "Test 1 cleared!");
                        FancyText m = new FancyText();
                        m.addText("[");
                        m.addHoverEvent("1056", "1054");
                        m.addHoverEvent("1056", "1058");
                        m.addHoverEvent("1056", "1056");
                        m.addText("]" + player.getName() + ": This is a test for JSON!");
                        m.sendToPlayer(player);
                        
                        FancyText commander = new FancyText();
                        commander.addHoverEvent(ChatColor.DARK_AQUA + "* " + ChatColor.AQUA + player.getName() + ": ", ChatColor.DARK_AQUA + "Rank: Commander, " + ChatColor.DARK_AQUA + "RealName:" + " " + player.getName());
                        commander.sendToPlayer(player); 
                        
                    }


    The out put is(the bottom one is the one with your own code):
    [​IMG][​IMG]
     
  22. Offline

    InfamousSheep

    Problem solved:
    Code:
    BaseComponent[] hoverText = new ComponentBuilder("Some text displayed when we hover over INFO").create();
    HoverEvent hoverEvent = new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverText);
    BaseComponent[] message = new ComponentBuilder("Hello there, ").append("INFO").event(hoverEvent).create();
    player.spigot().sendMessage(message);
    
    Only thing is that you have to use a patched version of spigot.
     
  23. Offline

    CullanP


    what is that
     
  24. Offline

    ResultStatic

    97WaterPolo CullanP InfamousSheep it works perfect for me. im on 1.7.9.

    Code:java
    1.  
    2. import java.util.LinkedHashMap;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.craftbukkit.v1_7_R3.util.CraftChatMessage;
    5. import org.bukkit.entity.Player;
    6. import net.minecraft.server.v1_7_R3.ChatClickable;
    7. import net.minecraft.server.v1_7_R3.ChatComponentText;
    8. import net.minecraft.server.v1_7_R3.ChatHoverable;
    9. import net.minecraft.server.v1_7_R3.EnumClickAction;
    10. import net.minecraft.server.v1_7_R3.EnumHoverAction;
    11. import net.minecraft.server.v1_7_R3.IChatBaseComponent;
    12. import net.minecraft.server.v1_7_R3.PacketPlayOutChat;
    13.  
    14. public class FancyText {
    15.  
    16.  
    17. LinkedHashMap<String, MessageComponent> message = new LinkedHashMap<String, MessageComponent>();
    18.  
    19.  
    20. public FancyText addText(String text){
    21. this.message.put(text, new MessageComponent(text, null, null));
    22. return this;
    23. }
    24.  
    25. public FancyText addClickableLink(String text,String link){
    26. this.message.put(link, new MessageComponent(text, link, EnumClickAction.OPEN_URL));
    27. return null;
    28.  
    29.  
    30.  
    31. }
    32.  
    33. public FancyText addRunnableCommand(String text, String command){
    34.  
    35. this.message.put(text, new MessageComponent(text, command, EnumClickAction.RUN_COMMAND));
    36. return this;
    37. }
    38.  
    39. public FancyText addChatSuggestion(String text,String suggestion){
    40.  
    41. this.message.put(text, new MessageComponent(text, suggestion, EnumClickAction.SUGGEST_COMMAND));
    42.  
    43. return this;
    44. }
    45.  
    46.  
    47. public FancyText addHoverEvent(String text, String hover){
    48.  
    49. this.message.put(text, new MessageComponent(text,hover ,EnumHoverAction.SHOW_TEXT));
    50.  
    51. return this;
    52. }
    53.  
    54.  
    55.  
    56.  
    57. public void sendToPlayer(Player player){
    58. ChatComponentText master = new ChatComponentText("");
    59. for (String text: message.keySet()){
    60. for (IChatBaseComponent m: message.get(text).compile()) {
    61. master.a(m);
    62. }
    63.  
    64. }
    65. Nms.getCraftPlayer(player).playerConnection.sendPacket(new PacketPlayOutChat(master));
    66. }
    67.  
    68. public void sendToAllPlayers(){
    69. for (Player player: Bukkit.getOnlinePlayers()){
    70. this.sendToPlayer(player);
    71. }
    72. }
    73. public class MessageComponent {
    74.  
    75. Enum<?> e;
    76. String data;
    77. String text;
    78. IChatBaseComponent[] chat;
    79.  
    80. public MessageComponent(String text, String data, Enum<?> e){
    81. this.e = e;
    82. this.text = text;
    83. this.data = data;
    84. chat = CraftChatMessage.fromString(text);
    85. }
    86.  
    87. public IChatBaseComponent[] compile(){
    88. for (IChatBaseComponent c: chat){
    89. if (data == null || e == null){
    90.  
    91. return chat;
    92. }
    93. if (e instanceof EnumClickAction){
    94. c.getChatModifier().setChatClickable(new ChatClickable((EnumClickAction) e, data));
    95. }else if (e instanceof EnumHoverAction){
    96. c.getChatModifier().a((new ChatHoverable((EnumHoverAction)e, new ChatComponentText(data))));
    97. }
    98. }
    99.  
    100. return chat;
    101. }
    102. }
    103. }



    Code:java
    1. FancyText text = new FancyText();
    2. text.addText("Worked");
    3. text.addHoverEvent(" hoverable", "hovering text");
    4. text.sendToPlayer(wrap.getPlayerSender());


    http://gyazo.com/80094874c0a9917df347981ecd553298
     
  25. Offline

    CullanP

    No wonder, doesn't work compiling in 1.7.10
     
  26. Offline

    97WaterPolo

    ResultStatic
    Straight 1.7.9 or the one with 1.8 compat?
     
  27. Offline

    InfamousSheep

    I'm using Spigot as an import and not Bukkit. The version is 1.7.10 that's probably why it didn't work.
     
  28. Offline

    ResultStatic


    i just saw that message. now that i think of it i am running spigot on my test server but i dont have that code in there. i dont see why that would effect the messages who knows
     
  29. Offline

    CullanP

    Would this work on latest spigot if compiled with 1.7.9?

    Edit: ChatComponentText master = new ChatComponentText(""); this line seems to be the cause
     
  30. Offline

    ResultStatic

    CullanP i have no idea what version of 1.7.9 i have. well they are going to close this thread for even mentioning spigot... they are so uptight about it ridiculous in my opinion
     
Thread Status:
Not open for further replies.

Share This Page