Class MsgFmt

java.lang.Object
dev.ua.theroer.magicutils.utils.MsgFmt

public final class MsgFmt extends Object
Single placeholder substitution engine for the whole library.

Resolves {name} tokens in a template using:

  • a single Map<String, ?> as the only argument,
  • or a flat varargs list — interpreted positionally against the order in which placeholder names first appear in the template,
  • or an Iterable<?> / array as the only argument, treated like the flat varargs list.

If the template contains no {curly} tokens at all and arguments are supplied, falls back to String.format(String, Object...) so legacy "%s"-style format strings still work.

An optional valueTransformer runs on every replacement before substitution. The lang module passes a MiniMessage::escapeTags transformer here to neutralize tags coming from user-supplied placeholders. Keeping the transformer pluggable means platform-api stays free of a hard MiniMessage dependency.

  • Method Details

    • apply

      public static String apply(String template, Object... placeholders)
      Substitutes placeholders without value transformation.
      Parameters:
      template - message template (may contain {name} tokens)
      placeholders - Map / Iterable / array / flat varargs of values
      Returns:
      message with placeholders applied, or the original template when nothing matched
    • apply

      public static String apply(String template, Function<String,String> valueTransformer, Object... placeholders)
      Substitutes placeholders, transforming each replacement value through valueTransformer before insertion.

      Pass null (or Function.identity()) for no transformation. Pass a tag-escape function (e.g. MiniMessage::escapeTags after a String::valueOf) to make user-supplied data safe for downstream parsing.

      Parameters:
      template - message template
      valueTransformer - optional transformer applied to each replacement (after String.valueOf)
      placeholders - Map / Iterable / array / flat varargs of values
      Returns:
      message with placeholders applied
    • args

      public static Map<String,Object> args(Object... keyValuePairs)
      Convenience overload: returns a builder-style argument list from key/value pairs. Equivalent to Map.of(k1,v1,k2,v2,...) but returns a Map object that apply(String, Object...) accepts.
      Parameters:
      keyValuePairs - even-length array of key, value, ...
      Returns:
      ordered map suitable for apply(template, args(...))