Skip to content

Commit 972b178

Browse files
committed
chore: minor nitpicks over new json module
1 parent 2d14cdc commit 972b178

File tree

8 files changed

+95
-60
lines changed

8 files changed

+95
-60
lines changed

text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/GsonComponentSerializer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ interface Builder extends AbstractBuilder<GsonComponentSerializer>, Buildable.Bu
128128
* @return this builder
129129
* @since 4.0.0
130130
*/
131+
@Override
131132
@NotNull Builder downsampleColors();
132133

133134
/**
@@ -153,6 +154,7 @@ interface Builder extends AbstractBuilder<GsonComponentSerializer>, Buildable.Bu
153154
*
154155
* @since 4.0.0
155156
*/
157+
@Override
156158
@NotNull Builder emitLegacyHoverEvent();
157159

158160
/**

text-serializer-gson/src/main/java/net/kyori/adventure/text/serializer/gson/JSONComponentSerializerProviderImpl.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,17 @@
3737
@ApiStatus.Internal
3838
public final class JSONComponentSerializerProviderImpl implements JSONComponentSerializer.Provider, Services.Fallback {
3939
@Override
40-
public @NotNull JSONComponentSerializer json() {
40+
public @NotNull JSONComponentSerializer instance() {
4141
return GsonComponentSerializer.gson();
4242
}
4343

4444
@Override
4545
public @NotNull Supplier<JSONComponentSerializer.@NotNull Builder> builder() {
4646
return GsonComponentSerializer::builder;
4747
}
48+
49+
@Override
50+
public String toString() {
51+
return "JSONComponentSerializerProviderImpl[GsonComponentSerializer]";
52+
}
4853
}

text-serializer-json-legacy-impl/src/main/java/net/kyori/adventure/text/serializer/json/legacyimpl/NBTLegacyHoverEventSerializerImpl.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,6 @@ private NBTLegacyHoverEventSerializerImpl() {
6565
);
6666
}
6767

68-
@Override
69-
public HoverEvent.@NotNull ShowEntity deserializeShowEntity(final @NotNull Component input, final Codec.Decoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
70-
assertTextComponent(input);
71-
final CompoundBinaryTag contents = SNBT_CODEC.decode(((TextComponent) input).content());
72-
return HoverEvent.ShowEntity.showEntity(
73-
Key.key(contents.getString(ENTITY_TYPE)),
74-
UUID.fromString(contents.getString(ENTITY_ID)),
75-
componentCodec.decode(contents.getString(ENTITY_NAME))
76-
);
77-
}
78-
79-
private static void assertTextComponent(final Component component) {
80-
if (!(component instanceof TextComponent) || !component.children().isEmpty()) {
81-
throw new IllegalArgumentException("Legacy events must be single Component instances");
82-
}
83-
}
84-
8568
@Override
8669
public @NotNull Component serializeShowItem(final HoverEvent.@NotNull ShowItem input) throws IOException {
8770
final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
@@ -94,6 +77,17 @@ private static void assertTextComponent(final Component component) {
9477
return Component.text(SNBT_CODEC.encode(builder.build()));
9578
}
9679

80+
@Override
81+
public HoverEvent.@NotNull ShowEntity deserializeShowEntity(final @NotNull Component input, final Codec.Decoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
82+
assertTextComponent(input);
83+
final CompoundBinaryTag contents = SNBT_CODEC.decode(((TextComponent) input).content());
84+
return HoverEvent.ShowEntity.showEntity(
85+
Key.key(contents.getString(ENTITY_TYPE)),
86+
UUID.fromString(contents.getString(ENTITY_ID)),
87+
componentCodec.decode(contents.getString(ENTITY_NAME))
88+
);
89+
}
90+
9791
@Override
9892
public @NotNull Component serializeShowEntity(final HoverEvent.@NotNull ShowEntity input, final Codec.Encoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
9993
final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
@@ -105,4 +99,10 @@ private static void assertTextComponent(final Component component) {
10599
}
106100
return Component.text(SNBT_CODEC.encode(builder.build()));
107101
}
102+
103+
private static void assertTextComponent(final Component component) {
104+
if (!(component instanceof TextComponent) || !component.children().isEmpty()) {
105+
throw new IllegalArgumentException("Legacy events must be single Component instances");
106+
}
107+
}
108108
}
Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,12 @@
2323
*/
2424
package net.kyori.adventure.text.serializer.json;
2525

26-
import java.util.Optional;
27-
import java.util.function.Supplier;
2826
import net.kyori.adventure.text.Component;
29-
import net.kyori.adventure.util.Services;
3027
import org.jetbrains.annotations.NotNull;
3128
import org.jetbrains.annotations.Nullable;
3229

33-
final class JSONComponentSerializerImpl implements JSONComponentSerializer {
34-
private static final JSONComponentSerializer MISSING_INSTANCE = new JSONComponentSerializerImpl();
35-
private static final Optional<Provider> SERVICE = Services.serviceWithFallback(Provider.class);
30+
final class DummyJSONComponentSerializer implements JSONComponentSerializer {
31+
static final JSONComponentSerializer INSTANCE = new DummyJSONComponentSerializer();
3632
private static final String UNSUPPORTED_MESSAGE =
3733
"No JsonComponentSerializer implementation found\n" +
3834
"\n" +
@@ -49,20 +45,8 @@ final class JSONComponentSerializerImpl implements JSONComponentSerializer {
4945
throw new UnsupportedOperationException(UNSUPPORTED_MESSAGE);
5046
}
5147

52-
// We cannot store these fields in JsonComponentSerializerImpl directly due to class initialisation issues.
53-
static final class Instances {
54-
static final JSONComponentSerializer INSTANCE = SERVICE
55-
.map(Provider::json)
56-
.orElse(MISSING_INSTANCE);
57-
58-
static final Supplier<Builder> BUILDER_SUPPLIER = SERVICE
59-
.map(Provider::builder)
60-
.orElse(BuilderImpl::new);
61-
}
62-
6348
// A no-op builder that just returns the unsupported instance.
6449
static final class BuilderImpl implements Builder {
65-
6650
@Override
6751
public @NotNull Builder downsampleColors() {
6852
return this;
@@ -80,7 +64,7 @@ static final class BuilderImpl implements Builder {
8064

8165
@Override
8266
public JSONComponentSerializer build() {
83-
return MISSING_INSTANCE;
67+
return INSTANCE;
8468
}
8569
}
8670
}

text-serializer-json/src/main/java/net/kyori/adventure/text/serializer/json/JSONComponentConstants.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@
3232
*/
3333
@ApiStatus.Internal
3434
public final class JSONComponentConstants {
35-
36-
private JSONComponentConstants() {
37-
throw new IllegalStateException("Cannot instantiate");
38-
}
39-
4035
public static final String TEXT = "text";
4136
public static final String TRANSLATE = "translate";
4237
public static final String TRANSLATE_FALLBACK = "fallback";
@@ -70,4 +65,8 @@ private JSONComponentConstants() {
7065
public static final String SHOW_ITEM_ID = "id";
7166
public static final String SHOW_ITEM_COUNT = "count";
7267
public static final String SHOW_ITEM_TAG = "tag";
68+
69+
private JSONComponentConstants() {
70+
throw new IllegalStateException("Cannot instantiate");
71+
}
7372
}

text-serializer-json/src/main/java/net/kyori/adventure/text/serializer/json/JSONComponentSerializer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public interface JSONComponentSerializer extends ComponentSerializer<Component,
4747
* @since 4.14.0
4848
*/
4949
static @NotNull JSONComponentSerializer json() {
50-
return JSONComponentSerializerImpl.Instances.INSTANCE;
50+
return JSONComponentSerializerAccessor.Instances.INSTANCE;
5151
}
5252

5353
/**
@@ -57,7 +57,7 @@ public interface JSONComponentSerializer extends ComponentSerializer<Component,
5757
* @since 4.14.0
5858
*/
5959
static JSONComponentSerializer.@NotNull Builder builder() {
60-
return JSONComponentSerializerImpl.Instances.BUILDER_SUPPLIER.get();
60+
return JSONComponentSerializerAccessor.Instances.BUILDER_SUPPLIER.get();
6161
}
6262

6363
/**
@@ -121,7 +121,7 @@ interface Provider {
121121
*/
122122
@ApiStatus.Internal
123123
@PlatformAPI
124-
@NotNull JSONComponentSerializer json();
124+
@NotNull JSONComponentSerializer instance();
125125

126126
/**
127127
* Provide a supplier for builder builders of {@link JSONComponentSerializer} instances.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* This file is part of adventure, licensed under the MIT License.
3+
*
4+
* Copyright (c) 2017-2023 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.serializer.json;
25+
26+
import java.util.Optional;
27+
import java.util.function.Supplier;
28+
import net.kyori.adventure.util.Services;
29+
30+
final class JSONComponentSerializerAccessor {
31+
private static final Optional<JSONComponentSerializer.Provider> SERVICE = Services.serviceWithFallback(JSONComponentSerializer.Provider.class);
32+
33+
private JSONComponentSerializerAccessor() {
34+
}
35+
36+
static final class Instances {
37+
static final JSONComponentSerializer INSTANCE = SERVICE
38+
.map(JSONComponentSerializer.Provider::instance)
39+
.orElse(DummyJSONComponentSerializer.INSTANCE);
40+
41+
static final Supplier<JSONComponentSerializer.Builder> BUILDER_SUPPLIER = SERVICE
42+
.map(JSONComponentSerializer.Provider::builder)
43+
.orElse(DummyJSONComponentSerializer.BuilderImpl::new);
44+
}
45+
}

text-serializer-json/src/main/java/net/kyori/adventure/text/serializer/json/LegacyHoverEventSerializer.java

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Adapter to convert between modern and legacy hover event formats.
3434
*
35-
* @since 4.0.0
35+
* @since 4.14.0
3636
*/
3737
public interface LegacyHoverEventSerializer {
3838
/**
@@ -41,39 +41,39 @@ public interface LegacyHoverEventSerializer {
4141
* @param input component whose plain-text value is a SNBT string
4242
* @return the deserialized event
4343
* @throws IOException if the input is improperly formatted
44-
* @since 4.0.0
44+
* @since 4.14.0
4545
*/
4646
HoverEvent.@NotNull ShowItem deserializeShowItem(@NotNull Component input) throws IOException;
4747

4848
/**
49-
* Convert a legacy hover event {@code show_entity} value to its modern format.
49+
* Convert a modern hover event {@code show_item} value to its legacy format.
5050
*
51-
* @param input component whose plain-text value is a SNBT string
52-
* @param componentDecoder A decoder that can take a JSON string and return a deserialized component
53-
* @return the deserialized event
51+
* @param input modern hover event
52+
* @return component with the legacy value as a SNBT string
5453
* @throws IOException if the input is improperly formatted
55-
* @since 4.0.0
54+
* @since 4.14.0
5655
*/
57-
HoverEvent.@NotNull ShowEntity deserializeShowEntity(@NotNull Component input, Codec.Decoder<Component, String, ? extends RuntimeException> componentDecoder) throws IOException;
56+
@NotNull Component serializeShowItem(HoverEvent.@NotNull ShowItem input) throws IOException;
5857

5958
/**
60-
* Convert a modern hover event {@code show_item} value to its legacy format.
59+
* Convert a legacy hover event {@code show_entity} value to its modern format.
6160
*
62-
* @param input modern hover event
63-
* @return component with the legacy value as a SNBT string
61+
* @param input component whose plain-text value is a SNBT string
62+
* @param componentDecoder A decoder that can take a JSON string and return a deserialized component
63+
* @return the deserialized event
6464
* @throws IOException if the input is improperly formatted
65-
* @since 4.0.0
65+
* @since 4.14.0
6666
*/
67-
@NotNull Component serializeShowItem(HoverEvent.@NotNull ShowItem input) throws IOException;
67+
HoverEvent.@NotNull ShowEntity deserializeShowEntity(@NotNull Component input, Codec.Decoder<Component, String, ? extends RuntimeException> componentDecoder) throws IOException;
6868

6969
/**
7070
* Convert a modern hover event {@code show_entity} value to its legacy format.
7171
*
72-
* @param input modern hover event
72+
* @param input modern hover event
7373
* @param componentEncoder An encoder that can take a {@link Component} and return a JSON string
7474
* @return component with the legacy value as a SNBT string
7575
* @throws IOException if the input is improperly formatted
76-
* @since 4.0.0
76+
* @since 4.14.0
7777
*/
7878
@NotNull Component serializeShowEntity(HoverEvent.@NotNull ShowEntity input, Codec.Encoder<Component, String, ? extends RuntimeException> componentEncoder) throws IOException;
7979
}

0 commit comments

Comments
 (0)