-
Notifications
You must be signed in to change notification settings - Fork 3
ComponentBuilder
This wiki page is all about the ComponentBuilder features! The ComponentBuilder is a replacement for Spigot's ComponentBuilder which, as of writing this draft (14th april 2020) has been marked as 'WIP' for 6+ years.
The ComponentBuilder is a tool that allows users to create Raw JSON text in the form of BaseComponents, which can be used in books and chat-messages.
This is an example of how you can chain functions to create a chat message.
ComponentBuilder builder = ComponentBuilder.create()
.thenText("This text has been ")
.setColor(ChatColor.LIGHT_PURPLE)
.thenText("formatted!")
.setHoverText("Click me!")
.setClickEvent(ClickEvent.Action.RUN_COMMAND, "I clicked some text!");Now you can send this message to a player.
player.spigot().sendMessage(builder.build());Or add it to a book.
ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
BookMeta meta = (BookMeta) book.getItemMeta();
meta.spigot().addPage(builder.build());
book.setItemMeta(meta);You can use the static ComponentBuilder#parse to parse a string into a ComponentBuilder. This allows you to create hover-able and clickable text from a single string.
The following string [Hover me!](show_text=That tickles!,open_url=https://tabuu.nl) would be translated to:
ComponentBuilder builder = ComponentBuilder.create()
.thenText("Hover me!")
.setHoverText("That tickles!")
.setClickEvent(ClickEvent.Action.OPEN_URL, "https://www.tabuu.nl");You can also format translatable text within the a string.
The following string [Let me guess your language!](show_text=\[language.name\]\(translatable=true\)!) would translate to:
ComponentBuilder translatable = ComponentBuilder.create()
.thenTranslatable("language.name")
.thenText("!");
ComponentBuilder builder = ComponentBuilder.create()
.thenText("Let me guess your language!")
.then(translatable);Some characters (']', '[', ')', '(', and ',') will have to be escaped in order to use them in some locations. You can do so by prefixing them with the '\' character. It is also possible to use the ComponentBuild.escape(String string) function!