Thank you all for over 10,000 downloads!
Using this API, you can very easily create and send JSON messages. They feature colors, bold/italic/etc., insertions, and most importantly, hover- and click actions.
Installation
Commands
< For more detailed information, see the Javadocs >
==> Go to the Wiki Page
==> Go to the Javadoc
Do not use this library without a reference to the author; ColoredCarrot, and the resource on spigot ( this link here) .
Do not mark this code as your own.
Original Thread:
https://www.spigotmc.org/threads/api-json-api-with-integrated-send-methods.136140/
JsonApi
v3.1.0
(Incompatible with v2.x)
v3.1.0
(Incompatible with v2.x)
Using this API, you can very easily create and send JSON messages. They feature colors, bold/italic/etc., insertions, and most importantly, hover- and click actions.
Installation
- Download the latest version of JsonApi
- Place it in your server's plugins folder
- Restart your server
Commands
- /command - permission - description
- /jsonapi - jsonapi.command.jsonapi - Display version information
< For more detailed information, see the Javadocs >
Spoiler: Tutorial
The base class for creating JSON messages is JsonMsg.
Construct it with the appropriate text and color:
Notice that we specified the color using the
JsonColor enum.
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.
To add another JsonMsg to an existing one, use
JsonMsg#append(JsonMsg) or
JsonMsg#append(/* arguments from any constructor are valid */):
All this could also be simplified to:
There is another super easy way to create JSON messages:
Changing the color and styling of a JsonMsg could also not be simpler:
The corresponding values can be retrieved with the same method (except #style(ChatColor)), without arguments.
Tip: All methods that would otherwise return void return the JsonMsg object itself, so you can chain them together:
Now, let's get to the fun stuff!
To show a text when the player hovers over msg, use any of these methods:
Of course, there are many more methods for showing items, achievements etc., and there is also the JsonClickEvent class.
Finally, we can send the message.
You can also use
JsonMsg#send(Iterable<Player>) and
JsonMsg#sendTitle(int, int, int, Player...).
Note: There are many more methods and tricks you can use, however, there are way too many to list them all here. Just experiment!
Creating the message
Every 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
)
;
Spoiler: Converting color-coded messages <-> JsonMsg
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:
To convert a JsonMsg back to a color-coded message, use the method
JsonMsg.toConsoleMessage(boolean insertResetCodes). The
insertResetCodes parameter specifies whether the message and each extra message should be prefixed with a reset code (§r).
Example:
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
// The §a (green color) carries over to "§o§lworld!",
// since it does not specify a color itself and allowStyleFallthrough is true
Example:
Code (Java):
String codeBased
= msg.
toConsoleMessage
(
true
)
;
// codeBased is now "§r§r§aHello, §r§a§l§oworld!"
// codeBased is now "§r§r§aHello, §r§a§l§oworld!"
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
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 ) ) ;
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 too many to list them all here. Just experiment!
==> Go to the Wiki Page
==> Go to the Javadoc
Do not use this library without a reference to the author; ColoredCarrot, and the resource on spigot ( this link here) .
Do not mark this code as your own.
Original Thread:
https://www.spigotmc.org/threads/api-json-api-with-integrated-send-methods.136140/
Quick facts
- Edition: Minecraft Java
- File type: .jar
- Minecraft versions listed: 1.8, 1.9, 1.10
- How to install: Install the matching mod loader (Forge, Fabric or NeoForge) for your Minecraft version. → Download the .jar. → Put it in the .minecraft/mods folder and launch that loader profile.
- Where to get it: Opens on Spigot — not every file is mirrored on our own servers.
Install steps are the general flow for this file type — How to install Minecraft Java mods & modpacks walks through it step by step.
JSON API is a free Minecraft Java mod. Compatible with Minecraft 1.8, 1.9, 1.10, 1.11 and newer. Downloaded 14,470 times (via Spigot). Download it and open it directly in the game.