Skip to content

Commit

Permalink
feat: Remove Metadata argument from ComponentRendererProvider in favo…
Browse files Browse the repository at this point in the history
…r of ComponentAppendable context
  • Loading branch information
phinner committed May 28, 2024
1 parent 91f64b2 commit d12cf79
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ final class AnsiComponentAppendable implements ComponentAppendable {
this.provider = provider;
}

@Override
public MetadataContainer getContext() {
return context;
}

@Override
public ComponentAppendable append(final Component component) {
escape();
Expand All @@ -62,7 +67,7 @@ public ComponentAppendable append(final Component component) {
}

final var renderer = provider.getRenderer(component);
if (renderer != null) renderer.render(component, this, context);
if (renderer != null) renderer.render(component, this);

{
final var popped = styles.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ static ComponentAppendable mindustry(final MetadataContainer metadata) {
metadata, DistributorProvider.get().getComponentRendererProvider());
}

MetadataContainer getContext();

ComponentAppendable append(final Component component);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
package com.xpdustry.distributor.api.component.render;

import com.xpdustry.distributor.api.component.Component;
import com.xpdustry.distributor.api.metadata.MetadataContainer;

public interface ComponentRenderer<T extends Component> {

static <T extends Component> ComponentRenderer<T> noop() {
return (component, appendable, metadata) -> {};
return (component, appendable) -> {};
}

void render(final T component, final ComponentAppendable appendable, final MetadataContainer metadata);
void render(final T component, final ComponentAppendable appendable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ final class MindustryComponentAppendable implements ComponentAppendable {
this.provider = provider;
}

@Override
public MetadataContainer getContext() {
return context;
}

@Override
public ComponentAppendable append(final Component component) {
{
Expand All @@ -48,7 +53,7 @@ public ComponentAppendable append(final Component component) {
}

final var renderer = provider.getRenderer(component);
if (renderer != null) renderer.render(component, this, context);
if (renderer != null) renderer.render(component, this);

{
final var popped = colors.pop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ public PlainComponentAppendable(final MetadataContainer context, final Component
this.provider = provider;
}

@Override
public MetadataContainer getContext() {
return context;
}

@Override
public ComponentAppendable append(final Component component) {
final var renderer = this.provider.getRenderer(component);
if (renderer != null) renderer.render(component, this, this.context);
if (renderer != null) renderer.render(component, this);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.xpdustry.distributor.api.component.render.ComponentAppendable;
import com.xpdustry.distributor.api.component.render.ComponentRenderer;
import com.xpdustry.distributor.api.component.render.ComponentRendererProvider;
import com.xpdustry.distributor.api.metadata.MetadataContainer;
import com.xpdustry.distributor.api.translation.ComponentAwareTranslation;
import java.time.ZoneId;
import java.util.Locale;
Expand Down Expand Up @@ -60,8 +59,7 @@ private static final class TextComponentRenderer implements ComponentRenderer<Te
private static final TextComponentRenderer INSTANCE = new TextComponentRenderer();

@Override
public void render(
final TextComponent component, final ComponentAppendable appendable, final MetadataContainer metadata) {
public void render(final TextComponent component, final ComponentAppendable appendable) {
appendable.append(component.getContent());
}
}
Expand All @@ -71,8 +69,7 @@ private static final class ListComponentRenderer implements ComponentRenderer<Li
private static final ListComponentRenderer INSTANCE = new ListComponentRenderer();

@Override
public void render(
final ListComponent component, final ComponentAppendable appendable, final MetadataContainer metadata) {
public void render(final ListComponent component, final ComponentAppendable appendable) {
for (final var child : component.getComponents()) {
appendable.append(child);
}
Expand All @@ -84,15 +81,13 @@ private static final class TemporalComponentRenderer implements ComponentRendere
private static final TemporalComponentRenderer INSTANCE = new TemporalComponentRenderer();

@Override
public void render(
final TemporalComponent component,
final ComponentAppendable appendable,
final MetadataContainer metadata) {
public void render(final TemporalComponent component, final ComponentAppendable appendable) {
component
.getFormat()
.toFormatter()
.withZone(ZoneId.of("UTC"))
.withLocale(metadata.getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault))
.withLocale(
appendable.getContext().getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault))
.formatTo(component.getTemporal(), appendable);
}
}
Expand All @@ -102,15 +97,12 @@ private static final class TranslatableComponentRenderer implements ComponentRen
private static final TranslatableComponentRenderer INSTANCE = new TranslatableComponentRenderer();

@Override
public void render(
final TranslatableComponent component,
final ComponentAppendable appendable,
final MetadataContainer metadata) {
public void render(final TranslatableComponent component, final ComponentAppendable appendable) {
final var translation = DistributorProvider.get()
.getGlobalTranslationSource()
.getTranslationOrMissing(
component.getKey(),
metadata.getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault));
appendable.getContext().getMetadata(Audience.LOCALE).orElseGet(Locale::getDefault));
if (translation instanceof ComponentAwareTranslation aware) {
aware.formatTo(component.getParameters(), appendable);
} else {
Expand All @@ -124,10 +116,7 @@ private static final class ValueComponentRenderer implements ComponentRenderer<V
private static final ValueComponentRenderer INSTANCE = new ValueComponentRenderer();

@Override
public void render(
final ValueComponent<?> component,
final ComponentAppendable appendable,
final MetadataContainer metadata) {
public void render(final ValueComponent<?> component, final ComponentAppendable appendable) {
appendable.append(component.getValue().toString());
}
}
Expand Down

0 comments on commit d12cf79

Please sign in to comment.