-
Notifications
You must be signed in to change notification settings - Fork 1
Send or Use Messages
Messages implement the ComponentLike interface and can therefore be used, wherever you would normally use Adventure Components. I will give a quick overview here but for a detailled explanation, checkout the Adventure docs.
If you use PaperMC, which already includes a lot of extra functions for components, you are able to use Messages simply like so:
player.sendMessage(MY_MESSAGE.formatted(myPlaceholders));
If you use SpigotMC, you have to write the sendMessage
method yourself. For this and for some other contexts, the following preparation must be done:
AudienceProvider audienceProvider= null;
// on enable:
audienceProvider= BukkitAudiences.create(plugin);
// on disable:
audienceProvider.close();
Use the earlier produced AudienceProvider to get a players audience and send a Message to this audience.
void sendMessage(CommandSender sender, Message message) {
if (sender instanceof Player player) {
audienceProvider.player(player.getUniqueId()).sendMessage(message);
return;
}
audienceProvider.console().sendMessage(message);
}
Same as chat messages, but with method
Audience#showTitle(Title)
Title is a combination of two components for title and subtitle.
Title.title(MSG_TITLE, MSG_SUBTITLE);
Same as chat messages, but with method
Audience#showBossBar(BossBar)
Bossbar is an object that combines a component with bossbar color, health and bars.
To use Messages in inventory names, you must use a different approach. Inventory names don't support hover or click messages. We therefore convert our message into the legacy format using paragraphs.
Inventory inv = Bukkit.createInventory(null, 6*9, MY_GUI_TITLE.toString(MessageFormat.LEGACY_PARAGRAPH));
Similar to inventory names, we cannot set hover and click texts. Instead, we have two options.
- We format the Message to nbt and replace the nbt data for name and lore of the ItemStack.
- We format the Message to legacy paragraph format and set the value.
ItemStack i = new ItemStack(Material.DIAMOND);
ItemMeta meta = i.getItemMeta(); // null check and so on
meta.setDisplayName(DIA_NAME.toString(MessageFormat.LEGACY_PARAGRAPH));
// split lore on linebreaks (`\n`) to get multiple lines.
meta.setLore(List.of(DIA_LORE.toString(MessageFormat.LEGACY_PARAGRAPH).split("\n")));
i.setItemMeta(meta);