Interface ChatFormat<T>

Type Parameters:
T - the type produced by accept(Player, String), typically ChatComponent

public interface ChatFormat<T>
A typed text-format contract used by MultiComponent to parse and serialize interactive component markup.

Implementations define a regular expression that identifies the markup handled by this format, and an accept method that converts a matching raw string into a fully configured ChatComponent.

The interface also provides default implementations for convenience:

Since:
1.5.0
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static final class 
    Internal cache that stores compiled Pattern instances keyed by their source regex string.
  • Method Summary

    Modifier and Type
    Method
    Description
    default T
    accept(String string)
    Parses a raw string into a typed result without player context.
    accept(org.bukkit.entity.Player player, String string)
    Parses a raw string into a typed result using the given player as context for version-aware color resolution.
    @NotNull String
    Returns the regular expression used to detect and extract segments handled by this format.
    default boolean
    Returns true when this format's regex finds at least one match inside the given string.
    default @NotNull Matcher
    matcher(String string)
    Creates a Matcher for the given text using the pattern derived from getRegex().
    default String
    Removes every occurrence of this format from the given string, leaving only the surrounding plain text.
    default @NotNull String
    Serializes a previously parsed result back to its formatted markup representation.
  • Method Details

    • getRegex

      @NotNull @NotNull String getRegex()
      Returns the regular expression used to detect and extract segments handled by this format.

      The returned string is compiled (and cached) by matcher(String) into a Pattern. Implementations should return a stable, non-null value because the pattern is cached in a shared map keyed by this string.

      Returns:
      the regex string for this format; never null
    • matcher

      @NotNull default @NotNull Matcher matcher(String string)
      Creates a Matcher for the given text using the pattern derived from getRegex().

      Compiled Pattern instances are cached in ChatFormat.Holder.PATTERNS to avoid repeated compilation overhead. The first call for a given regex compiles and stores the pattern; subsequent calls retrieve it from the cache.

      Parameters:
      string - the text to match against
      Returns:
      a Matcher ready to be used for find() or matches() calls
    • isFormatted

      default boolean isFormatted(String string)
      Returns true when this format's regex finds at least one match inside the given string.
      Parameters:
      string - the text to inspect
      Returns:
      true if the format is present; false otherwise
    • removeFormat

      default String removeFormat(String string)
      Removes every occurrence of this format from the given string, leaving only the surrounding plain text.

      The default implementation replaces each full match (including tags) with an empty string. Implementations that need to preserve inner text (e.g. strip only the outer markup but keep the content) should override this method.

      Parameters:
      string - the text from which to remove formatted segments; may be blank
      Returns:
      the text without any segments matched by this format's regex
    • accept

      @NotNull T accept(org.bukkit.entity.Player player, String string)
      Parses a raw string into a typed result using the given player as context for version-aware color resolution.

      When the string matches this format, the returned result should carry the interactive events (click, hover, etc.) extracted from the markup. When no match is found, implementations typically return a plain component wrapping the original text.

      Parameters:
      player - the player who will receive the resulting component; used to resolve color capabilities. May be null for a conservative legacy fallback.
      string - the raw text to parse, which may or may not contain format markup
      Returns:
      the parsed result; never null
    • accept

      @NotNull default T accept(String string)
      Parses a raw string into a typed result without player context.

      Delegates to accept(Player, String) with a null player, which causes the formatting pipeline to fall back to legacy-safe color output because no player capability information is available.

      Parameters:
      string - the raw text to parse
      Returns:
      the parsed result; never null
    • toFormattedString

      @NotNull default @NotNull String toFormattedString(T result)
      Serializes a previously parsed result back to its formatted markup representation.

      The default implementation throws UnsupportedOperationException. Implementations that need round-trip serialization (e.g. for persistence or network transport) should override this method.

      Parameters:
      result - the typed result to serialize; must have been produced by this format
      Returns:
      the formatted string representation of the result
      Throws:
      UnsupportedOperationException - if this format does not support serialization