Skip to content

Commit

Permalink
chore: minor nitpicks over new json module
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Jun 7, 2023
1 parent 2d14cdc commit 972b178
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ interface Builder extends AbstractBuilder<GsonComponentSerializer>, Buildable.Bu
* @return this builder
* @since 4.0.0
*/
@Override
@NotNull Builder downsampleColors();

/**
Expand All @@ -153,6 +154,7 @@ interface Builder extends AbstractBuilder<GsonComponentSerializer>, Buildable.Bu
*
* @since 4.0.0
*/
@Override
@NotNull Builder emitLegacyHoverEvent();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@
@ApiStatus.Internal
public final class JSONComponentSerializerProviderImpl implements JSONComponentSerializer.Provider, Services.Fallback {
@Override
public @NotNull JSONComponentSerializer json() {
public @NotNull JSONComponentSerializer instance() {
return GsonComponentSerializer.gson();
}

@Override
public @NotNull Supplier<JSONComponentSerializer.@NotNull Builder> builder() {
return GsonComponentSerializer::builder;
}

@Override
public String toString() {
return "JSONComponentSerializerProviderImpl[GsonComponentSerializer]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,6 @@ private NBTLegacyHoverEventSerializerImpl() {
);
}

@Override
public HoverEvent.@NotNull ShowEntity deserializeShowEntity(final @NotNull Component input, final Codec.Decoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
assertTextComponent(input);
final CompoundBinaryTag contents = SNBT_CODEC.decode(((TextComponent) input).content());
return HoverEvent.ShowEntity.showEntity(
Key.key(contents.getString(ENTITY_TYPE)),
UUID.fromString(contents.getString(ENTITY_ID)),
componentCodec.decode(contents.getString(ENTITY_NAME))
);
}

private static void assertTextComponent(final Component component) {
if (!(component instanceof TextComponent) || !component.children().isEmpty()) {
throw new IllegalArgumentException("Legacy events must be single Component instances");
}
}

@Override
public @NotNull Component serializeShowItem(final HoverEvent.@NotNull ShowItem input) throws IOException {
final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
Expand All @@ -94,6 +77,17 @@ private static void assertTextComponent(final Component component) {
return Component.text(SNBT_CODEC.encode(builder.build()));
}

@Override
public HoverEvent.@NotNull ShowEntity deserializeShowEntity(final @NotNull Component input, final Codec.Decoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
assertTextComponent(input);
final CompoundBinaryTag contents = SNBT_CODEC.decode(((TextComponent) input).content());
return HoverEvent.ShowEntity.showEntity(
Key.key(contents.getString(ENTITY_TYPE)),
UUID.fromString(contents.getString(ENTITY_ID)),
componentCodec.decode(contents.getString(ENTITY_NAME))
);
}

@Override
public @NotNull Component serializeShowEntity(final HoverEvent.@NotNull ShowEntity input, final Codec.Encoder<Component, String, ? extends RuntimeException> componentCodec) throws IOException {
final CompoundBinaryTag.Builder builder = CompoundBinaryTag.builder()
Expand All @@ -105,4 +99,10 @@ private static void assertTextComponent(final Component component) {
}
return Component.text(SNBT_CODEC.encode(builder.build()));
}

private static void assertTextComponent(final Component component) {
if (!(component instanceof TextComponent) || !component.children().isEmpty()) {
throw new IllegalArgumentException("Legacy events must be single Component instances");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,12 @@
*/
package net.kyori.adventure.text.serializer.json;

import java.util.Optional;
import java.util.function.Supplier;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.util.Services;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

final class JSONComponentSerializerImpl implements JSONComponentSerializer {
private static final JSONComponentSerializer MISSING_INSTANCE = new JSONComponentSerializerImpl();
private static final Optional<Provider> SERVICE = Services.serviceWithFallback(Provider.class);
final class DummyJSONComponentSerializer implements JSONComponentSerializer {
static final JSONComponentSerializer INSTANCE = new DummyJSONComponentSerializer();
private static final String UNSUPPORTED_MESSAGE =
"No JsonComponentSerializer implementation found\n" +
"\n" +
Expand All @@ -49,20 +45,8 @@ final class JSONComponentSerializerImpl implements JSONComponentSerializer {
throw new UnsupportedOperationException(UNSUPPORTED_MESSAGE);
}

// We cannot store these fields in JsonComponentSerializerImpl directly due to class initialisation issues.
static final class Instances {
static final JSONComponentSerializer INSTANCE = SERVICE
.map(Provider::json)
.orElse(MISSING_INSTANCE);

static final Supplier<Builder> BUILDER_SUPPLIER = SERVICE
.map(Provider::builder)
.orElse(BuilderImpl::new);
}

// A no-op builder that just returns the unsupported instance.
static final class BuilderImpl implements Builder {

@Override
public @NotNull Builder downsampleColors() {
return this;
Expand All @@ -80,7 +64,7 @@ static final class BuilderImpl implements Builder {

@Override
public JSONComponentSerializer build() {
return MISSING_INSTANCE;
return INSTANCE;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@
*/
@ApiStatus.Internal
public final class JSONComponentConstants {

private JSONComponentConstants() {
throw new IllegalStateException("Cannot instantiate");
}

public static final String TEXT = "text";
public static final String TRANSLATE = "translate";
public static final String TRANSLATE_FALLBACK = "fallback";
Expand Down Expand Up @@ -70,4 +65,8 @@ private JSONComponentConstants() {
public static final String SHOW_ITEM_ID = "id";
public static final String SHOW_ITEM_COUNT = "count";
public static final String SHOW_ITEM_TAG = "tag";

private JSONComponentConstants() {
throw new IllegalStateException("Cannot instantiate");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public interface JSONComponentSerializer extends ComponentSerializer<Component,
* @since 4.14.0
*/
static @NotNull JSONComponentSerializer json() {
return JSONComponentSerializerImpl.Instances.INSTANCE;
return JSONComponentSerializerAccessor.Instances.INSTANCE;
}

/**
Expand All @@ -57,7 +57,7 @@ public interface JSONComponentSerializer extends ComponentSerializer<Component,
* @since 4.14.0
*/
static JSONComponentSerializer.@NotNull Builder builder() {
return JSONComponentSerializerImpl.Instances.BUILDER_SUPPLIER.get();
return JSONComponentSerializerAccessor.Instances.BUILDER_SUPPLIER.get();
}

/**
Expand Down Expand Up @@ -121,7 +121,7 @@ interface Provider {
*/
@ApiStatus.Internal
@PlatformAPI
@NotNull JSONComponentSerializer json();
@NotNull JSONComponentSerializer instance();

/**
* Provide a supplier for builder builders of {@link JSONComponentSerializer} instances.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2023 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.serializer.json;

import java.util.Optional;
import java.util.function.Supplier;
import net.kyori.adventure.util.Services;

final class JSONComponentSerializerAccessor {
private static final Optional<JSONComponentSerializer.Provider> SERVICE = Services.serviceWithFallback(JSONComponentSerializer.Provider.class);

private JSONComponentSerializerAccessor() {
}

static final class Instances {
static final JSONComponentSerializer INSTANCE = SERVICE
.map(JSONComponentSerializer.Provider::instance)
.orElse(DummyJSONComponentSerializer.INSTANCE);

static final Supplier<JSONComponentSerializer.Builder> BUILDER_SUPPLIER = SERVICE
.map(JSONComponentSerializer.Provider::builder)
.orElse(DummyJSONComponentSerializer.BuilderImpl::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
/**
* Adapter to convert between modern and legacy hover event formats.
*
* @since 4.0.0
* @since 4.14.0
*/
public interface LegacyHoverEventSerializer {
/**
Expand All @@ -41,39 +41,39 @@ public interface LegacyHoverEventSerializer {
* @param input component whose plain-text value is a SNBT string
* @return the deserialized event
* @throws IOException if the input is improperly formatted
* @since 4.0.0
* @since 4.14.0
*/
HoverEvent.@NotNull ShowItem deserializeShowItem(@NotNull Component input) throws IOException;

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

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

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

0 comments on commit 972b178

Please sign in to comment.