Interface ChatFormat<T>
- Type Parameters:
T- the type produced byaccept(Player, String), typicallyChatComponent
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:
matcher(String)— returns a pre-compiledMatcheragainst the format regexisFormatted(String)— quick check for whether the format is present in a stringremoveFormat(String)— strips all formatted segments from a stringaccept(String)— parses without a player contexttoFormattedString(Object)— serializes a result back to markup (optional)
- Since:
- 1.5.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classInternal cache that stores compiledPatterninstances keyed by their source regex string. -
Method Summary
Modifier and TypeMethodDescriptiondefault TParses a raw string into a typed result without player context.Parses a raw string into a typed result using the given player as context for version-aware color resolution.@NotNull StringgetRegex()Returns the regular expression used to detect and extract segments handled by this format.default booleanisFormatted(String string) Returnstruewhen this format's regex finds at least one match inside the given string.default @NotNull MatcherCreates aMatcherfor the given text using the pattern derived fromgetRegex().default StringremoveFormat(String string) Removes every occurrence of this format from the given string, leaving only the surrounding plain text.default @NotNull StringtoFormattedString(T result) Serializes a previously parsed result back to its formatted markup representation.
-
Method Details
-
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 aPattern. 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
Creates aMatcherfor the given text using the pattern derived fromgetRegex().Compiled
Patterninstances are cached inChatFormat.Holder.PATTERNSto 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
Matcherready to be used forfind()ormatches()calls
-
isFormatted
Returnstruewhen this format's regex finds at least one match inside the given string.- Parameters:
string- the text to inspect- Returns:
trueif the format is present;falseotherwise
-
removeFormat
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
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 benullfor 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
Parses a raw string into a typed result without player context.Delegates to
accept(Player, String)with anullplayer, 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
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
-