@Override
  public @NotNull Component render(final @NotNull Component component, final @NotNull State state) {
    if (!state.running) return component;
    final boolean prevFirstMatch = state.firstMatch;
    state.firstMatch = true;

    final List<Component> oldChildren = component.children();
    final int oldChildrenSize = oldChildren.size();
    Style oldStyle = component.style();
    List<Component> children = null;
    Component modified = component;
    // replace the component itself
    if (component instanceof TextComponent) {
      final String content = ((TextComponent) component).content();
      final Matcher matcher = state.pattern.matcher(content);
      int replacedUntil = 0; // last index handled
      while (matcher.find()) {
        final PatternReplacementResult result = state.continuer.shouldReplace(matcher, ++state.matchCount, state.replaceCount);
        if (result == PatternReplacementResult.CONTINUE) {
          // ignore this replacement
          continue;
        } else if (result == PatternReplacementResult.STOP) {
          // end replacement
          state.running = false;
          break;
        }

        if (matcher.start() == 0) {
          // if we're a full match, modify the component directly
          if (matcher.end() == content.length()) {
            final ComponentLike replacement = state.replacement.apply(matcher, Component.text().content(matcher.group())
              .style(component.style()));

            modified = replacement == null ? Component.empty() : replacement.asComponent();

            if (modified.style().hoverEvent() != null) {
              oldStyle = oldStyle.hoverEvent(null); // Remove original hover if it has been replaced completely
            }

            // merge style of the match into this component to prevent unexpected loss of style
            modified = modified.style(modified.style().merge(component.style(), Style.Merge.Strategy.IF_ABSENT_ON_TARGET));

            if (children == null) { // Prepare children
              children = new ArrayList<>(oldChildrenSize + modified.children().size());
              children.addAll(modified.children());
            }
          } else {
            // otherwise, work on a child of the root node
            modified = Component.text("", component.style());
            final ComponentLike child = state.replacement.apply(matcher, Component.text().content(matcher.group()));
            if (child != null) {
              if (children == null) {
                children = new ArrayList<>(oldChildrenSize + 1);
              }
              children.add(child.asComponent());
            }
          }
        } else {
          if (children == null) {
            children = new ArrayList<>(oldChildrenSize + 2);
          }
          if (state.firstMatch) {
            // truncate parent to content before match
            modified = ((TextComponent) component).content(content.substring(0, matcher.start()));
          } else if (replacedUntil < matcher.start()) {
            children.add(Component.text(content.substring(replacedUntil, matcher.start())));
          }
          final ComponentLike builder = state.replacement.apply(matcher, Component.text().content(matcher.group()));
          if (builder != null) {
            children.add(builder.asComponent());
          }
        }
        state.replaceCount++;
        state.firstMatch = false;
        replacedUntil = matcher.end();
      }
      if (replacedUntil < content.length()) {
        // append trailing content
        if (replacedUntil > 0) {
          if (children == null) {
            children = new ArrayList<>(oldChildrenSize);
          }
          children.add(Component.text(content.substring(replacedUntil)));
        }
        // otherwise, we haven't modified the component, so nothing to change
      }
    } else if (modified instanceof TranslatableComponent) { // get TranslatableComponent with() args
      final List<TranslationArgument> args = ((TranslatableComponent) modified).arguments();
      List<TranslationArgument> newArgs = null;
      for (int i = 0, size = args.size(); i < size; i++) {
        final TranslationArgument original = args.get(i);
        final TranslationArgument replaced = original.value() instanceof Component ? TranslationArgument.component(this.render((Component) original.value(), state)) : original;
        if (replaced != original) {
          if (newArgs == null) {
            newArgs = new ArrayList<>(size);
            if (i > 0) {
              newArgs.addAll(args.subList(0, i));
            }
          }
        }
        if (newArgs != null) {
          newArgs.add(replaced);
        }
      }
      if (newArgs != null) {
        modified = ((TranslatableComponent) modified).arguments(newArgs);
      }
    }
    // Only visit children if we're running
    if (state.running) {
      // hover event
      if (state.replaceInsideHoverEvents) {
        final HoverEvent<?> event = oldStyle.hoverEvent();
        if (event != null) {
          final HoverEvent<?> rendered = event.withRenderedValue(this, state);
          if (event != rendered) {
            modified = modified.style(s -> s.hoverEvent(rendered));
          }
        }
      }
      // Children
      boolean first = true;
      for (int i = 0; i < oldChildrenSize; i++) {
        final Component child = oldChildren.get(i);
        final Component replaced = this.render(child, state);
        if (replaced != child) {
          if (children == null) {
            children = new ArrayList<>(oldChildrenSize);
          }
          if (first) {
            children.addAll(oldChildren.subList(0, i));
          }
          first = false;
        }
        if (children != null) {
          children.add(replaced);
          first = false;
        }
      }
    } else {
      // we're not visiting children, re-add original children if necessary
      if (children != null) {
        children.addAll(oldChildren);
      }
    }

    state.firstMatch = prevFirstMatch;
    // Update the modified component with new children
    if (children != null) {
      return modified.children(children);
    }
    return modified;
  }
A renderer performing a replacement on every {@link TextComponent} element of a component tree.
api/src/main/java/net/kyori/adventure/text/TextReplacementRenderer.java