Interface ComponentReplacer

Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ComponentReplacer
Functional interface similar to a Function, which takes a Component as an input and can change/remove the component based on what is returned.
Component replacers can be used by ComponentTree and IReplaceable.replace(ComponentReplacer).

This interface also provides static factories to help you use the most common replacers.

  • Method Details

    • apply

      @Nullable Component apply(@Nonnull Component oldComponent)
      Attempts to replace or remove the given component.

      If this method returns the same component and contains children, then this replacer will be applied recursively; otherwise, the component is not replaced.

      The returned component must be compatible with the source (a ActionRow or a Container for example) it originated from.

      Parameters:
      oldComponent - The component which is attempted to be replaced
      Returns:
      A new, compatible component, the same component, or null to remove the component.
      Throws:
      IllegalArgumentException - If null is passed
    • all

      @Nonnull static ComponentReplacer all(@Nonnull Collection<? extends ComponentReplacer> replacers)
      Creates a new ComponentReplacer combining the provided replacers.

      Each replacer will run one after the other, if one returns a new component, the next replacer will still run against it.
      However, if a replacer returns null, thus removing the component, it will stop.

      Parameters:
      replacers - The replacers to combine
      Returns:
      A ComponentReplacer running all the provided replacers
      Throws:
      IllegalArgumentException - If the collection is empty or null is passed
    • all

      @Nonnull static ComponentReplacer all(@Nonnull ComponentReplacer first, @Nonnull ComponentReplacer... others)
      Creates a new ComponentReplacer combining the provided replacers.

      Each replacer will run one after the other, if one returns a new component, the next replacer will still run against it.
      However, if a replacer returns null, thus removing the component, it will stop.

      Parameters:
      first - The first replacer
      others - Additional replacers
      Returns:
      A ComponentReplacer running all the provided replacers
      Throws:
      IllegalArgumentException - If null is passed
    • of

      @Nonnull static <T extends Component> ComponentReplacer of(@Nonnull Class<? super T> type, @Nonnull Predicate<? super T> filter, @Nonnull Function<? super T, Component> update)
      Creates a ComponentReplacer which recursively iterates on components of the given type, while running the update function that satisfy the provided filter.

      The provided update function can return null to remove the component.

      Parameters:
      type - The type of component which should be attempted to be replaced
      filter - The filter to match against
      update - The replacement function, can return null
      Returns:
      A ComponentReplacer with the provided functions
      Throws:
      IllegalArgumentException - If null is passed
    • byUniqueId

      @Nonnull static ComponentReplacer byUniqueId(@Nonnull Component oldComponent, @Nullable Component newComponent)
      Creates a ComponentReplacer which replaces a given component with another, based on their numeric ID.
      Parameters:
      oldComponent - The component to replace
      newComponent - The component to replace with, null to remove the component
      Returns:
      A ComponentReplacer replacing the old component with the new one
      Throws:
      IllegalArgumentException - If oldComponent is null
    • byUniqueId

      @Nonnull static ComponentReplacer byUniqueId(int id, @Nullable Component newComponent)
      Creates a ComponentReplacer which replaces a given component with another, based on their numeric ID.
      Parameters:
      id - The ID of the component to replace
      newComponent - The component to replace with, null to remove the component
      Returns:
      A ComponentReplacer replacing the old component with the new one
    • byUniqueId

      @Nonnull static ComponentReplacer byUniqueId(int id, @Nonnull Function<? super Component, Component> update)
      Creates a ComponentReplacer which replaces a given component with another, based on their numeric ID.

      The provided update function can return null to remove the component.

      Parameters:
      id - The ID of the component to replace
      update - The replacement function, can return null
      Returns:
      A ComponentReplacer replacing the old component with the new one
      Throws:
      IllegalArgumentException - If null is passed