JsonApi Usage
-
JsonApi Usage
The JsonApi was created by ColoredCarrot and can be used to create and send JSON messages in almost any versions of Minecraft.
==> Project Page
Creating the messageEvery good tutorial starts with a "Hello, World!" application. Let's create that JSON message!
The base class for creating JSON messages is JsonMsg.
Construct it with the appropriate text and color:
Code (Java):JsonMsg msg = new JsonMsg("Hello, ", JsonColor.AQUA);
Also, see how we only put half of the text into the JsonMsg? That's because we want the other half to be another color, and italic. Let's see how that's done:
We'll first create the second half, but this time use the constructor JsonMsg(String, ChatColor...). It styles the message with all styles specified as ChatColors.
Code (Java):JsonMsg msg2 = new JsonMsg("world!", ChatColor.RED, ChatColor.ITALIC /*, ChatColor.BOLD, etc.*/);
Code (Java):msg.append(msg2);
Code (Java):msg.append("world!", ChatColor.RED, ChatColor.ITALIC);
You can convert any color-code based message to a JsonMsg using the constructor JsonMsg(String text, boolean allowStyleFallthrough).
The allowStyleFallthrough parameter specifies whether the styles should be reset at every new "segment" of the text, where the segments are separated by a continuous set of color codes.
Example:
Code (Java):JsonMsg msg = new JsonMsg("§aHello, §o§lworld!", true);
// The §a (green color) carries over to "§o§lworld!",
// since it does not specify a color itself and allowStyleFallthrough is true
Example:
Code (Java):
Styling the message
Changing the color and styling of a JsonMsg could also not be simpler:
Code (Java):msg.color(JsonColor.GREEN); // Change the color
msg.underlined(true); // Underline the text
msg.style(ChatColor.GREEN, ChatColor.UNDERLINE); // Style the text
Tip: All methods that would otherwise return void return the JsonMsg object itself, so you can chain them together:
Code (Java):msg.color(JsonColor.GREEN).underlined(true);
Click & Hover Events
To show a text when the player hovers over msg, use any of these methods:
Code (Java):msg.hoverEvent(JsonHoverEvent.showText(msg2));
msg.hoverEvent(JsonHoverEvent.showText("Hey there!", JsonColor.GRAY));
msg.hoverEvent(JsonHoverEvent.showText("Hey there!", ChatColor.GRAY, ChatColor.BOLD));
Sending the message
Finally, we can send the message.
Code (Java):msg.send(player1, player2 /* , player3, etc. */);
Note: There are many more methods and tricks you can use, however, there are way to many to list them all here. Just experiment! - Loading...
- Loading...
XenCarta PRO
© Jason Axelrod from 8WAYRUN.COM