[NMS] [JSON] JSONText - JSON Messages for 1.7.9+

Discussion in 'Resources' started by MineStein, Oct 18, 2014.

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

    MineStein

    Awesome update details at the bottom of the page.

    Hello everyone! I have written a relatively simple class that uses NMS' ChatSerializer and packets to send messages that you can hover over to show text, click to perform an action, etc.

    Here's the class:
    Code:java
    1. public class JSONText {
    2.  
    3. String JSONText;
    4. CraftPlayer craftPlayer;
    5. EntityPlayer recipeint;
    6.  
    7. public JSONText(String JSONText, CraftPlayer craftPlayer) {
    8. this.JSONText = JSONText;
    9. this.craftPlayer = craftPlayer;
    10.  
    11. this.recipeint = craftPlayer.getHandle();
    12. }
    13.  
    14. public void playOut() {
    15. IChatBaseComponent component = ChatSerializer.a(JSONText);
    16. PacketPlayOutChat packetPlayOutChat = new PacketPlayOutChat(component, true);
    17.  
    18. recipeint.playerConnection.sendPacket(packetPlayOutChat);
    19. }
    20.  
    21. public String getJSONText() {
    22. return JSONText;
    23. }
    24.  
    25. public void setJSONText(String JSON) {
    26. this.JSONText = JSON;
    27. }
    28.  
    29. public CraftPlayer getCraftPlayer() {
    30. return craftPlayer;
    31. }
    32.  
    33. public void setCraftPlayer(CraftPlayer craftPlayer) {
    34. this.craftPlayer = craftPlayer;
    35. }
    36.  
    37. public EntityPlayer getRecipeint() {
    38. return recipeint;
    39. }
    40.  
    41. public void setRecipeint(EntityPlayer recipeint) {
    42. this.recipeint = recipeint;
    43. }
    44. }
    45.  


    To create the text, use the following link and generate the String: http://minecraftjson.com

    Now you need to create a new instance of the JSONText class like this:

    Code:java
    1. final CraftPlayer p = (CraftPlayer) player;
    2.  
    3. final JSONText text = new JSONText("[\n" +
    4. " {\n" +
    5. " \"text\": \"§bClick this for help!\",\n" +
    6. " \"clickEvent\": {\n" +
    7. " \"action\": \"suggest_command\",\n" +
    8. " \"value\": \"/help \"\n" +
    9. " },\n" +
    10. " \"hoverEvent\": {\n" +
    11. " \"action\": \"show_text\",\n" +
    12. " \"value\": {\n" +
    13. " \"text\": \"§5§oProvides help for a specified\n§5§ocommand/gives you a list\n§5§oof all commands.\"\n" +
    14. " }\n" +
    15. " }\n" +
    16. " }\n" +
    17.  
    18. "]", p);


    Then to actually send the text, do JSONText#playOut. This will send the JSON message to the player.


    Update #2!

    In this update of the library, I have added a new class file for creating JSON text without the complexity of writing your own, or the tediousness of using websites.

    Code:
    public class JSONCreator {
    
        public enum ClickEventType {
            RUN_COMMAND("run_command"),
    
            SUGGEST_COMMAND("suggest_command"),
    
            OPEN_URL("open_url");
    
            String type;
    
            ClickEventType(String type) {
                this.type = type;
            }
    
            String getType() {
                return this.type;
            }
        }
    
        StringBuilder json;
    
        public JSONCreator() {
            this.json = new StringBuilder();
            add("[\"\",");
        }
    
        public JSONCreator addText(String text, ClickEventType clickEventType, String clickEventValue, String hoverText) {
            add("{\"text\":\""+text+"\"");
    
            if (clickEventType!=null&&clickEventValue!=null) {
                add(", \"clickEvent\":{\""+clickEventType.getType()+"\",\"value\":\""+clickEventValue+"\"}");
            }
    
            if (hoverText!=null) {
                add(", \"hoverEvent\":{\"action\":\"show_text\", \"value\":{\"value\":\"\", \"extra\":[{\"text\":\""+hoverText+"\"}]}}");
            }
    
            return this;
        }
    
        public JSONCreator finish() {
            add("}]");
    
            return this;
        }
    
        public String toString() {
            return this.json.toString();
        }
    
        private void add(Object object) {
            this.json.append(object);
        }
    }
    To use this class, you must first create a new instance. Upon instantiation, the class generates the beginning to the JSON text.


    Adding plain text.

    You can use JSONCreator to create ordinary text without any hover/click features, or you could opt in to do so.

    Here is the code to create a plain piece of text.

    Code:
    JSONCreator creator = new JSONCreator()
                    .addText("Plain text", null, null, null);

    Adding click events.

    JSONCreator can also create text that can do three things when clicked on (run a command, suggest a command in chat, or open a URL in the browser).

    Here is the code to create them.

    Code:
    JSONCreator creator = new JSONCreator()
                    .addText("Click me :)", ClickEventType.OPEN_URL, "http://www.google.com", null);

    Adding hover events.

    Implement a sweet hover display whenever the text is hovered over!

    Here is the code.

    Code:
    JSONCreator creator = new JSONCreator()
                    .addText("Hover over me :)", null, null, "Much amaze!");

    Displaying the text.

    Displaying the text is super simple.

    Code:
    creator.finish().toString();
    IMPORTANT!
    ****************************************
    THE CODE WILL NOT WORK UNLESS YOU CALL THE FINISH METHOD AT THE END. THE JSON FORMATTING WILL BE IMPROPER OTHERWISE.
     
    Last edited: Feb 24, 2015
    zThana and FerusGrim like this.
  2. Offline

    FerusGrim

    I've been wondering for a while how to actually do this, but never really dedicated the time to figuring it out. Thanks, for this. Really, Bukkit should have already had this important feature already built-in! Here's to hoping that certain... alternatives, do so.
     
  3. Offline

    xTrollxDudex

  4. Offline

    MineStein

    Check out the new update! The new attached file ('JSONCreator') allows you to create complex JSON messages in chat without the complexity of writing it yourself, or the tediousness of using a website.
     
  5. Offline

    Maxx_Qc

    Hi, I have a problem.

    JSONCreator creator = new JSONCreator().addText("Plain text", null, null, null);
    target.sendMessage(creator.finish().toString());

    It prints normally, not like JSON.
    [""bla bla bla]
    This is how it appears, but replace bla bla bla with args of JSON.
     
  6. Offline

    cococow123

    Does it work for 1.8?
     
Thread Status:
Not open for further replies.

Share This Page