java.lang.Object
com.winterhavenmc.library.messagebuilder.pipeline.replacer.MacroReplacer
All Implemented Interfaces:
Replacer

public class MacroReplacer extends Object implements Replacer
Default implementation of the Replacer interface that performs macro substitution on input strings using values derived from a MacroObjectMap.

This replacer extracts macros from the message using configurable regular expressions, then delegates resolution of each macro to a Resolver. Once resolved, macros are replaced in the string using their corresponding values.

The class supports two levels of macro detection:

Unresolvable macros are left intact in the output string, ensuring safe and fail-tolerant rendering behavior.

See Also:
  • Field Details

    • FULL_KEY_PATTERN

      public static final Pattern FULL_KEY_PATTERN
      Pattern used to match fully qualified macro placeholders (e.g., %ITEM.NAME_SINGULAR%) for the purpose of final string replacement.
    • BASE_KEY_PATTERN

      public static final Pattern BASE_KEY_PATTERN
      Pattern used to match base macro keys (e.g., [ITEM}) for macro resolution via the Resolver.

      This pattern determines which objects should be passed to the resolver to populate values for all matching derived keys.

  • Constructor Details

    • MacroReplacer

      public MacroReplacer(Resolver resolver, Matcher placeholderMatcher)
      Constructs a MacroReplacer with the given resolver and placeholder matcher.
      Parameters:
      resolver - the macro resolver responsible for producing string values for each macro
      placeholderMatcher - the matcher used to identify macro patterns in message strings
  • Method Details

    • replace

      public String replace(MacroObjectMap macroObjectMap, String messageString)
      Replaces all macro placeholders found in the given message string using the provided MacroObjectMap.

      The replacement process is performed in two stages:

      1. Extract base macro keys from the string using BASE_KEY_PATTERN
      2. Resolve and populate a MacroStringMap with string values
      3. Substitute each matching placeholder using FULL_KEY_PATTERN

      If a macro cannot be resolved, it is left in-place as its original placeholder form.

      Specified by:
      replace in interface Replacer
      Parameters:
      macroObjectMap - a map of macro keys to context objects used for resolution
      messageString - the input string containing macro placeholders (may be null)
      Returns:
      the final formatted message with macros replaced