IChatBaseComponent

Discussion in 'Plugin Development' started by Kilovice, Jun 29, 2014.

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

    Kilovice

    I am working with IChatBaseComponent (sucks because json is hard to read) anyway I have this:
    Code:java
    1. IChatBaseComponent comp = ChatSerializer.a("{\"text\":\"" + "\",\"extra\":[{\"text\":\"" + "§9/cg globalmute §8- §5Globally Mutes Chat"
    2. + "\",\"clickEvent\": {\"action\":\"run_command\",\"value\":\"" + "/cg globalmute"
    3. + "\",\"hoverEvent\": {\"action\":\"show_text\",\"value\":\"" + "§7Click to Globally Mute Chat!"
    4. + "\"}}}]}");
    5. PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
    6. ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);


    Everything works excluding the hoverEvent, any idea what's wrong?
     
  2. Offline

    Stealth2800

    Have you taken a look at Fanciful? It makes dealing with the JSON much easier.
     
    Garris0n likes this.
  3. Offline

    fireblast709

    Kilovice After nicely formatting your actual JSON
    Code:
    {
        "text": "",
        "extra":
        [
            {
                "text": "§9/cg globalmute §8- §5Globally Mutes Chat",
                "clickEvent": 
                {
                    "action": "run_command",
                    "value": "/cg globalmute",
                    "hoverEvent":
                    {
                        "action": "show_text",
                        "value": "§7Click to Globally Mute Chat!"
                    }
                }
            }
        ]
    }
    It is pretty obvious what's wrong :3
     
    Garris0n likes this.
  4. Offline

    Kilovice

    Stealth2800
    Sorry, don't like using Libraries unless absolutely necessary

    fireblast709
    I'm quite a bit of a noob when it comes to json xD

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  5. Offline

    Garris0n

    Suit yourself, but it's a lot nicer than trying to write raw JSON into your IDE. I think this thread already shows why that's not the best idea.
     
  6. Offline

    Stealth2800

    Kilovice Why bother dealing with raw JSON when there's such an easy alternative instead? If you're not willing to use the resources you have, you're going to be reinventing the wheel many times.
     
  7. Offline

    Kilovice

    Stealth2800
    The only time I ever use external libraries is if its that much of a pain in the ass or absolutely necessary, with this, I'm having one problem and I can't seem to find it

    fireblast709 Garris0n
    I fixed the json message using a json parser, but how do I then convert it to IChatBaseComponent

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  8. Offline

    MCForger

    Kilovice
    Just look how Fanciful created a message.
     
  9. Offline

    Garris0n

    It was pretty obvious assuming you know how to create the chat messages in the first place.

    https://github.com/Bukkit/mc-dev/blob/master/net/minecraft/server/ChatSerializer.java#L175
     
  10. if you want to keep it easy you can use this:
    CLib.java
    Code:
        public static void sendMagicMessage(Player player, EnchantedMessage[] msg) {
            IChatBaseComponent comp = null;
            for(EnchantedMessage Message : msg){
            String FirstMessage = setColor(Message.text());
            String SpecialText = setColor(Message.specialText());
            String Hover = setColor(Message.onHover());
            FirstMessage = "\"" + FirstMessage + "\"";
            SpecialText = "\"" + SpecialText + "\"";
            Hover = "\"" + Hover + "\"";
            String finalS = "";
            if(Message.command().equals("false")){
                finalS = "{\"text\":" + FirstMessage +",\"extra\":[{\"text\":" + SpecialText + ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":" + Hover +"}}]}";
            }
            else
            {
                finalS = "{\"text\":" + FirstMessage +",\"extra\":[{\"text\":" + SpecialText + ",\"hoverEvent\":{\"action\":\"show_text\",\"value\":" + Hover +"},\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/" + Message.command() + "\"}}]}";
            }
            if(comp==null){
                comp = ChatSerializer.a(finalS);
            }
            else
            {
                IChatBaseComponent comp2 = ChatSerializer.a(finalS);
                comp.addSibling(comp2);
            }
            }
            PacketPlayOutChat packet = new PacketPlayOutChat(comp, true);
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
        }
      private static String setColor(String string){
         string = string.replace("#BOLD", ChatColor.BOLD.toString());
         string = string.replace("#DRED", ChatColor.DARK_RED.toString());
         string = string.replace("#RED", ChatColor.RED.toString());
         string = string.replace("#DBLUE", ChatColor.DARK_BLUE.toString());
         string = string.replace("#BLUE", ChatColor.BLUE.toString());
         string = string.replace("#AQUA", ChatColor.AQUA.toString());
         string = string.replace("#BLACK", ChatColor.BLACK.toString());
         string = string.replace("#ITALIC", ChatColor.ITALIC.toString());
         string = string.replace("#RESET", ChatColor.RESET.toString());
         string = string.replace("#DAQUA", ChatColor.DARK_AQUA.toString());
         string = string.replace("#DGRAY", ChatColor.DARK_GRAY.toString());
         string = string.replace("#DGREEN", ChatColor.DARK_GREEN.toString());
         string = string.replace("#DPURPLE", ChatColor.DARK_PURPLE.toString());
         string = string.replace("#MAGIC", ChatColor.MAGIC.toString());
         string = string.replace("#GOLD", ChatColor.GOLD.toString());
         string = string.replace("#GRAY", ChatColor.GRAY.toString());
         string = string.replace("#GREEN", ChatColor.GREEN.toString());
         string = string.replace("#LPURPLE", ChatColor.LIGHT_PURPLE.toString());
         string = string.replace("#ST", ChatColor.STRIKETHROUGH.toString());
         string = string.replace("#WHITE", ChatColor.WHITE.toString());
         string = string.replace("#YELLOW", ChatColor.YELLOW.toString());
         return string;
       }
    
    EnchantedMessage.java
    Code:
    public class EnchantedMessage {
        private String text;
        private String specialtext;
        private String onhover;
        private String command;
        public EnchantedMessage(String Text, String SpecialText, String OnHover, String OnClick){
            this.text = Text;
            this.specialtext = SpecialText;
            this.onhover = OnHover;
            if(OnClick.equals("")){
                this.command = "false";
            }
            else
            {
                this.command = OnClick;
            }
        }
        public String onHover(){
            return this.onhover;
        }
        public String text(){
            return this.text;
        }
        public String specialText(){
            return this.specialtext;
        }
        public String command(){
            return this.command;
        }
    }
    
    Usage:
    Code:
    EnchantedMessage em1 = new EnchantedMessage("TextHere","SpecialTextWithAction","ActionOnHoveringOnSpecialText","CommandOnClickSpecialText");
    CLib.sendMagicMessage(player, new EnchantedMessage[]{em1});
    Hope it helped you out ;)
    if you leave CommandOnClickSpecialText empty the message wont run a command on click.
    and dont forget to put a space in front of your second and third and....... usage of TextHere
    Because it will be all written to one line
     
  11. Offline

    eyamaz

Thread Status:
Not open for further replies.

Share This Page