-
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #842 from KyoriPowered/feature/virtual-component-h…
…olders feat(api): virtual components
- Loading branch information
Showing
18 changed files
with
1,141 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
api/src/main/java/net/kyori/adventure/text/VirtualComponent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* This file is part of adventure, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2017-2024 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.adventure.text; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
|
||
/** | ||
* A virtual component. | ||
* | ||
* <p>This component type is transient, and not guaranteed to survive during any sort of transformations or serialization.</p> | ||
* | ||
* @since 4.18.0 | ||
*/ | ||
public interface VirtualComponent extends TextComponent { | ||
/** | ||
* Gets the renderer context type. | ||
* | ||
* @return the renderer context type | ||
* @since 4.18.0 | ||
*/ | ||
@NotNull Class<?> contextType(); | ||
|
||
/** | ||
* Gets the renderer. | ||
* | ||
* @return the renderer | ||
* @since 4.18.0 | ||
*/ | ||
@NotNull VirtualComponentRenderer<?> renderer(); | ||
} |
91 changes: 91 additions & 0 deletions
91
api/src/main/java/net/kyori/adventure/text/VirtualComponentImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* This file is part of adventure, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2017-2024 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.adventure.text; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import net.kyori.adventure.text.format.Style; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
final class VirtualComponentImpl<C> extends TextComponentImpl implements VirtualComponent { | ||
static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer) { | ||
return createVirtual(contextType, renderer, Collections.emptyList(), Style.empty()); | ||
} | ||
|
||
static <C> VirtualComponent createVirtual(final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer, final List<? extends ComponentLike> children, final Style style) { | ||
final List<Component> filteredChildren = ComponentLike.asComponents(children, IS_NOT_EMPTY); | ||
|
||
return new VirtualComponentImpl<>(filteredChildren, style, "", contextType, renderer); | ||
} | ||
|
||
private final Class<C> contextType; | ||
private final VirtualComponentRenderer<C> renderer; | ||
|
||
private VirtualComponentImpl(final @NotNull List<Component> children, final @NotNull Style style, final @NotNull String content, final @NotNull Class<C> contextType, final @NotNull VirtualComponentRenderer<C> renderer) { | ||
super(children, style, content); | ||
this.contextType = contextType; | ||
this.renderer = renderer; | ||
} | ||
|
||
@Override | ||
VirtualComponent create0(final @NotNull List<? extends ComponentLike> children, final @NotNull Style style, final @NotNull String content) { | ||
return new VirtualComponentImpl<>(ComponentLike.asComponents(children, IS_NOT_EMPTY), style, content, this.contextType, this.renderer); | ||
} | ||
|
||
@Override | ||
public @NotNull Class<C> contextType() { | ||
return this.contextType; | ||
} | ||
|
||
@Override | ||
public @NotNull VirtualComponentRenderer<C> renderer() { | ||
return this.renderer; | ||
} | ||
|
||
@Override | ||
public @NotNull String content() { | ||
return this.renderer.fallbackString(); | ||
} | ||
|
||
@Override | ||
public @NotNull Builder toBuilder() { | ||
return new BuilderImpl<>(this); | ||
} | ||
|
||
static final class BuilderImpl<C> extends TextComponentImpl.BuilderImpl { | ||
private final Class<C> contextType; | ||
private final VirtualComponentRenderer<C> renderer; | ||
|
||
BuilderImpl(final VirtualComponentImpl<C> other) { | ||
super(other); | ||
this.contextType = other.contextType(); | ||
this.renderer = other.renderer(); | ||
} | ||
|
||
@Override | ||
public @NotNull TextComponent build() { | ||
return createVirtual(this.contextType, this.renderer, this.children, this.buildStyle()); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
api/src/main/java/net/kyori/adventure/text/VirtualComponentRenderer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* This file is part of adventure, licensed under the MIT License. | ||
* | ||
* Copyright (c) 2017-2024 KyoriPowered | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
* SOFTWARE. | ||
*/ | ||
package net.kyori.adventure.text; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.UnknownNullability; | ||
|
||
/** | ||
* A holder for a value. | ||
* | ||
* @param <C> the context type | ||
* @since 4.18.0 | ||
*/ | ||
public interface VirtualComponentRenderer<C> { | ||
/** | ||
* Gets the value by rendering using {@code context}. | ||
* | ||
* @param context the context | ||
* @return the rendered value | ||
* @since 4.18.0 | ||
*/ | ||
@UnknownNullability ComponentLike apply(final @NotNull C context); | ||
|
||
/** | ||
* Get a fallback value for when this component has been serialized without being rendered. | ||
* | ||
* <p>By default, this will be an empty string.</p> | ||
* | ||
* @return the fallback string | ||
* @since 4.18.0 | ||
*/ | ||
default @NotNull String fallbackString() { | ||
return ""; | ||
} | ||
} |
Oops, something went wrong.