Class MessageService

java.lang.Object
net.blockhost.commons.message.MessageService

public final class MessageService extends Object

Service for handling MiniMessage-based message formatting and sending.

This service provides a convenient API for deserializing MiniMessage strings, applying placeholders, and sending messages to audiences (players, console, etc.).

Example usage:

// Create a message service with a message provider
MessageService messages = MessageService.create(key -> config.getString("messages." + key));

// Send a simple message
messages.send(player, "welcome");

// Send a message with placeholders
messages.send(player, "player-joined", Map.of("player", playerName));

// Or use TagResolvers directly
messages.send(player, "balance", Placeholder.parsed("amount", balance));
See Also:
  • MiniMessage
  • TagResolver
  • Method Summary

    Modifier and Type
    Method
    Description
    create(Function<String,String> messageProvider)
    Creates a new message service with the specified message provider.
    get(String key)
    Gets a message by key and parses it.
    get(String key, Map<String,String> placeholders)
    Gets a message by key and parses it with placeholder replacements.
    get(String key, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
    Gets a message by key and parses it with tag resolvers.
    static net.kyori.adventure.text.minimessage.MiniMessage
     
    static net.kyori.adventure.text.minimessage.MiniMessage
    Returns the shared MiniMessage instance.
    static Component
    parse(String message)
    Parses a MiniMessage string into a Component.
    static Component
    parse(String message, Map<String,String> placeholders)
    Parses a MiniMessage string with placeholder replacements.
    static Component
    parse(String message, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
    Parses a MiniMessage string with tag resolvers.
    static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
    placeholder(String key, String value)
    Creates a placeholder resolver for a key-value pair.
    static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
    placeholder(String key, Component component)
    Creates a placeholder resolver for a key-component pair.
    static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
    resolvers(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
    Combines multiple tag resolvers into one.
    void
    send(Audience audience, String key)
    Sends a message to an audience.
    void
    send(Audience audience, String key, Map<String,String> placeholders)
    Sends a message to an audience with placeholder replacements.
    void
    send(Audience audience, String key, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
    Sends a message to an audience with tag resolvers.
    void
    sendRaw(Audience audience, String message)
    Sends a raw MiniMessage string to an audience.
    void
    sendRaw(Audience audience, String message, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
    Sends a raw MiniMessage string to an audience with tag resolvers.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • create

      public static MessageService create(Function<String,String> messageProvider)

      Creates a new message service with the specified message provider.

      The message provider is a function that takes a message key and returns the raw MiniMessage string for that key. This allows integration with any configuration system.

      Parameters:
      messageProvider - function that retrieves raw messages by key
      Returns:
      a new message service
    • parse

      public static Component parse(String message)
      Parses a MiniMessage string into a Component.
    • parse

      public static Component parse(String message, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
      Parses a MiniMessage string with tag resolvers.
    • parse

      public static Component parse(String message, Map<String,String> placeholders)
      Parses a MiniMessage string with placeholder replacements.
    • get

      public Component get(String key)
      Gets a message by key and parses it.
    • get

      public Component get(String key, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
      Gets a message by key and parses it with tag resolvers.
    • get

      public Component get(String key, Map<String,String> placeholders)
      Gets a message by key and parses it with placeholder replacements.
    • send

      public void send(Audience audience, String key)
      Sends a message to an audience.
    • send

      public void send(Audience audience, String key, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
      Sends a message to an audience with tag resolvers.
    • send

      public void send(Audience audience, String key, Map<String,String> placeholders)
      Sends a message to an audience with placeholder replacements.
    • sendRaw

      public void sendRaw(Audience audience, String message)
      Sends a raw MiniMessage string to an audience.
    • sendRaw

      public void sendRaw(Audience audience, String message, net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
      Sends a raw MiniMessage string to an audience with tag resolvers.
    • placeholder

      public static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver placeholder(String key, String value)
      Creates a placeholder resolver for a key-value pair.
    • placeholder

      public static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver placeholder(String key, Component component)
      Creates a placeholder resolver for a key-component pair.
    • resolvers

      public static net.kyori.adventure.text.minimessage.tag.resolver.TagResolver resolvers(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver... resolvers)
      Combines multiple tag resolvers into one.
    • miniMessage

      public static net.kyori.adventure.text.minimessage.MiniMessage miniMessage()
      Returns the shared MiniMessage instance.
    • MINI_MESSAGE

      public static net.kyori.adventure.text.minimessage.MiniMessage MINI_MESSAGE()