diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/Context.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/Context.java index 00b85158c..d3d08d3fb 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/Context.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/Context.java @@ -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; @@ -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. diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/ContextImpl.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/ContextImpl.java index df6a43a9f..2558dec65 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/ContextImpl.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/ContextImpl.java @@ -106,10 +106,22 @@ public UnaryOperator 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); diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/HoverTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/HoverTag.java index 7aaab366e..c837be0e8 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/HoverTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/HoverTag.java @@ -54,7 +54,7 @@ static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingExc final HoverEvent.Action action = (HoverEvent.Action) 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) { @@ -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); diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/TranslatableTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/TranslatableTag.java index 4281a8767..59eef76ed 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/TranslatableTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/TranslatableTag.java @@ -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(); diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/TagResolverTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/TagResolverTest.java index 7ce2deb17..4c7667eca 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/TagResolverTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/TagResolverTest.java @@ -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; @@ -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; @@ -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(" !", Placeholder.parsed("foo", "Hello")); + + final Component expected = Component.text() + .append( + text("Hello", color(NamedTextColor.RED)), + text(" "), + text("!", color(NamedTextColor.GREEN)) + ) + .build(); + + assertEquals(expected, input); + } + + @Test + void testContextParseVarargs() { + final Context ctx = TestBase.dummyContext("dummy text"); + final Component input = ctx.deserialize(" !", + Placeholder.parsed("foo", "Hello"), Placeholder.parsed("bar", "World")); + + 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");