Skip to content

Commit

Permalink
feature-flag -> option
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Dec 17, 2023
1 parent d4e79df commit 75f23ef
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ truth = "1.1.5"
# shared
examination-api = { module = "net.kyori:examination-api", version.ref = "examination" }
examination-string = { module = "net.kyori:examination-string", version.ref = "examination" }
featureFlag = { module = "net.kyori:feature-flag", version = "1.0.0-SNAPSHOT" }
option = { module = "net.kyori:option", version = "1.0.0-SNAPSHOT" }
guava = { module = "com.google.guava:guava", version.ref = "guava" }
guava-testlib = { module = "com.google.guava:guava-testlib", version.ref = "guava" }
jetbrainsAnnotations = "org.jetbrains:annotations:24.1.0"
Expand Down
2 changes: 1 addition & 1 deletion nbt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

dependencies {
api(libs.featureFlag)
api(libs.option)
api(libs.examination.api)
api(libs.examination.string)
compileOnlyApi(libs.jetbrainsAnnotations)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.TranslationArgument;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.Nullable;

import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.EXTRA;
Expand All @@ -78,7 +78,7 @@ final class ComponentSerializerImpl extends TypeAdapter<Component> {
static final Type COMPONENT_LIST_TYPE = new TypeToken<List<Component>>() {}.getType();
static final Type TRANSLATABLE_ARGUMENT_LIST_TYPE = new TypeToken<List<TranslationArgument>>() {}.getType();

static TypeAdapter<Component> create(final FeatureFlagConfig features, final Gson gson) {
static TypeAdapter<Component> create(final OptionState features, final Gson gson) {
return new ComponentSerializerImpl(features.value(JSONFlags.EMIT_COMPACT_TEXT_COMPONENT), gson).nullSafe();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.util.Buildable;
import net.kyori.adventure.util.PlatformAPI;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -125,10 +125,10 @@ static Builder builder() {
*/
interface Builder extends AbstractBuilder<GsonComponentSerializer>, Buildable.Builder<GsonComponentSerializer>, JSONComponentSerializer.Builder {
@Override
@NotNull Builder features(final @NotNull FeatureFlagConfig flags);
@NotNull Builder features(final @NotNull OptionState flags);

@Override
@NotNull Builder editFeatures(final @NotNull Consumer<FeatureFlagConfig.Builder> flagEditor);
@NotNull Builder editFeatures(final @NotNull Consumer<OptionState.Builder> flagEditor);

/**
* Sets that the serializer should downsample hex colors to named colors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.util.Services;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -59,9 +59,9 @@ static final class Instances {
private final Gson serializer;
private final UnaryOperator<GsonBuilder> populator;
private final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer;
private final FeatureFlagConfig flags;
private final OptionState flags;

GsonComponentSerializerImpl(final FeatureFlagConfig flags, final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer) {
GsonComponentSerializerImpl(final OptionState flags, final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer) {
this.flags = flags;
this.legacyHoverSerializer = legacyHoverSerializer;
this.populator = builder -> {
Expand Down Expand Up @@ -122,7 +122,7 @@ static final class Instances {
}

static final class BuilderImpl implements Builder {
private FeatureFlagConfig flags = JSONFlags.byDataVersion(); // latest
private OptionState flags = JSONFlags.byDataVersion(); // latest
private net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer;

BuilderImpl() {
Expand All @@ -136,14 +136,14 @@ static final class BuilderImpl implements Builder {
}

@Override
public @NotNull Builder features(final @NotNull FeatureFlagConfig flags) {
public @NotNull Builder features(final @NotNull OptionState flags) {
this.flags = requireNonNull(flags, "flags");
return this;
}

@Override
public @NotNull Builder editFeatures(final @NotNull Consumer<FeatureFlagConfig.Builder> flagEditor) {
final FeatureFlagConfig.Builder builder = FeatureFlagConfig.featureFlagConfig()
public @NotNull Builder editFeatures(final @NotNull Consumer<OptionState.Builder> flagEditor) {
final OptionState.Builder builder = OptionState.optionState()
.values(this.flags);
requireNonNull(flagEditor, "flagEditor").accept(builder);
this.flags = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import net.kyori.adventure.text.format.TextColor;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.Nullable;

final class SerializerFactory implements TypeAdapterFactory {
Expand All @@ -57,10 +57,10 @@ final class SerializerFactory implements TypeAdapterFactory {
static final Class<UUID> UUID_TYPE = UUID.class;
static final Class<TranslationArgument> TRANSLATION_ARGUMENT_TYPE = TranslationArgument.class;

private final FeatureFlagConfig features;
private final OptionState features;
private final net.kyori.adventure.text.serializer.json.LegacyHoverEventSerializer legacyHoverSerializer;

SerializerFactory(final FeatureFlagConfig features, final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer) {
SerializerFactory(final OptionState features, final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHoverSerializer) {
this.features = features;
this.legacyHoverSerializer = legacyHoverSerializer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.adventure.util.Codec;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.Nullable;

import static net.kyori.adventure.text.serializer.json.JSONComponentConstants.CLICK_EVENT;
Expand Down Expand Up @@ -82,7 +82,7 @@ final class StyleSerializer extends TypeAdapter<Style> {
}
}

static TypeAdapter<Style> create(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final FeatureFlagConfig features, final Gson gson) {
static TypeAdapter<Style> create(final net.kyori.adventure.text.serializer.json.@Nullable LegacyHoverEventSerializer legacyHover, final OptionState features, final Gson gson) {
final JSONFlags.HoverEventValueMode hoverMode = features.value(JSONFlags.EMIT_HOVER_EVENT_TYPE);
return new StyleSerializer(
legacyHover,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
import java.io.IOException;
import java.util.UUID;
import net.kyori.adventure.text.serializer.json.JSONFlags;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;

final class UUIDSerializer extends TypeAdapter<UUID> {
private final boolean emitIntArray;

static TypeAdapter<UUID> uuidSerializer(final FeatureFlagConfig features) {
static TypeAdapter<UUID> uuidSerializer(final OptionState features) {
return new UUIDSerializer(features.value(JSONFlags.EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY)).nullSafe();
}

Expand Down
2 changes: 1 addition & 1 deletion text-serializer-json/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ configurations.register("jmh") {

dependencies {
api(projects.adventureApi)
api(libs.featureFlag)
api(libs.option)
annotationProcessor(projects.adventureAnnotationProcessors)

testFixturesApi(libs.junit.api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import java.util.function.Consumer;
import net.kyori.adventure.text.Component;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -50,12 +50,12 @@ final class DummyJSONComponentSerializer implements JSONComponentSerializer {
// A no-op builder that just returns the unsupported instance.
static final class BuilderImpl implements Builder {
@Override
public @NotNull Builder features(final @NotNull FeatureFlagConfig flags) {
public @NotNull Builder features(final @NotNull OptionState flags) {
return this;
}

@Override
public @NotNull Builder editFeatures(final @NotNull Consumer<FeatureFlagConfig.Builder> flagEditor) {
public @NotNull Builder editFeatures(final @NotNull Consumer<OptionState.Builder> flagEditor) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.ComponentSerializer;
import net.kyori.adventure.util.PlatformAPI;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -78,7 +78,7 @@ interface Builder {
* @see JSONFlags
* @since 4.15.0
*/
@NotNull Builder features(final @NotNull FeatureFlagConfig flags);
@NotNull Builder features(final @NotNull OptionState flags);

/**
* Edit the active set of feature flags.
Expand All @@ -88,7 +88,7 @@ interface Builder {
* @see JSONFlags
* @since 4.15.0
*/
@NotNull Builder editFeatures(final @NotNull Consumer<FeatureFlagConfig.Builder> flagEditor);
@NotNull Builder editFeatures(final @NotNull Consumer<OptionState.Builder> flagEditor);

/**
* Sets that the serializer should downsample hex colors to named colors.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/
package net.kyori.adventure.text.serializer.json;

import net.kyori.featureflag.FeatureFlag;
import net.kyori.featureflag.FeatureFlagConfig;
import net.kyori.option.Option;
import net.kyori.option.OptionState;
import org.jetbrains.annotations.NotNull;

/**
Expand All @@ -50,29 +50,29 @@ private JSONFlags() {
* @since 4.15.0
* @sinceMinecraft 1.16
*/
public static final FeatureFlag<Boolean> EMIT_RGB = FeatureFlag.booleanFlag(key("emit/rgb"), true);
public static final Option<Boolean> EMIT_RGB = Option.booleanOption(key("emit/rgb"), true);
/**
* Control how hover event values should be emitted.
*
* @since 4.15.0
*/
public static final FeatureFlag<HoverEventValueMode> EMIT_HOVER_EVENT_TYPE = FeatureFlag.enumFlag(key("emit/hover_value_mode"), HoverEventValueMode.class, HoverEventValueMode.MODERN_ONLY);
public static final Option<HoverEventValueMode> EMIT_HOVER_EVENT_TYPE = Option.enumOption(key("emit/hover_value_mode"), HoverEventValueMode.class, HoverEventValueMode.MODERN_ONLY);

/**
* Whether to emit text components with no style and no children as plain text.
*
* @since 4.15.0
* @sinceMinecraft 1.20.3
*/
public static final FeatureFlag<Boolean> EMIT_COMPACT_TEXT_COMPONENT = FeatureFlag.booleanFlag(key("emit/compact_text_component"), true);
public static final Option<Boolean> EMIT_COMPACT_TEXT_COMPONENT = Option.booleanOption(key("emit/compact_text_component"), true);

/**
* Whether to emit the hover event show entity action's entity UUID as an int array,
* as understood by 1.20.3+, or as a string as understood by previous versions.
*
* @since 4.15.0
*/
public static final FeatureFlag<Boolean> EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY = FeatureFlag.booleanFlag(key("emit/hover_show_entity_id_as_int_array"), true);
public static final Option<Boolean> EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY = Option.booleanOption(key("emit/hover_show_entity_id_as_int_array"), true);

/**
* Whether to be strict about accepting invalid hover/click events.
Expand All @@ -81,12 +81,12 @@ private JSONFlags() {
*
* @since 4.15.0
*/
public static final FeatureFlag<Boolean> VALIDATE_STRICT_EVENTS = FeatureFlag.booleanFlag(key("validate/strict_events"), true);
public static final Option<Boolean> VALIDATE_STRICT_EVENTS = Option.booleanOption(key("validate/strict_events"), true);

/**
* Versioned by world data version.
*/
private static final FeatureFlagConfig.Versioned BY_DATA_VERSION = FeatureFlagConfig.versionedFeatureFlagConfig()
private static final OptionState.Versioned BY_DATA_VERSION = OptionState.versionedOptionState()
.version(
VERSION_INITIAL,
b -> b.value(EMIT_HOVER_EVENT_TYPE, HoverEventValueMode.LEGACY_ONLY)
Expand All @@ -112,7 +112,7 @@ private JSONFlags() {
*
* <p>This may provide a less efficient representation of components, but will not result in information being discarded.</p>
*/
private static final FeatureFlagConfig MOST_COMPATIBLE = FeatureFlagConfig.featureFlagConfig()
private static final OptionState MOST_COMPATIBLE = OptionState.optionState()
.value(EMIT_HOVER_EVENT_TYPE, HoverEventValueMode.BOTH)
.value(EMIT_HOVER_SHOW_ENTITY_ID_AS_INT_ARRAY, false)
.value(EMIT_COMPACT_TEXT_COMPONENT, false)
Expand All @@ -129,7 +129,7 @@ private static String key(final String value) {
* @return the versioned flag set
* @since 4.15.0
*/
public static FeatureFlagConfig.@NotNull Versioned byDataVersion() {
public static OptionState.@NotNull Versioned byDataVersion() {
return BY_DATA_VERSION;
}

Expand All @@ -141,7 +141,7 @@ private static String key(final String value) {
* @return the most widely compatible feature flag set
* @since 4.15.0
*/
public static @NotNull FeatureFlagConfig compatibility() {
public static @NotNull OptionState compatibility() {
return MOST_COMPATIBLE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,14 @@ void testSkipInvalidHoverEventWhenLenient() {
@SuppressWarnings("deprecation")
void testFailOnInvalidHoverEvents() {
assertThrows(JsonParseException.class, () -> {
deserialize(object(object -> {
object.addProperty(JSONComponentConstants.TEXT, "hello");
object.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_text");
hover.add(JSONComponentConstants.HOVER_EVENT_VALUE, new JsonArray());
}));
deserialize(object(object -> {
object.addProperty(JSONComponentConstants.TEXT, "hello");
object.add(JSONComponentConstants.HOVER_EVENT, object(hover -> {
hover.addProperty(JSONComponentConstants.HOVER_EVENT_ACTION, "show_text");
hover.add(JSONComponentConstants.HOVER_EVENT_VALUE, new JsonArray());
}));
}
);
}));
});
}

}

0 comments on commit 75f23ef

Please sign in to comment.