Skip to content

Commit 9236992

Browse files
authored
Merge pull request #1421 from PaperMC/feature/easy-object-component
feature(api): Easier way to create object components
2 parents c280cca + e2febc6 commit 9236992

6 files changed

Lines changed: 79 additions & 3 deletions

File tree

api/src/main/java/net/kyori/adventure/text/Component.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import net.kyori.adventure.text.format.TextColor;
5151
import net.kyori.adventure.text.format.TextDecoration;
5252
import net.kyori.adventure.text.object.ObjectContents;
53+
import net.kyori.adventure.text.object.ObjectContentsLike;
54+
import net.kyori.adventure.text.object.PlayerHeadObjectContents;
5355
import net.kyori.adventure.text.serializer.ComponentSerializer;
5456
import net.kyori.adventure.translation.Translatable;
5557
import net.kyori.adventure.util.ARGBLike;
@@ -561,6 +563,22 @@ static ObjectComponent object(final Consumer<? super ObjectComponent.Builder> co
561563
return AbstractBuilder.configureAndBuild(object(), consumer);
562564
}
563565

566+
/**
567+
* Creates an object component with the given contents.
568+
*
569+
* @param objectContentsLike the contents
570+
* @return an object component
571+
* @since 5.2.0
572+
*/
573+
@Contract(value = "_ -> new", pure = true)
574+
static ObjectComponent object(final ObjectContentsLike objectContentsLike) {
575+
if (requireNonNull(objectContentsLike, "objectContentsLike") instanceof PlayerHeadObjectContents.SkinSource skinSource) {
576+
return object(ObjectContents.playerHead(skinSource));
577+
} else {
578+
return object(objectContentsLike.asObjectContents());
579+
}
580+
}
581+
564582
/**
565583
* Creates an object component with the given contents.
566584
*

api/src/main/java/net/kyori/adventure/text/ObjectComponent.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
package net.kyori.adventure.text;
2525

2626
import net.kyori.adventure.text.object.ObjectContents;
27+
import net.kyori.adventure.text.object.ObjectContentsLike;
2728
import org.jspecify.annotations.Nullable;
2829

2930
/**
@@ -32,7 +33,7 @@
3233
* @since 4.25.0
3334
* @sinceMinecraft 1.21.9
3435
*/
35-
public sealed interface ObjectComponent extends ScopedComponent<ObjectComponent> permits ObjectComponentImpl {
36+
public sealed interface ObjectComponent extends ScopedComponent<ObjectComponent>, ObjectContentsLike permits ObjectComponentImpl {
3637
/**
3738
* Gets the contents of this object component.
3839
*

api/src/main/java/net/kyori/adventure/text/ObjectComponentImpl.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public ObjectComponent style(final Style style) {
6666
return create(this.children, style, this.contents, this.fallback);
6767
}
6868

69+
@Override
70+
public ObjectContents asObjectContents() {
71+
return this.contents;
72+
}
73+
6974
static final class BuilderImpl extends AbstractComponentBuilder<ObjectComponent, Builder> implements Builder {
7075
private @Nullable ObjectContents objectContents; // Not nullable for built type, it errors in builder.
7176
private @Nullable ComponentLike fallback;

api/src/main/java/net/kyori/adventure/text/object/ObjectContents.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
* @since 4.25.0
3737
* @sinceMinecraft 1.21.9
3838
*/
39-
public sealed interface ObjectContents permits SpriteObjectContents, PlayerHeadObjectContents {
39+
public sealed interface ObjectContents extends ObjectContentsLike permits SpriteObjectContents, PlayerHeadObjectContents {
4040
/**
4141
* Creates a sprite contents with the given atlas and sprite.
4242
*
@@ -108,4 +108,9 @@ static PlayerHeadObjectContents playerHead(final UUID id) {
108108
static PlayerHeadObjectContents playerHead(final PlayerHeadObjectContents.SkinSource skinSource) {
109109
return playerHead().skin(skinSource).build();
110110
}
111+
112+
@Override
113+
default ObjectContents asObjectContents() {
114+
return this;
115+
}
111116
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of adventure, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2017-2025 KyoriPowered
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package net.kyori.adventure.text.object;
25+
26+
import org.jetbrains.annotations.Contract;
27+
28+
/**
29+
* Something that can be represented as an {@link ObjectContents}.
30+
*
31+
* @since 5.2.0
32+
*/
33+
public interface ObjectContentsLike {
34+
/**
35+
* Gets an {@link ObjectContents} representation.
36+
*
37+
* @return the object contents
38+
* @since 5.2.0
39+
*/
40+
@Contract(pure = true)
41+
ObjectContents asObjectContents();
42+
}

api/src/main/java/net/kyori/adventure/text/object/PlayerHeadObjectContents.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ sealed interface Builder permits PlayerHeadObjectContentsImpl.BuilderImpl {
294294
* @see ObjectContents#playerHead(SkinSource)
295295
* @since 4.25.0
296296
*/
297-
interface SkinSource {
297+
interface SkinSource extends ObjectContentsLike {
298298
/**
299299
* Applies this skin source to the given player head contents builder.
300300
*
@@ -305,5 +305,10 @@ interface SkinSource {
305305
@PlatformAPI
306306
@ApiStatus.Internal
307307
void applySkinToPlayerHeadContents(Builder builder);
308+
309+
@Override
310+
default ObjectContents asObjectContents() {
311+
return ObjectContents.playerHead(this);
312+
}
308313
}
309314
}

0 commit comments

Comments
 (0)