Class MessageBuilder

java.lang.Object
com.winterhavenmc.library.messagebuilder.MessageBuilder

public final class MessageBuilder extends Object
A class that implements a builder pattern for messages to be sent to a player or console.

This class should be instantiated in a plugin's onEnable method, and the build method compose is called with a CommandSender as recipient and an enum constant whose name is used to reference the message key defined in the language file. Macro replacements can then be assigned with a chained method call to the setMacro method, which can be repeated as necessary to set all the macros to be replaced in the message string. Finally, the send method is called as a final chained method call to effectuate the actual sending of the message.

If the message is configured in the language file with a repeat-delay, an entry will be added to the message cooldown map for the player / message, and a task started to remove the entry from the cooldown map after the configured repeat-delay time has elapsed.

example:

MessageBuilder messageBuilder = MessageBuilder.create(plugin)

messageBuilder.compose(recipient, MessageId.MESSAGE_KEY)
    .setMacro(Macro.PLACEHOLDER1, object)
    .setMacro(Macro.PLACEHOLDER2, replacementString)
    .send();

Note that any object may be passed as the replacement string, which will be converted using that object's toString method. Some placeholders, such as RECIPIENT, PLUGIN and SERVER are automatically populated along with any subfields in all messages and are therefore always available as message placeholders,

  • Field Details

  • Method Details

    • create

      public static MessageBuilder create(org.bukkit.plugin.Plugin plugin)
      A static factory method for instantiating this class. Due to the necessity of performing file I/O operations to instantiate this class, this static method has been provided to perform the potentially blocking operations before instantiation.The I/O dependencies are then injected into the constructor of the class, which is declared package-private to prevent instantiation from outside this class except by use of this static factory method. Finally, if successfully instantiated, an instance of the class will be returned. If file operations fail, the object will not be left in a partially instantiated state.
      Returns:
      an instance of this class
    • compose

      public <E extends Enum<E>> Message compose(org.bukkit.command.CommandSender recipient, E messageId)
      Initiate the message building sequence. Parameters of this method are passed into this library domain from the plugin, so robust validation is employed and type-safety is enforced by converting to domain specific types and validating on creation. If a null CommandSender is passed as the recipient parameter, an empty, no-op message is returned, as it is plausible a null or invalid value could be passed. The enum constant messageId parameter, conversely, cannot be null under normal conditions, and therefore throws a validation exception if a null is encountered.
      Parameters:
      recipient - the command sender to whom the message will be sent
      messageId - the message identifier enum constant
      Returns:
      Message an initialized message object
    • reload

      public boolean reload()
      Reload resources
    • config

      public ConfigRepository config()
      Provides external access to the config repository
    • constants

      public ConstantRepository constants()
      Provides external access to the constant repository
    • items

      public ItemRepository items()
      Provides external access to the item repository
    • sounds

      public SoundRepository sounds()
      Provides external access to the sound repository
    • worlds

      public WorldRepository worlds()
      Provides external access to the enabled worlds provider