Skip to content

Send or Use Messages

Leo edited this page Dec 1, 2023 · 3 revisions

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.

Preparation

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();

Chat Message

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);
}

Titles

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);

Bossbar

Same as chat messages, but with method Audience#showBossBar(BossBar) Bossbar is an object that combines a component with bossbar color, health and bars.

Inventory Names

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));

ItemStacks

Similar to inventory names, we cannot set hover and click texts. Instead, we have two options.

  1. We format the Message to nbt and replace the nbt data for name and lore of the ItemStack.
  2. 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);