Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow parsing with custom Tag Resolvers #701

Merged
merged 4 commits into from Feb 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -25,6 +25,7 @@

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -38,14 +39,35 @@
*/
@ApiStatus.NonExtendable
public interface Context {

/**
* Deserializes a MiniMessage string using all the settings of this context.
*
* @param message the message to parse
* @return the parsed message
* @since 4.10.0
*/
@NotNull Component deserialize(final @NotNull String message);

/**
* Deserializes a MiniMessage string using all the settings of this context.
*
* @param message the message to parse
* @param resolver additional tag resolver, added to all other resolvers in this parse, but taking priority in the event of a name overlap
* @return the parsed message
* @since 4.10.0
*/
@NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver resolver);

/**
* Parses a MiniMessage string using all the settings of this context.
* Deserializes a MiniMessage string using all the settings of this context.
*
* @param message the message to parse
* @param resolvers additional tag resolvers, added to all other resolvers in this parse, but taking priority in the event of a name overlap
* @return the parsed message
* @since 4.10.0
*/
@NotNull Component parse(final @NotNull String message);
@NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver@NotNull... resolvers);

/**
* Create a new parsing exception.
Expand Down
Expand Up @@ -106,10 +106,22 @@ public UnaryOperator<Component> postProcessor() {
}

@Override
public @NotNull Component parse(final @NotNull String message) {
public @NotNull Component deserialize(final @NotNull String message) {
return this.miniMessage.deserialize(requireNonNull(message, "message"), this.tagResolver);
}

@Override
public @NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver resolver) {
return this.miniMessage.deserialize(requireNonNull(message, "message"),
TagResolver.builder().resolver(this.tagResolver).resolver(requireNonNull(resolver, "resolver")).build());
}

@Override
public @NotNull Component deserialize(final @NotNull String message, final @NotNull TagResolver@NotNull... resolvers) {
return this.miniMessage.deserialize(requireNonNull(message, "message"),
TagResolver.builder().resolver(this.tagResolver).resolvers(requireNonNull(resolvers, "resolvers")).build());
}

@Override
public @NotNull ParsingException newException(@NotNull final String message) {
return new ParsingExceptionImpl(message, this.message, null, EMPTY_TOKEN_ARRAY);
Expand Down
Expand Up @@ -54,7 +54,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingExc
final HoverEvent.Action<Object> action = (HoverEvent.Action<Object>) HoverEvent.Action.NAMES.value(actionName);
final Object value;
if (action == (Object) HoverEvent.Action.SHOW_TEXT) {
value = ctx.parse(args.popOr("show_text action requires a message").value());
value = ctx.deserialize(args.popOr("show_text action requires a message").value());
} else if (action == (Object) HoverEvent.Action.SHOW_ITEM) {
value = parseShowItem(args, ctx);
} else if (action == (Object) HoverEvent.Action.SHOW_ENTITY) {
Expand Down Expand Up @@ -85,7 +85,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingExc
final Key key = Key.key(args.popOr("Show entity needs a type argument").value());
final UUID id = UUID.fromString(args.popOr("Show entity needs an entity UUID").value());
if (args.hasNext()) {
final Component name = context.parse(args.pop().value());
final Component name = context.deserialize(args.pop().value());
return HoverEvent.ShowEntity.of(key, id, name);
}
return HoverEvent.ShowEntity.of(key, id);
Expand Down
Expand Up @@ -51,7 +51,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingExc
if (args.hasNext()) {
with = new ArrayList<>();
while (args.hasNext()) {
with.add(ctx.parse(args.pop().value()));
with.add(ctx.deserialize(args.pop().value()));
}
} else {
with = Collections.emptyList();
Expand Down
Expand Up @@ -26,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.Context;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.TestBase;
Expand All @@ -35,6 +36,8 @@
import org.jetbrains.annotations.Nullable;
import org.junit.jupiter.api.Test;

import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.TextColor.color;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -81,6 +84,40 @@ void testSingleAndResolversCombine() {
assertEquals("from resolver", ((PreProcess) resolveForTest(built, "overlapping")).value());
}

@Test
void testContextParseOne() {
final Context ctx = TestBase.dummyContext("dummy text");
final Component input = ctx.deserialize("<foo> <bar><green>!</green>", Placeholder.parsed("foo", "<red>Hello</red>"));

final Component expected = Component.text()
.append(
text("Hello", color(NamedTextColor.RED)),
text(" <bar>"),
text("!", color(NamedTextColor.GREEN))
)
.build();

assertEquals(expected, input);
}

@Test
void testContextParseVarargs() {
final Context ctx = TestBase.dummyContext("dummy text");
final Component input = ctx.deserialize("<foo> <bar><green>!</green>",
Placeholder.parsed("foo", "<red>Hello</red>"), Placeholder.parsed("bar", "<yellow>World</yellow>"));

final Component expected = Component.text()
.append(
text("Hello", color(NamedTextColor.RED)),
text(" "),
text("World", color(NamedTextColor.YELLOW)),
text("!", color(NamedTextColor.GREEN))
)
.build();

assertEquals(expected, input);
}

private static @NotNull Tag resolveForTest(final TagResolver resolver, final String tag) {
try {
final Context ctx = TestBase.dummyContext("help i shouldn't be seen");
Expand Down