Skip to content

Commit

Permalink
api: Flattener renames
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Mar 8, 2021
1 parent b568374 commit 7cad65c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface ComponentFlattener extends Buildable<ComponentFlattener, Compon
* @since 4.7.0
*/
static @NonNull Builder builder() {
return new ComponentFlattenerImpl.Builder();
return new ComponentFlattenerImpl.BuilderImpl();
}

/**
Expand Down Expand Up @@ -96,10 +96,10 @@ interface Builder extends Buildable.Builder<ComponentFlattener> {
* @param converter the converter to map that component to a string
* @param <T> component type
* @return this builder
* @see #complexMapper(Class, BiConsumer) for component types that are too complex to be directly rendered to a string
* @since 4.7.0
* @see #complexType(Class, BiConsumer) for component types that are too complex to be directly rendered to a string
*/
<T extends Component> @NonNull Builder type(final @NonNull Class<T> type, final @NonNull Function<T, String> converter);
<T extends Component> @NonNull Builder mapper(final @NonNull Class<T> type, final @NonNull Function<T, String> converter);

/**
* Register a type of component that needs to be flattened to an intermediate stage.
Expand All @@ -110,7 +110,7 @@ interface Builder extends Buildable.Builder<ComponentFlattener> {
* @return this builder
* @since 4.7.0
*/
<T extends Component> @NonNull Builder complexType(final @NonNull Class<T> type, final @NonNull BiConsumer<T, Consumer<Component>> converter);
<T extends Component> @NonNull Builder complexMapper(final @NonNull Class<T> type, final @NonNull BiConsumer<T, Consumer<Component>> converter);

/**
* Register a handler for unknown component types.
Expand All @@ -121,6 +121,6 @@ interface Builder extends Buildable.Builder<ComponentFlattener> {
* @return this builder
* @since 4.7.0
*/
@NonNull Builder unknownHandler(final @Nullable Function<Component, String> converter);
@NonNull Builder unknownMapper(final @Nullable Function<Component, String> converter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,16 @@

final class ComponentFlattenerImpl implements ComponentFlattener {
@SuppressWarnings("deprecation")
static final ComponentFlattener BASIC = new Builder()
.type(KeybindComponent.class, KeybindComponent::keybind) // IntelliJ is wrong here, this is fine
.type(ScoreComponent.class, ScoreComponent::value) // Removed in Vanilla 1.16, but we keep it for backwards compat
.type(SelectorComponent.class, SelectorComponent::pattern)
.type(TextComponent.class, TextComponent::content)
.type(TranslatableComponent.class, TranslatableComponent::key)
static final ComponentFlattener BASIC = new BuilderImpl()
.mapper(KeybindComponent.class, KeybindComponent::keybind) // IntelliJ is wrong here, this is fine
.mapper(ScoreComponent.class, ScoreComponent::value) // Removed in Vanilla 1.16, but we keep it for backwards compat
.mapper(SelectorComponent.class, SelectorComponent::pattern)
.mapper(TextComponent.class, TextComponent::content)
.mapper(TranslatableComponent.class, TranslatableComponent::key)
// The Vanilla game will not print NBT components, expecting those to be resolved with sender context
.build();
static final ComponentFlattener TEXT_ONLY = new Builder()
.type(TextComponent.class, TextComponent::content)
static final ComponentFlattener TEXT_ONLY = new BuilderImpl()
.mapper(TextComponent.class, TextComponent::content)
.build();

private static final int MAX_DEPTH = 512;
Expand Down Expand Up @@ -137,7 +137,7 @@ private void flatten0(final @NonNull Component input, final @NonNull FlattenerLi

@Override
public ComponentFlattener.@NonNull Builder toBuilder() {
return new Builder(this.flatteners, this.complexFlatteners, this.unknownHandler);
return new BuilderImpl(this.flatteners, this.complexFlatteners, this.unknownHandler);
}

// A function that allows nesting other flatten operations
Expand All @@ -148,17 +148,17 @@ interface Handler {
void handle(final Component input, final FlattenerListener listener, final int depth);
}

static final class Builder implements ComponentFlattener.Builder {
static final class BuilderImpl implements Builder {
private final Map<Class<?>, Function<?, String>> flatteners;
private final Map<Class<?>, BiConsumer<?, Consumer<Component>>> complexFlatteners;
private @Nullable Function<Component, String> unknownHandler;

Builder() {
BuilderImpl() {
this.flatteners = new HashMap<>();
this.complexFlatteners = new HashMap<>();
}

Builder(final Map<Class<?>, Function<?, String>> flatteners, final Map<Class<?>, BiConsumer<?, Consumer<Component>>> complexFlatteners, final @Nullable Function<Component, String> unknownHandler) {
BuilderImpl(final Map<Class<?>, Function<?, String>> flatteners, final Map<Class<?>, BiConsumer<?, Consumer<Component>>> complexFlatteners, final @Nullable Function<Component, String> unknownHandler) {
this.flatteners = new HashMap<>(flatteners);
this.complexFlatteners = new HashMap<>(complexFlatteners);
this.unknownHandler = unknownHandler;
Expand All @@ -170,7 +170,7 @@ static final class Builder implements ComponentFlattener.Builder {
}

@Override
public <T extends Component> ComponentFlattener.@NonNull Builder type(final @NonNull Class<T> type, final @NonNull Function<T, String> converter) {
public <T extends Component> ComponentFlattener.@NonNull Builder mapper(final @NonNull Class<T> type, final @NonNull Function<T, String> converter) {
this.validateNoneInHierarchy(requireNonNull(type, "type"));
this.flatteners.put(
type,
Expand All @@ -181,7 +181,7 @@ static final class Builder implements ComponentFlattener.Builder {
}

@Override
public <T extends Component> ComponentFlattener.@NonNull Builder complexType(final @NonNull Class<T> type, final @NonNull BiConsumer<T, Consumer<Component>> converter) {
public <T extends Component> ComponentFlattener.@NonNull Builder complexMapper(final @NonNull Class<T> type, final @NonNull BiConsumer<T, Consumer<Component>> converter) {
this.validateNoneInHierarchy(requireNonNull(type, "type"));
this.complexFlatteners.put(
type,
Expand Down Expand Up @@ -209,7 +209,7 @@ private static void testHierarchy(final Class<?> existing, final Class<?> beingR
}

@Override
public ComponentFlattener.@NonNull Builder unknownHandler(final @Nullable Function<Component, String> converter) {
public ComponentFlattener.@NonNull Builder unknownMapper(final @Nullable Function<Component, String> converter) {
this.unknownHandler = converter;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void testKeybind() {
void testCustomHandler() {
final ComponentFlattener customized = ComponentFlattener.basic()
.toBuilder()
.type(TranslatableComponent.class, component -> component.key().toUpperCase(Locale.ROOT))
.mapper(TranslatableComponent.class, component -> component.key().toUpperCase(Locale.ROOT))
.build();

this.testFlatten(customized, Component.translatable("adventure.test.key"))
Expand All @@ -223,7 +223,7 @@ void testEmptyFlattenerProducesNoContent() {
@Test
void testComplexHandler() {
final ComponentFlattener flattener = ComponentFlattener.basic().toBuilder()
.complexType(TranslatableComponent.class, (component, accepter) -> accepter.accept(Component.text(component.key(), NamedTextColor.RED)))
.complexMapper(TranslatableComponent.class, (component, accepter) -> accepter.accept(Component.text(component.key(), NamedTextColor.RED)))
.build();

this.testFlatten(flattener, Component.translatable("my.key"))
Expand All @@ -237,16 +237,16 @@ void testComplexHandler() {
void testFailsWhenInSameHierarchy() {
final ComponentFlattener.Builder builder = ComponentFlattener.builder();

builder.type(NBTComponent.class, x -> "");
builder.mapper(NBTComponent.class, x -> "");

// simple subtype
assertThrows(IllegalArgumentException.class, () -> builder.type(BlockNBTComponent.class, $ -> ""));
assertThrows(IllegalArgumentException.class, () -> builder.mapper(BlockNBTComponent.class, $ -> ""));
// complex subtype
assertThrows(IllegalArgumentException.class, () -> builder.complexType(BlockNBTComponent.class, ($, $$) -> {}));
assertThrows(IllegalArgumentException.class, () -> builder.complexMapper(BlockNBTComponent.class, ($, $$) -> {}));

// simple supertype
assertThrows(IllegalArgumentException.class, () -> builder.type(Component.class, $ -> ""));
assertThrows(IllegalArgumentException.class, () -> builder.mapper(Component.class, $ -> ""));
// complex supertype
assertThrows(IllegalArgumentException.class, () -> builder.complexType(Component.class, ($, $$) -> {}));
assertThrows(IllegalArgumentException.class, () -> builder.complexMapper(Component.class, ($, $$) -> {}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public PlainComponentSerializer() {
@Deprecated
public PlainComponentSerializer(final @Nullable Function<KeybindComponent, String> keybind, final @Nullable Function<TranslatableComponent, String> translatable) {
final ComponentFlattener.Builder builder = ComponentFlattener.basic().toBuilder();
if(keybind != null) builder.type(KeybindComponent.class, keybind);
if(translatable != null) builder.type(TranslatableComponent.class, translatable);
if(keybind != null) builder.mapper(KeybindComponent.class, keybind);
if(translatable != null) builder.mapper(TranslatableComponent.class, translatable);
this.flattener = builder.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ static final class BuilderImpl implements PlainComponentSerializer.Builder {

BuilderImpl() {
this.flattener = ComponentFlattener.basic().toBuilder()
.unknownHandler(comp -> {
.unknownMapper(comp -> {
throw new UnsupportedOperationException("Don't know how to turn " + comp.getClass().getSimpleName() + " into a string");
})
.build();
Expand Down

0 comments on commit 7cad65c

Please sign in to comment.