Interface ComponentReplacer
- All Known Implementing Classes:
TypedComponentReplacerImpl
- 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 ComponentReplacer
all
(Collection<? extends ComponentReplacer> replacers) Creates a newComponentReplacer
combining the provided replacers.static ComponentReplacer
all
(ComponentReplacer first, ComponentReplacer... others) Creates a newComponentReplacer
combining the provided replacers.Attempts to replace or remove the given component.static ComponentReplacer
byUniqueId
(int id, Function<? super Component, Component> update) Creates aComponentReplacer
which replaces a given component with another, based on their numeric ID.static ComponentReplacer
byUniqueId
(int id, Component newComponent) Creates aComponentReplacer
which replaces a given component with another, based on their numeric ID.static ComponentReplacer
byUniqueId
(Component oldComponent, Component newComponent) Creates aComponentReplacer
which replaces a given component with another, based on their numeric ID.static <T extends Component>
ComponentReplacerCreates aComponentReplacer
which recursively iterates on components of the given type, while running theupdate
function 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
ActionRow
or aContainer
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
- Ifnull
is passed
-
all
Creates a newComponentReplacer
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 returnsnull
, 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 ornull
is passed
-
all
@Nonnull static ComponentReplacer all(@Nonnull ComponentReplacer first, @Nonnull ComponentReplacer... others) Creates a newComponentReplacer
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 returnsnull
, thus removing the component, it will stop.- Parameters:
first
- The first replacerothers
- Additional replacers- Returns:
- A
ComponentReplacer
running all the provided replacers - Throws:
IllegalArgumentException
- Ifnull
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 aComponentReplacer
which recursively iterates on components of the given type, while running theupdate
function that satisfy the provided filter.The provided
update
function can returnnull
to 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
ComponentReplacer
with the provided functions - Throws:
IllegalArgumentException
- Ifnull
is passed
-
byUniqueId
@Nonnull static ComponentReplacer byUniqueId(@Nonnull Component oldComponent, @Nullable Component newComponent) Creates aComponentReplacer
which replaces a given component with another, based on their numeric ID.- Parameters:
oldComponent
- The component to replacenewComponent
- The component to replace with,null
to remove the component- Returns:
- A
ComponentReplacer
replacing the old component with the new one - Throws:
IllegalArgumentException
- IfoldComponent
isnull
-
byUniqueId
Creates aComponentReplacer
which 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,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 aComponentReplacer
which replaces a given component with another, based on their numeric ID.The provided
update
function can returnnull
to remove the component.- Parameters:
id
- The ID of the component to replaceupdate
- The replacement function, can returnnull
- Returns:
- A
ComponentReplacer
replacing the old component with the new one - Throws:
IllegalArgumentException
- Ifnull
is passed
-