Skip to content

Commit

Permalink
and now we go virtual
Browse files Browse the repository at this point in the history
  • Loading branch information
kezz committed Jan 6, 2025
1 parent 51ba2d1 commit d504ba7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import java.util.Map;
import java.util.Objects;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.VirtualComponent;
import net.kyori.adventure.text.VirtualComponentRenderer;
import net.kyori.adventure.text.minimessage.Context;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.tag.Tag;
Expand All @@ -43,16 +45,20 @@ final class ArgumentTag implements TagResolver {
private static final String NAME_1 = "arg";

private final List<? extends ComponentLike> argumentComponents;
private final Map<String, NamedTranslationArgument> namedArguments;
private final Map<String, ComponentLike> namedArguments;

ArgumentTag(final @NotNull List<? extends ComponentLike> argumentComponents) {
this.argumentComponents = new ArrayList<>(Objects.requireNonNull(argumentComponents, "argumentComponents"));

final Map<String, NamedTranslationArgument> namedArgumentMap = new HashMap<>(this.argumentComponents.size());
final Map<String, ComponentLike> namedArgumentMap = new HashMap<>(this.argumentComponents.size());
for (final ComponentLike argument : this.argumentComponents) {
if (argument instanceof NamedTranslationArgument) {
final NamedTranslationArgument namedArgument = (NamedTranslationArgument) argument;
namedArgumentMap.put(namedArgument.name(), namedArgument);
if (argument instanceof VirtualComponent) {
final VirtualComponentRenderer<?> renderer = ((VirtualComponent) argument).renderer();

if (renderer instanceof NamedTranslationArgument) {
final NamedTranslationArgument namedArgument = (NamedTranslationArgument) argument;
namedArgumentMap.put(namedArgument.name(), namedArgument.translationArgument());
}
}
}

Expand All @@ -70,7 +76,7 @@ final class ArgumentTag implements TagResolver {

return Tag.inserting(this.argumentComponents.get(index));
} else {
final NamedTranslationArgument namedArgument = this.namedArguments.get(name);
final ComponentLike namedArgument = this.namedArguments.get(name);

if (namedArgument != null) {
return Tag.inserting(namedArgument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
*/
package net.kyori.adventure.text.minimessage.translation;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TranslatableComponent;
import net.kyori.adventure.text.TranslationArgument;
import net.kyori.adventure.text.TranslationArgumentLike;
import net.kyori.adventure.text.VirtualComponent;
import net.kyori.adventure.text.minimessage.tag.TagPattern;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;

import static java.util.Objects.requireNonNull;
Expand All @@ -38,9 +41,18 @@
* <p>This is intended for use with {@link TranslatableComponent translatable components}
* used with a {@link MiniMessageTranslator} instance to allow {@code <name>} tags.</p>
*
* <p>Static methods on this class work by creating
* {@link VirtualComponent virtual components} that store instances of this class.
* The MiniMessage translator instance detects these virtual components to use the name
* provided as tag names to replace the {@code <arg>} tag.</p>
*
* <p>As the names provided to all static methods in this class are used to create tags,
* they must be valid tag names.</p>
*
* @since 4.19.0
*/
public interface NamedTranslationArgument extends TranslationArgumentLike {
@ApiStatus.NonExtendable
public interface NamedTranslationArgument {
/**
* Create a named boolean argument.
*
Expand All @@ -49,7 +61,7 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @return the named argument
* @since 4.19.0
*/
static @NotNull NamedTranslationArgument bool(final @TagPattern @NotNull String name, final boolean value) {
static @NotNull ComponentLike bool(final @TagPattern @NotNull String name, final boolean value) {
return argument(name, TranslationArgument.bool(value));
}

Expand All @@ -61,7 +73,7 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @return the named argument
* @since 4.19.0
*/
static @NotNull NamedTranslationArgument numeric(final @TagPattern @NotNull String name, final @NotNull Number value) {
static @NotNull ComponentLike numeric(final @TagPattern @NotNull String name, final @NotNull Number value) {
return argument(name, TranslationArgument.numeric(value));
}

Expand All @@ -73,7 +85,7 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @return the named argument
* @since 4.19.0
*/
static @NotNull NamedTranslationArgument component(final @TagPattern @NotNull String name, final @NotNull ComponentLike value) {
static @NotNull ComponentLike component(final @TagPattern @NotNull String name, final @NotNull ComponentLike value) {
return argument(name, TranslationArgument.component(value));
}

Expand All @@ -85,7 +97,7 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @return the named argument
* @since 4.19.0
*/
static @NotNull NamedTranslationArgument argument(final @TagPattern @NotNull String name, final @NotNull TranslationArgumentLike argument) {
static @NotNull ComponentLike argument(final @TagPattern @NotNull String name, final @NotNull TranslationArgumentLike argument) {
return argument(name, requireNonNull(argument, "argument").asTranslationArgument());
}

Expand All @@ -97,8 +109,8 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @return the named argument
* @since 4.19.0
*/
static @NotNull NamedTranslationArgument argument(final @TagPattern @NotNull String name, final @NotNull TranslationArgument argument) {
return new NamedTranslationArgumentImpl(name, argument);
static @NotNull ComponentLike argument(final @TagPattern @NotNull String name, final @NotNull TranslationArgument argument) {
return Component.virtual(Void.class, new NamedTranslationArgumentImpl(name, argument));
}

/**
Expand All @@ -108,4 +120,12 @@ public interface NamedTranslationArgument extends TranslationArgumentLike {
* @since 4.19.0
*/
@TagPattern @NotNull String name();

/**
* The backing translation argument.
*
* @return the translation argument
* @since 4.19.0
**/
@NotNull TranslationArgument translationArgument();
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,26 @@
package net.kyori.adventure.text.minimessage.translation;

import java.util.Objects;
import net.kyori.adventure.text.ComponentLike;
import net.kyori.adventure.text.TranslationArgument;
import net.kyori.adventure.text.VirtualComponentRenderer;
import net.kyori.adventure.text.minimessage.internal.TagInternals;
import net.kyori.adventure.text.minimessage.tag.TagPattern;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.UnknownNullability;

final class NamedTranslationArgumentImpl implements NamedTranslationArgument {
final class NamedTranslationArgumentImpl implements NamedTranslationArgument, VirtualComponentRenderer<Void> {

private final @TagPattern @NotNull String name;
private final @NotNull TranslationArgument argument;
private final @NotNull TranslationArgument translationArgument;

NamedTranslationArgumentImpl(final @TagPattern @NotNull String name, final @NotNull TranslationArgument argument) {
Objects.requireNonNull(name, "name");
Objects.requireNonNull(argument, "argument");
TagInternals.assertValidTagName(name);

this.name = name;
this.argument = argument;
this.translationArgument = argument;
}

@Override
Expand All @@ -49,7 +52,17 @@ final class NamedTranslationArgumentImpl implements NamedTranslationArgument {
}

@Override
public @NotNull TranslationArgument asTranslationArgument() {
return this.argument;
public @NotNull TranslationArgument translationArgument() {
return this.translationArgument;
}

@Override
public @UnknownNullability ComponentLike apply(final @NotNull Void context) {
return this.translationArgument;
}

@Override
public @NotNull String fallbackString() {
return ""; // Not for display purposes.
}
}

0 comments on commit d504ba7

Please sign in to comment.