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.

It should be instantiated in a plugin's onEnable method, and the build method is called with a CommandSender as recipient and an E enum member to reference the message 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, usually 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, except in the case of some placeholder keys that are treated specially by the doMacroReplacements method. Special keys are: ITEM or ITEM_NAME, ITEM_QUANTITY, WORLD or WORLD_NAME, PLAYER or PLAYER_NAME, LOCATION or PLAYER_LOCATION, DURATION or DURATION_MINUTES

  • 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 void reload()
      Reload messages from configured language file
    • getConstantResolver

      public ConstantResolver getConstantResolver()