From 80384725fa9acac6a193fe7809185284a8c382ab Mon Sep 17 00:00:00 2001 From: Noah van der Aa Date: Wed, 30 Mar 2022 13:40:30 +0200 Subject: [PATCH 1/5] feat(minimessage): selector tag --- .../minimessage/tag/standard/SelectorTag.java | 70 +++++++++++++++++++ .../tag/standard/StandardTags.java | 16 ++++- .../tag/standard/SelectorTagTest.java | 57 +++++++++++++++ 3 files changed, 142 insertions(+), 1 deletion(-) create mode 100644 text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java create mode 100644 text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java new file mode 100644 index 000000000..338b0c45b --- /dev/null +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java @@ -0,0 +1,70 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.minimessage.tag.standard; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.SelectorComponent; +import net.kyori.adventure.text.minimessage.Context; +import net.kyori.adventure.text.minimessage.ParsingException; +import net.kyori.adventure.text.minimessage.internal.serializer.Emitable; +import net.kyori.adventure.text.minimessage.internal.serializer.SerializableResolver; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.jetbrains.annotations.Nullable; + +/** + * Insert a selector component into the result. + * + * @since 4.11.0 + */ +final class SelectorTag { + private static final String SEL = "sel"; + private static final String SELECTOR = "selector"; + + static final TagResolver RESOLVER = SerializableResolver.claimingComponent( + StandardTags.names(SEL, SELECTOR), + SelectorTag::create, + SelectorTag::claim + ); + + private SelectorTag() { + } + + static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingException { + final String key = args.popOr("A selection key is required").value(); + + return Tag.inserting(Component.selector(key)); + } + + static @Nullable Emitable claim(final Component input) { + if (!(input instanceof SelectorComponent)) return null; + + final SelectorComponent st = (SelectorComponent) input; + return emit -> { + emit.tag(SEL); + emit.argument(st.pattern()); + }; + } +} diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java index 3674d5802..11ee2099b 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java @@ -59,7 +59,8 @@ private StandardTags() { RainbowTag.RESOLVER, ResetTag.RESOLVER, NewlineTag.RESOLVER, - TransitionTag.RESOLVER + TransitionTag.RESOLVER, + SelectorTag.RESOLVER ) .build(); @@ -214,6 +215,19 @@ public static TagResolver transition() { return NewlineTag.RESOLVER; } + /** + * Get a resolver for the {@value SelectorTag#SELECTOR} tag. + * + *

This tag also responds to {@value SelectorTag#SEL}.

+ * + * @return a resolver for the {@value SelectorTag#SELECTOR} tag + * @since 4.11.0 + */ + public static @NotNull TagResolver selector() { + return SelectorTag.RESOLVER; + } + + /** * Get a resolver that handles all default standard tags. * diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java new file mode 100644 index 000000000..82f81a3e2 --- /dev/null +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java @@ -0,0 +1,57 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2022 KyoriPowered + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package net.kyori.adventure.text.minimessage.tag.standard; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.minimessage.AbstractTest; +import org.junit.jupiter.api.Test; + +import static net.kyori.adventure.text.Component.selector; +import static net.kyori.adventure.text.Component.text; + +class SelectorTagTest extends AbstractTest { + @Test + void testSerializeSelector() { + final String expected = "Hello there, !"; + + final TextComponent.Builder builder = text() + .content("Hello there, ") + .append(selector("@s")) + .append(text("!")); + + this.assertSerializedEquals(expected, builder); + } + + @Test + void testSelector() { + final String input = "Hello there, !"; + final Component expected = text() + .content("Hello there, ") + .append(selector("@s")) + .append(text("!")).build(); + + this.assertParsedEquals(expected, input); + } +} From 7d9884f55f60a9b4ac2c7fbaf70909f7a2a7e6fb Mon Sep 17 00:00:00 2001 From: Noah van der Aa Date: Wed, 30 Mar 2022 19:00:30 +0200 Subject: [PATCH 2/5] '/*' hAs MoRe ThAn 1 EmPtY lInEs BeFoRe. [EmPtYlInEsEpArAtOr] --- .../adventure/text/minimessage/tag/standard/StandardTags.java | 1 - 1 file changed, 1 deletion(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java index 11ee2099b..1b87c0d67 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/StandardTags.java @@ -227,7 +227,6 @@ public static TagResolver transition() { return SelectorTag.RESOLVER; } - /** * Get a resolver that handles all default standard tags. * From f6a8fd4805bca649e12432e3467db9866e602dfa Mon Sep 17 00:00:00 2001 From: Noah van der Aa Date: Mon, 23 May 2022 09:14:35 +0200 Subject: [PATCH 3/5] Add separator --- .../text/minimessage/tag/standard/SelectorTag.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java index 338b0c45b..a000db3f6 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java @@ -24,6 +24,7 @@ package net.kyori.adventure.text.minimessage.tag.standard; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.SelectorComponent; import net.kyori.adventure.text.minimessage.Context; import net.kyori.adventure.text.minimessage.ParsingException; @@ -54,8 +55,12 @@ private SelectorTag() { static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingException { final String key = args.popOr("A selection key is required").value(); + ComponentLike separator = null; + if (args.hasNext()) { + separator = ctx.deserialize(args.pop().value()); + } - return Tag.inserting(Component.selector(key)); + return Tag.inserting(Component.selector(key, separator)); } static @Nullable Emitable claim(final Component input) { From 4b791206c9c734632ad99201aa59cd0f6c013cd8 Mon Sep 17 00:00:00 2001 From: Noah van der Aa Date: Mon, 23 May 2022 09:18:17 +0200 Subject: [PATCH 4/5] Test separator --- .../minimessage/tag/standard/SelectorTagTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java index 82f81a3e2..f046bf1f1 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTagTest.java @@ -54,4 +54,15 @@ void testSelector() { this.assertParsedEquals(expected, input); } + + @Test + void testSeparator() { + final String input = "Hello there, !"; + final Component expected = text() + .content("Hello there, ") + .append(selector("@s", text("separator"))) + .append(text("!")).build(); + + this.assertParsedEquals(expected, input); + } } From 8db3abb9311774601a5e291a6fb11415ef80de31 Mon Sep 17 00:00:00 2001 From: zml Date: Tue, 31 May 2022 20:13:37 -0700 Subject: [PATCH 5/5] text-minimessage: Serialize selector separator as well --- .../adventure/text/minimessage/tag/standard/SelectorTag.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java index a000db3f6..33f45fa68 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/tag/standard/SelectorTag.java @@ -70,6 +70,9 @@ static Tag create(final ArgumentQueue args, final Context ctx) throws ParsingExc return emit -> { emit.tag(SEL); emit.argument(st.pattern()); + if (st.separator() != null) { + emit.argument(st.separator()); + } }; } }