Util JsonBuilder

Discussion in 'Resources' started by FisheyLP, Apr 2, 2015.

Thread Status:
Not open for further replies.
  1. I've made a Class called JsonBuilder:
    You can easily create messages with colors, clickEvent, hoverEvent, a parser and even send it to a player!
    JsonBuilder.java (open)
    Code:java
    1. public class JsonBuilder {
    2.  
    3. /* JsonBuilder by FisheyLP */
    4.  
    5. public enum ClickAction {
    6. RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL
    7. }
    8. public enum HoverAction {
    9. SHOW_TEXT
    10. }
    11.  
    12. private List<String> extras = new ArrayList<String>();
    13.  
    14. public JsonBuilder(String... text) {
    15. for(String extra : text)
    16. parse(extra);
    17. }
    18.  
    19. public JsonBuilder parse(String text) {
    20. String regex = "[&§]{1}([a-fA-Fl-oL-O0-9]){1}";
    21. text = text.replaceAll(regex, "§$1");
    22. if(!Pattern.compile(regex).matcher(text).find()) {
    23. withText(text);
    24. return this;
    25. }
    26. String[] words = text.split(regex);
    27.  
    28. int index = words[0].length();
    29. for(String word : words) {
    30. try {
    31. if(index != words[0].length())
    32. withText(word).withColor("§"+text.charAt(index - 1));
    33. } catch(Exception e){}
    34. index += word.length() + 2;
    35. }
    36. return this;
    37. }
    38.  
    39. public JsonBuilder withText(String text) {
    40. extras.add("{text:\"" + text + "\"}");
    41. return this;
    42. }
    43.  
    44. public JsonBuilder withColor(ChatColor color) {
    45. String c = color.name().toLowerCase();
    46. addSegment(color.isColor() ? "color:" + c : c + ":true");
    47. return this;
    48. }
    49.  
    50. public JsonBuilder withColor(String color) {
    51. while(color.length() != 1) color = color.substring(1).trim();
    52. withColor(ChatColor.getByChar(color));
    53. return this;
    54. }
    55.  
    56. public JsonBuilder withClickEvent(ClickAction action, String value) {
    57. addSegment("clickEvent:{action:" + action.toString().toLowerCase()
    58. + ",value:\"" + value + "\"}");
    59. return this;
    60. }
    61.  
    62. public JsonBuilder withHoverEvent(HoverAction action, String value) {
    63. addSegment("hoverEvent:{action:" + action.toString().toLowerCase()
    64. + ",value:\"" + value + "\"}");
    65. return this;
    66. }
    67.  
    68. private void addSegment(String segment) {
    69. String lastText = extras.get(extras.size() - 1);
    70. lastText = lastText.substring(0, lastText.length() - 1)
    71. + ","+segment+"}";
    72. extras.remove(extras.size() - 1);
    73. extras.add(lastText);
    74. }
    75.  
    76. public String toString() {
    77. if(extras.size() <= 1) return extras.size() == 0 ? "{text:\"\"}" : extras.get(0);
    78. String text = extras.get(0).substring(0, extras.get(0).length() - 1) + ",extra:[";
    79. extras.remove(0);;
    80. for (String extra : extras)
    81. text = text + extra + ",";
    82. text = text.substring(0, text.length() - 1) + "]}";
    83. return text;
    84. }
    85.  
    86. public void sendJson(Player p) {
    87. ((CraftPlayer) p).getHandle().playerConnection.sendPacket(
    88. new PacketPlayOutChat(ChatSerializer.a(toString()), true));
    89. }
    90. }

    Use it like this:
    Code:java
    1. JsonBuilder jb = new JsonBuilder().withText("Where do you want to go? ").withColor(ChatColor.BOLD).withText("Spawn").withColor(ChatColor.GOLD).withClickEvent(ClickAction.RUN_COMMAND, "/spawn").withText(" or ").withText("Home").withColor("&6").withClickEvent(ClickAction.RUN_COMMAND, "/home");
    2.  
    3. System.out.println(jb.toString());

    Output:
    Code:java
    1. {text:"Where do you want to go? ",bold:true,extra:[{text:"Spawn",color:gold,clickEvent:{action:run_command,value:"/spawn"}},{text:" or "},{text:"Home",color:gold,clickEvent:{action:run_command,value:"/home"}}]}


    Or just parse some text:
    Code:java
    1. JsonBuilder jb = new JsonBuilder().parse("§7[§4Owner§7] §4FisheyLP§7: §cHi!");
    2. System.out.println(jb.toString());

    Output:
    Code:java
    1. {text:"[",color:gray,extra:[{text:"Owner",color:dark_red},{text:"] ",color:gray},{text:"FisheyLP",color:dark_red},{text:": ",color:gray},{text:"Hi!",color:red}]}

    To send it to a player use:
    Code:java
    1. jb.sendJson(player);

    It has these methods:
    Code:
    parse(String text) //Parses the text and converts the chat colors to json text
    withText(String text) //adds a text.
    withColor(ChatColor color) //sets the color of the last added text.
    withColor(String color) //sets the color of the last added text (Colorcodes like &a).
    withClickEvent(ClickAction action, String value) //adds a clickEvent to the last added text.
    withHoverEvent(HoverAction action, String value) //adds a hoverEvent to the last added text.
    toString() //returns the json text.
    sendJson(Player p) //sends the generated json to a player.
    The enum ClickAction has three values at the moment:
    Code:
    RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL
    The enum HoverAction has one value at the moment:
    Code:
    SHOW_TEXT
    Have fun with it :D
     
    Last edited: May 1, 2015
  2. Offline

    ChipDev

    Nice job! Gonna use this!
     
  3. This works great! I really appreciate you creating this for everyone to use!
     
  4. Offline

    teej107

    bwfcwalshy likes this.
  5. Offline

    mrCookieSlime

    @FisheyLP
    One suggestion though:
    Your "other" ArrayList should be static and final since you obviously do not want to recreate that Object everytime you create a new Instance of that Class. It would just be a waste of RAM.
     
  6. Okay I fix it
     
  7. Offline

    ArthurMaker

    @FisheyLP Looks good, but you could add more features, like "show_item" and "show_achievement". What do you think? :)
     
  8. ArthurMaker and teej107 like this.
  9. Yeah I work on that. I only added those which I know atm but I will look how to do show_item and show_achievement
     
    ArthurMaker likes this.
  10. Offline

    Phasesaber

    I feel like it would be better to make a syntax for links/colors instead of chaining methods on the end.
    Code:
    JsonBuilder jb = JsonBuilder.parse("&1This is a &2[message](http://google.com)&1!");
     
  11. Offline

    teej107

    You might as well just learn how JSON is formatted rather than learn some new parsing method from an API. Every developer knows how to call methods and knowing what they do is just as simple as using the Java docs, which every developer should do if he has questions.
     
    skyrimfan1, bwfcwalshy and Phasesaber like this.
  12. Offline

    Phasesaber

    @teej107 A text parser would be cool though.
     
  13. @Phasesaber
    I added the parser, it currently works for the colors.
    And @mrCookieSlime, I removed the ArrayList you talked about, and used the chatcolor.isColor() method instead
     
    Last edited: Apr 30, 2015
  14. Offline

    skyrimfan1

    I hate to be condescending but isn't this a watered down version of Fanciful?

    Forgive me, but it's a pet peeve of mine when people try and reinvent the wheel (so to speak) unnecessarily.
     
  15. Offline

    SimonG

    How can I add JSON into a book?
    I know how to add text into a book. But if i do the JSON.toString() than just the JSON comes in the book, not the text what I want.

    How can I do this?
     
  16. The json for books works a little bit different. I may work on the json things for books and signs
     
  17. Offline

    cococow123

  18. You either use bukkit and not craftbukkit for your plugin or you haven't imported it. In eclipse press ctrl + shift + o
     
  19. Offline

    cococow123

    Everything is imported.
    @FisheyLP

    After I imported everything, the red line showed up.
     
  20. @cococow123
    If you were smart enough, you would know you could hover the error to see what the problem is, and from my mind I can guess that that constructor does not exist. You used 1.8 while this library is built for 1.7- I think, there the 2nd parameter is a boolean, but in 1.8 it's a byte
     
  21. Offline

    cococow123

Thread Status:
Not open for further replies.

Share This Page