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 (Move your mouse to reveal the content) JsonBuilder.java (open) JsonBuilder.java (close) Code:java public class JsonBuilder { /* JsonBuilder by FisheyLP */ public enum ClickAction { RUN_COMMAND, SUGGEST_COMMAND, OPEN_URL } public enum HoverAction { SHOW_TEXT } private List<String> extras = new ArrayList<String>(); public JsonBuilder(String... text) { for(String extra : text) parse(extra); } public JsonBuilder parse(String text) { String regex = "[&§]{1}([a-fA-Fl-oL-O0-9]){1}"; text = text.replaceAll(regex, "§$1"); if(!Pattern.compile(regex).matcher(text).find()) { withText(text); return this; } String[] words = text.split(regex); int index = words[0].length(); for(String word : words) { try { if(index != words[0].length()) withText(word).withColor("§"+text.charAt(index - 1)); } catch(Exception e){} index += word.length() + 2; } return this; } public JsonBuilder withText(String text) { extras.add("{text:\"" + text + "\"}"); return this; } public JsonBuilder withColor(ChatColor color) { String c = color.name().toLowerCase(); addSegment(color.isColor() ? "color:" + c : c + ":true"); return this; } public JsonBuilder withColor(String color) { while(color.length() != 1) color = color.substring(1).trim(); withColor(ChatColor.getByChar(color)); return this; } public JsonBuilder withClickEvent(ClickAction action, String value) { addSegment("clickEvent:{action:" + action.toString().toLowerCase() + ",value:\"" + value + "\"}"); return this; } public JsonBuilder withHoverEvent(HoverAction action, String value) { addSegment("hoverEvent:{action:" + action.toString().toLowerCase() + ",value:\"" + value + "\"}"); return this; } private void addSegment(String segment) { String lastText = extras.get(extras.size() - 1); lastText = lastText.substring(0, lastText.length() - 1) + ","+segment+"}"; extras.remove(extras.size() - 1); extras.add(lastText); } public String toString() { if(extras.size() <= 1) return extras.size() == 0 ? "{text:\"\"}" : extras.get(0); String text = extras.get(0).substring(0, extras.get(0).length() - 1) + ",extra:["; extras.remove(0);; for (String extra : extras) text = text + extra + ","; text = text.substring(0, text.length() - 1) + "]}"; return text; } public void sendJson(Player p) { ((CraftPlayer) p).getHandle().playerConnection.sendPacket( new PacketPlayOutChat(ChatSerializer.a(toString()), true)); }} Use it like this: Code:java 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"); System.out.println(jb.toString()); Output: Code:java {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 JsonBuilder jb = new JsonBuilder().parse("§7[§4Owner§7] §4FisheyLP§7: §cHi!");System.out.println(jb.toString()); Output: Code:java {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 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
@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.
@FisheyLP Looks good, but you could add more features, like "show_item" and "show_achievement". What do you think?
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
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!");
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.
@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
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.
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?
You either use bukkit and not craftbukkit for your plugin or you haven't imported it. In eclipse press ctrl + shift + o
@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