Chat arguments
The CommandAPI provides a number of ways to interact with chat formatting in Minecraft. These are the following:
- ChatColor: The color of text rendered in Minecraft
- Chat: Text which is said in chat. This also includes entity selectors such as
@a
and@r
- ChatComponent: Minecraft's Raw JSON text format
The CommandAPI implements Chat and ChatComponent in two separate ways: Spigot-compatible and Adventure-compatible. The differences between these and how to use them are described in their own relevant pages. To use Minecraft 1.19's chat preview feature, information on that can be found in Chat preview.
Chat color argument
The ChatColorArgument
class is used to represent a given chat color (e.g. red or green)
Example - Username color changing plugin
Say we want to create a plugin to change the color of a player's username. We want to create a command of the following form:
/namecolor <chatcolor>
We then use the ChatColorArgument
to change the player's name color:
new CommandAPICommand("namecolor")
.withArguments(new ChatColorArgument("chatcolor"))
.executesPlayer((player, args) -> {
ChatColor color = (ChatColor) args[0];
player.setDisplayName(color + player.getName());
})
.register();
CommandAPICommand("namecolor")
.withArguments(ChatColorArgument("chatcolor"))
.executesPlayer(PlayerCommandExecutor { player, args ->
val color = args[0] as ChatColor
player.setDisplayName("$color${player.getName()}")
})
.register()