Class TextComponentParser

java.lang.Object
org.skriptlang.skript.bukkit.text.TextComponentParser

public final class TextComponentParser extends Object
Used for parsing Strings as Components. This class makes use of two parses: a safe parser and an unsafe parser.
The safe parser is intended to only parse tags that would be okay to process for any message, regardless of the source. This includes simple component features such as colors and text decorations.
The unsafe parser parses all tags, regardless of any potential risk (e.g. components that can run commands). Typically, users have to opt-in (in some manner) for this parser to be used.
  • Method Details

    • instance

      public static TextComponentParser instance()
      Returns:
      The global parser instance used by Skript.
    • linkParseMode

      public TextComponentParser.LinkParseMode linkParseMode()
      Returns:
      The link parse mode for this parser, which describes how potential links should be treated.
    • linkParseMode

      public void linkParseMode(TextComponentParser.LinkParseMode linkParseMode)
      Sets the link parse mode for this parser, which describes how potential links should be treated.
      Parameters:
      linkParseMode - The link parse mode to use.
    • colorsCauseReset

      public boolean colorsCauseReset()
      Returns:
      Whether color codes cause a reset of existing formatting. Essentially, this setting controls whether all color tags should be prepended with a <reset> tag.
      See Also:
      • ParserDirective.RESET
    • colorsCauseReset

      public void colorsCauseReset(boolean colorsCauseReset)
      Sets whether color codes cause a reset of existing formatting. Essentially, this setting controls whether all color tags should be prepended with a <reset> tag.
      Parameters:
      colorsCauseReset - Whether color codes should cause a reset.
      See Also:
      • ParserDirective.RESET
    • registerPlaceholder

      public void registerPlaceholder(String name, net.kyori.adventure.text.minimessage.tag.Tag result)
      Registers a simple key-value placeholder with Skript's unsafe message parser.
      Parameters:
      name - The name/key of the placeholder.
      result - The result/value of the placeholder.
    • registerResettingPlaceholder

      public void registerResettingPlaceholder(String name, net.kyori.adventure.text.minimessage.tag.Tag result)
      Registers a simple key-value placeholder with Skript's unsafe message parser. The registered placeholder will instruct the parser to reset existing formatting before applying the tag if colorsCauseReset() is true.
      Parameters:
      name - The name/key of the placeholder.
      result - The result/value of the placeholder.
    • registerSafePlaceholder

      public void registerSafePlaceholder(String name, net.kyori.adventure.text.minimessage.tag.Tag result)
      Registers a simple key-value placeholder with Skript's safe and unsafe message parsers.
      Parameters:
      name - The name/key of the placeholder.
      result - The result/value of the placeholder.
    • registerSafeResettingPlaceholder

      public void registerSafeResettingPlaceholder(String name, net.kyori.adventure.text.minimessage.tag.Tag result)
      Registers a simple key-value placeholder with Skript's safe and unsafe message parsers. The registered placeholder will instruct the parser to reset existing formatting before applying the tag if colorsCauseReset() is true.
      Parameters:
      name - The name/key of the placeholder.
      result - The result/value of the placeholder.
    • unregisterPlaceholder

      public void unregisterPlaceholder(String tag)
      Unregisters a simple key-value placeholder from Skript's message parsers.
      Parameters:
      tag - The name of the placeholder to unregister.
    • registerResolver

      public void registerResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver resolver)
      Registers a TagResolver with Skript's unsafe message parser.
      Parameters:
      resolver - The TagResolver to register.
    • registerSafeResolver

      public void registerSafeResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver resolver)
      Registers a TagResolver with Skript's safe and unsafe message parsers.
      Parameters:
      resolver - The TagResolver to register.
    • unregisterResolver

      public void unregisterResolver(net.kyori.adventure.text.minimessage.tag.resolver.TagResolver resolver)
      Unregisters a TagResolver from Skript's message parsers.
      Parameters:
      resolver - The TagResolver to unregister.
    • parse

      public net.kyori.adventure.text.Component parse(Object message)
      Parses a string using the safe and unsafe parsers.
      Parameters:
      message - The message to parse.
      Returns:
      A component from the parsed message.
    • parseSafe

      public net.kyori.adventure.text.Component parseSafe(Object message)
      Parses a string using the safe parser. Only safe tags, such as color and decoration, will be parsed.
      Parameters:
      message - The message to parse.
      Returns:
      A component from the parsed message.
    • reformatText

      public String reformatText(String text)
      Reformats user component text for maximum compatibility with MiniMessage.
      Parameters:
      text - The text to reformat.
      Returns:
      Reformatted text.
    • escape

      public String escape(String string)
      Escapes all tags known to this parser in the given string. This method will also escape legacy color codes by prepending them with a backslash.
      Parameters:
      string - The string to escape tags in.
      Returns:
      The string with tags escaped.
    • stripFormatting

      public String stripFormatting(String string)
      Strips all formatting from a string. This will handle nested tags, such as "<red<red>>".
      Parameters:
      string - The string to strip formatting from.
      Returns:
      The stripped string.
    • stripSafeFormatting

      public String stripSafeFormatting(String string)
      Strips safe formatting from a string, meaning only safe tags such as colors and decorations will be stripped. This will handle nested tags, such as "<red<red>>".
      Parameters:
      string - The string to strip formatting from.
      Returns:
      The stripped string.
    • stripFormatting

      public String stripFormatting(net.kyori.adventure.text.Component component)
      Strips all formatting from a component.
      Parameters:
      component - The component to strip formatting from.
      Returns:
      A stripped string from a component.
    • toString

      public String toString(net.kyori.adventure.text.Component component)
      Converts a component back into a formatted string.
      Parameters:
      component - The component to convert.
      Returns:
      A formatted string.
    • toLegacyString

      public String toLegacyString(net.kyori.adventure.text.Component component)
      Converts a component into a legacy formatted string using the section character (§) for formatting codes.
      Parameters:
      component - The component to convert.
      Returns:
      The legacy string.