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.
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 Summary
Modifier and TypeMethodDescriptionstatic ComponentReplacerall(Collection<? extends ComponentReplacer> replacers) Creates a newComponentReplacercombining the provided replacers.static ComponentReplacerall(ComponentReplacer first, ComponentReplacer... others) Creates a newComponentReplacercombining the provided replacers.Attempts to replace or remove the given component.static ComponentReplacerbyUniqueId(int id, Function<? super Component, Component> update) Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.static ComponentReplacerbyUniqueId(int id, Component newComponent) Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.static ComponentReplacerbyUniqueId(Component oldComponent, Component newComponent) Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.static <T extends Component>
ComponentReplacerCreates aComponentReplacerwhich recursively iterates on components of the given type, while running theupdatefunction that satisfy the provided filter.
-
Method Details
-
apply
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
ActionRowor aContainerfor example) it originated from.- Parameters:
oldComponent- The component which is attempted to be replaced- Returns:
- A new, compatible component, the same component, or
nullto remove the component. - Throws:
IllegalArgumentException- Ifnullis passed
-
all
Creates a newComponentReplacercombining 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 returnsnull, thus removing the component, it will stop.- Parameters:
replacers- The replacers to combine- Returns:
- A
ComponentReplacerrunning all the provided replacers - Throws:
IllegalArgumentException- If the collection is empty ornullis passed
-
all
@Nonnull static ComponentReplacer all(@Nonnull ComponentReplacer first, @Nonnull ComponentReplacer... others) Creates a newComponentReplacercombining 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 returnsnull, thus removing the component, it will stop.- Parameters:
first- The first replacerothers- Additional replacers- Returns:
- A
ComponentReplacerrunning all the provided replacers - Throws:
IllegalArgumentException- Ifnullis 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 aComponentReplacerwhich recursively iterates on components of the given type, while running theupdatefunction that satisfy the provided filter.The provided
updatefunction can returnnullto remove the component.- Parameters:
type- The type of component which should be attempted to be replacedfilter- The filter to match againstupdate- The replacement function, can returnnull- Returns:
- A
ComponentReplacerwith the provided functions - Throws:
IllegalArgumentException- Ifnullis passed
-
byUniqueId
@Nonnull static ComponentReplacer byUniqueId(@Nonnull Component oldComponent, @Nullable Component newComponent) Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.- Parameters:
oldComponent- The component to replacenewComponent- The component to replace with,nullto remove the component- Returns:
- A
ComponentReplacerreplacing the old component with the new one - Throws:
IllegalArgumentException- IfoldComponentisnull
-
byUniqueId
Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.- Parameters:
id- The ID of the component to replacenewComponent- The component to replace with,nullto remove the component- Returns:
- A
ComponentReplacerreplacing the old component with the new one
-
byUniqueId
@Nonnull static ComponentReplacer byUniqueId(int id, @Nonnull Function<? super Component, Component> update) Creates aComponentReplacerwhich replaces a given component with another, based on their numeric ID.The provided
updatefunction can returnnullto remove the component.- Parameters:
id- The ID of the component to replaceupdate- The replacement function, can returnnull- Returns:
- A
ComponentReplacerreplacing the old component with the new one - Throws:
IllegalArgumentException- Ifnullis passed
-