From a3282a7f5552e7ec7276476f76eda38871470b95 Mon Sep 17 00:00:00 2001 From: mbax Date: Wed, 29 Dec 2021 16:40:05 -0500 Subject: [PATCH 01/49] Introduce a method by which to directly insert raw replacements. Credits: @zml2008 for smarter approach @kezz for javadocs suggestion --- .../text/minimessage/placeholder/Placeholder.java | 12 ++++++++++++ .../text/minimessage/placeholder/Replacement.java | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java index 22d24898f..687559b73 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java @@ -55,6 +55,18 @@ public interface Placeholder extends Replacement { return new PlaceholderImpl<>(key, Objects.requireNonNull(value, "value")); } + /** + * Creates a placeholder that inserts a raw string, ignoring any MiniMessage tags present. + * + * @param key the key + * @param value the replacement + * @return the placeholder + * @since 4.10.0 + */ + static @NotNull Placeholder raw(final @NotNull String key, final @NotNull String value) { + return Placeholder.component(key, Component.text(value)); + } + /** * Creates a replacement that inserts a component. * diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java index 2743f3c04..fdf8a9580 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java @@ -51,6 +51,17 @@ public interface Replacement extends Examinable { return new ReplacementImpl<>(Objects.requireNonNull(miniMessage, "miniMessage")); } + /** + * Creates a replacement that inserts a raw string, ignoring any MiniMessage tags present. + * + * @param raw the string + * @return the replacement + * @since 4.10.0 + */ + static @NotNull Replacement raw(final @NotNull String raw) { + return Replacement.component(Component.text(raw)); + } + /** * Creates a replacement that inserts a component. * From 0fac57b66380651c5d45d3705affe1575ce507e0 Mon Sep 17 00:00:00 2001 From: Kieran Wallbanks Date: Wed, 12 Jan 2022 17:06:09 +0000 Subject: [PATCH 02/49] chore: Remove parse method --- .../kyori/adventure/text/minimessage/MiniMessage.java | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessage.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessage.java index 75dc75119..55b158d5e 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessage.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/MiniMessage.java @@ -104,17 +104,6 @@ public interface MiniMessage extends ComponentSerializer}. * From 83ca5ee6a7062c4b98e40e15ea90e7d6d3a28cf3 Mon Sep 17 00:00:00 2001 From: zml Date: Wed, 12 Jan 2022 16:54:08 -0800 Subject: [PATCH 03/49] text-minimessage: Update tests for parse -> deserialize --- .../benchmark/MiniMessageBenchmark.java | 6 ++--- .../minimessage/MiniMessageParserTest.java | 20 ++++++++--------- .../MiniMessageSerializerTest.java | 6 ++--- .../text/minimessage/MiniMessageTest.java | 22 +++++++++---------- .../adventure/text/minimessage/TestBase.java | 2 +- 5 files changed, 28 insertions(+), 28 deletions(-) diff --git a/text-minimessage/src/jmh/java/net/kyori/adventure/text/minimessage/benchmark/MiniMessageBenchmark.java b/text-minimessage/src/jmh/java/net/kyori/adventure/text/minimessage/benchmark/MiniMessageBenchmark.java index 5145e6b43..9179c71f2 100644 --- a/text-minimessage/src/jmh/java/net/kyori/adventure/text/minimessage/benchmark/MiniMessageBenchmark.java +++ b/text-minimessage/src/jmh/java/net/kyori/adventure/text/minimessage/benchmark/MiniMessageBenchmark.java @@ -42,7 +42,7 @@ public class MiniMessageBenchmark { @Benchmark public Component testNiceMix() { final String input = " random strangerclick here to FEEL it"; - return MiniMessage.miniMessage().parse(input); + return MiniMessage.miniMessage().deserialize(input); } @Benchmark @@ -57,12 +57,12 @@ public Component testSimple() { @Benchmark public Component testGradient() { final String input = "COLORS ARE COOL"; - return MiniMessage.miniMessage().parse(input); + return MiniMessage.miniMessage().deserialize(input); } @Benchmark public Component testRainbow() { final String input = "COLORS ARE COOL"; - return MiniMessage.miniMessage().parse(input); + return MiniMessage.miniMessage().deserialize(input); } } diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java index a44eeac5e..f72dcfd9d 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageParserTest.java @@ -91,10 +91,10 @@ void testBritish() { final String input2 = "This is english"; final String input3 = "This is still english"; // British is superior english final String input4 = "This is still english"; - final Component out1 = this.PARSER.parse(input1); - final Component out2 = this.PARSER.parse(input2); - final Component out3 = this.PARSER.parse(input3); - final Component out4 = this.PARSER.parse(input4); + final Component out1 = this.PARSER.deserialize(input1); + final Component out2 = this.PARSER.deserialize(input2); + final Component out3 = this.PARSER.deserialize(input3); + final Component out4 = this.PARSER.deserialize(input4); assertEquals(out1, out2); assertEquals(out3, out4); @@ -104,8 +104,8 @@ void testBritish() { void testBritishColour() { final String input1 = "This is english"; // no it's british final String input2 = "This is english"; - final Component out1 = this.PARSER.parse(input1); - final Component out2 = this.PARSER.parse(input2); + final Component out1 = this.PARSER.deserialize(input1); + final Component out2 = this.PARSER.deserialize(input2); assertEquals(out1, out2); } @@ -294,7 +294,7 @@ void testEscapePlaceholders() { void testUnescape() { final String input = "TEST\\ nested\\Test"; final String expected = "TEST nestedTest"; - final Component comp = this.PARSER.parse(input); + final Component comp = this.PARSER.deserialize(input); assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp)); } @@ -303,7 +303,7 @@ void testUnescape() { void testNoUnescape() { final String input = "TEST\\ \\\\< nested\\Test"; final String expected = "TEST \\< nestedTest"; - final TextComponent comp = (TextComponent) this.PARSER.parse(input); + final TextComponent comp = (TextComponent) this.PARSER.deserialize(input); assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp)); } @@ -312,7 +312,7 @@ void testNoUnescape() { void testEscapeParse() { final String expected = "test"; final String escaped = MiniMessage.miniMessage().escapeTokens(expected); - final Component comp = MiniMessage.miniMessage().parse(escaped); + final Component comp = MiniMessage.miniMessage().deserialize(escaped); assertEquals(expected, PlainTextComponentSerializer.plainText().serialize(comp)); } @@ -1325,7 +1325,7 @@ void testShowItemHover() { void testShowEntityHover() { final UUID uuid = UUID.randomUUID(); final String nameString = "Custom Name!"; - final Component name = this.PARSER.parse(nameString); + final Component name = this.PARSER.deserialize(nameString); final Component expected = text("test").hoverEvent(HoverEvent.showEntity(Key.key("minecraft", "zombie"), uuid, name)); final String input = String.format("test", uuid, nameString); final String input1 = String.format("test", uuid, nameString); diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageSerializerTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageSerializerTest.java index d961ab000..3aa184a97 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageSerializerTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageSerializerTest.java @@ -245,10 +245,10 @@ void testFont() { void testRainbow() { final String expected = "test >> reeeeeeeee"; - final Component parsed = MiniMessage.miniMessage().parse(expected); + final Component parsed = MiniMessage.miniMessage().deserialize(expected); final String serialized = MiniMessage.miniMessage().serialize(parsed); - final Component reparsed = MiniMessage.miniMessage().parse(serialized); + final Component reparsed = MiniMessage.miniMessage().deserialize(serialized); assertEquals(this.prettyPrint(parsed), this.prettyPrint(reparsed)); } @@ -266,7 +266,7 @@ void testShowItemHover() { void testShowEntityHover() { final UUID uuid = UUID.randomUUID(); final String nameString = "Custom Name!"; - final Component name = MiniMessage.miniMessage().parse(nameString); + final Component name = MiniMessage.miniMessage().deserialize(nameString); final TextComponent.Builder input = text() .content("test") .hoverEvent(HoverEvent.showEntity(Key.key("minecraft", "zombie"), uuid, name)); diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageTest.java index cf339d09e..5ea338b4b 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageTest.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/MiniMessageTest.java @@ -133,7 +133,7 @@ void testPlaceholderInHover() { final String input = "'>This is a test message."; final MiniMessage miniMessage = MiniMessage.miniMessage(); - this.assertParsedEquals(miniMessage, expected, input, component("prefix", MiniMessage.miniMessage().parse("<#FF0000>[Plugin]"))); + this.assertParsedEquals(miniMessage, expected, input, component("prefix", MiniMessage.miniMessage().deserialize("<#FF0000>[Plugin]"))); } @Test @@ -276,19 +276,19 @@ void testNonStrictGH69() { @Test void testStrictException() { final String input = "Example: /plot flag set coral-dry true"; - assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); } @Test void testMissingCloseOfHover() { final String input = "Hello'TEST'> : '>"; - assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); } @Test void testNonEndingComponent() { final String input = " assertEquals(strings, Collections.singletonList("Expected end sometimes after open tag + name, but got name = Token{type=NAME, value=\"red is already created! Try different name! \"} and inners = []"))).build().parse(input); + MiniMessage.builder().parsingErrorMessageConsumer(strings -> assertEquals(strings, Collections.singletonList("Expected end sometimes after open tag + name, but got name = Token{type=NAME, value=\"red is already created! Try different name! \"} and inners = []"))).build().deserialize(input); } @Test @@ -321,7 +321,7 @@ void unclosedTagStrict() { "\tREDGREENREDBLUE\n" + "\t^~~~^ ^~~~~^"; - final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); assertEquals(thrown.getMessage(), errorMessage); } @@ -333,7 +333,7 @@ void implicitCloseStrict() { "\tREDGREENNO COLORBLUE\n" + "\t^~~~^ ^~~~~~^ ^~~~~^"; - final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); assertEquals(thrown.getMessage(), errorMessage); } @@ -345,7 +345,7 @@ void implicitCloseNestedStrict() { "\tREDGREENBLUEYELLOW\n" + "\t ^~~~~~^ ^~~~~~~^ ^~~~~~~^"; - final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); assertEquals(thrown.getMessage(), errorMessage); } @@ -357,7 +357,7 @@ void resetWhileStrict() { "\tREDGREENNO COLORBLUE\n" + "\t ^~~~~~^"; - final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().parse(input)); + final ParsingException thrown = assertThrows(ParsingException.class, () -> MiniMessage.builder().strict(true).build().deserialize(input)); assertEquals(thrown.getMessage(), errorMessage); } @@ -366,7 +366,7 @@ void debugModeSimple() { final String input = " RED "; final StringBuilder sb = new StringBuilder(); - MiniMessage.builder().debug(sb).build().parse(input); + MiniMessage.builder().debug(sb).build().deserialize(input); final List messages = Arrays.asList(sb.toString().split("\n")); assertTrue(messages.contains("Beginning parsing message RED ")); @@ -385,7 +385,7 @@ void debugModeMoreComplex() { final String input = " RED BLUE bad click "; final StringBuilder sb = new StringBuilder(); - MiniMessage.builder().debug(sb).build().parse(input); + MiniMessage.builder().debug(sb).build().deserialize(input); final List messages = Arrays.asList(sb.toString().split("\n")); assertTrue(messages.contains("Beginning parsing message RED BLUE bad click ")); @@ -413,7 +413,7 @@ void debugModeMoreComplexNoError() { final String input = " RED BLUE good click "; final StringBuilder sb = new StringBuilder(); - MiniMessage.builder().debug(sb).build().parse(input); + MiniMessage.builder().debug(sb).build().deserialize(input); final List messages = Arrays.asList(sb.toString().split("\n")); assertTrue(messages.contains("Beginning parsing message RED BLUE good click ")); diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/TestBase.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/TestBase.java index 123c03727..3b0762538 100644 --- a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/TestBase.java +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/TestBase.java @@ -46,7 +46,7 @@ void assertParsedEquals(final @NotNull Component expected, final @NotNull String void assertParsedEquals(final MiniMessage miniMessage, final Component expected, final String input) { final String expectedSerialized = this.prettyPrint(expected.compact()); - final String actual = this.prettyPrint(miniMessage.parse(input).compact()); + final String actual = this.prettyPrint(miniMessage.deserialize(input).compact()); assertEquals(expectedSerialized, actual); } From 9ebd7719031d7117b516d9f18386fc809b1c8629 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sat, 15 Jan 2022 22:45:17 -0800 Subject: [PATCH 04/49] text-minimessage: Some cleanup --- .../DynamicPlaceholderResolver.java | 3 +-- .../GroupedPlaceholderResolver.java | 15 ++++++----- .../minimessage/placeholder/Placeholder.java | 12 +++------ .../placeholder/PlaceholderImpl.java | 8 ++++++ .../placeholder/PlaceholderResolver.java | 27 ++++++++++--------- .../minimessage/placeholder/Replacement.java | 3 +-- 6 files changed, 35 insertions(+), 33 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/DynamicPlaceholderResolver.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/DynamicPlaceholderResolver.java index e454cff70..b80a3c4fe 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/DynamicPlaceholderResolver.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/DynamicPlaceholderResolver.java @@ -30,12 +30,11 @@ import org.jetbrains.annotations.Nullable; final class DynamicPlaceholderResolver implements PlaceholderResolver { + private final Map> cache = new HashMap<>(); private final Function> resolver; - private final Map> cache; DynamicPlaceholderResolver(final Function> resolver) { this.resolver = resolver; - this.cache = new HashMap<>(); } @Override diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/GroupedPlaceholderResolver.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/GroupedPlaceholderResolver.java index 1aa0fd6af..d2d7e1a43 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/GroupedPlaceholderResolver.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/GroupedPlaceholderResolver.java @@ -27,19 +27,20 @@ import org.jetbrains.annotations.Nullable; final class GroupedPlaceholderResolver implements PlaceholderResolver { - private final Iterable placeholderResolvers; + private final Iterable resolvers; - GroupedPlaceholderResolver(final @NotNull Iterable placeholderResolvers) { - this.placeholderResolvers = placeholderResolvers; + GroupedPlaceholderResolver(final @NotNull Iterable resolvers) { + this.resolvers = resolvers; } @Override public @Nullable Replacement resolve(final @NotNull String key) { - for (final PlaceholderResolver placeholderResolver : this.placeholderResolvers) { - final Replacement placeholder = placeholderResolver.resolve(key); - if (placeholder != null) return placeholder; + for (final PlaceholderResolver resolver : this.resolvers) { + final Replacement placeholder = resolver.resolve(key); + if (placeholder != null) { + return placeholder; + } } - return null; } } diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java index db4d5fd27..9374beaec 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Placeholder.java @@ -23,7 +23,6 @@ */ package net.kyori.adventure.text.minimessage.placeholder; -import java.util.Locale; import java.util.Objects; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.ComponentLike; @@ -38,7 +37,6 @@ */ @ApiStatus.NonExtendable public interface Placeholder extends Replacement { - /** * Creates a placeholder that inserts a MiniMessage string. The inserted string will impact * the rest of the parse process. @@ -49,9 +47,7 @@ public interface Placeholder extends Replacement { * @since 4.10.0 */ static @NotNull Placeholder miniMessage(final @NotNull String key, final @NotNull String value) { - if (!Objects.requireNonNull(key, "key").equals(key.toLowerCase(Locale.ROOT))) - throw new IllegalArgumentException("key must be lowercase, was " + key); - + PlaceholderImpl.checkKey(key); return new PlaceholderImpl<>(key, Objects.requireNonNull(value, "value")); } @@ -64,7 +60,7 @@ public interface Placeholder extends Replacement { * @since 4.10.0 */ static @NotNull Placeholder raw(final @NotNull String key, final @NotNull String value) { - return Placeholder.component(key, Component.text(value)); + return component(key, Component.text(value)); } /** @@ -76,9 +72,7 @@ public interface Placeholder extends Replacement { * @since 4.10.0 */ static @NotNull Placeholder component(final @NotNull String key, final @NotNull ComponentLike value) { - if (!Objects.requireNonNull(key, "key").equals(key.toLowerCase(Locale.ROOT))) - throw new IllegalArgumentException("key must be lowercase, was " + key); - + PlaceholderImpl.checkKey(key); return new PlaceholderImpl<>( key, Objects.requireNonNull( diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java index a4d84a256..9c9eb560e 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text.minimessage.placeholder; +import java.util.Locale; +import java.util.Objects; import java.util.stream.Stream; import net.kyori.examination.ExaminableProperty; import net.kyori.examination.string.StringExaminer; @@ -32,6 +34,12 @@ final class PlaceholderImpl implements Placeholder { private final String key; private final T value; + static void checkKey(final @NotNull String key) { + if (!Objects.requireNonNull(key, "key").equals(key.toLowerCase(Locale.ROOT))) { + throw new IllegalArgumentException("key must be lowercase, was " + key); + } + } + PlaceholderImpl(final @NotNull String key, final @NotNull T value) { this.key = key; this.value = value; diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java index 7f70768e0..950e51020 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java @@ -89,14 +89,15 @@ public interface PlaceholderResolver { /** * Constructs a placeholder resolver capable of resolving from multiple sources. * - * @param placeholderResolvers the placeholder resolvers + * @param resolvers the placeholder resolvers * @return the placeholder resolver * @since 4.10.0 */ - static @NotNull PlaceholderResolver combining(final @NotNull PlaceholderResolver @NotNull ... placeholderResolvers) { - if (Objects.requireNonNull(placeholderResolvers, "placeholderResolvers").length == 1) - return Objects.requireNonNull(placeholderResolvers[0], "placeholderResolvers must not contain null elements"); - return new GroupedPlaceholderResolver(Arrays.asList(placeholderResolvers)); + static @NotNull PlaceholderResolver combining(final @NotNull PlaceholderResolver@NotNull... resolvers) { + if (Objects.requireNonNull(resolvers, "resolvers").length == 1) { + return Objects.requireNonNull(resolvers[0], "resolvers must not contain null elements"); + } + return combining(Arrays.asList(resolvers)); } /** @@ -104,21 +105,21 @@ public interface PlaceholderResolver { * *

The provided iterable is copied. This means changes to the iterable will not reflect in the returned resolver.

* - * @param placeholderResolvers the placeholder resolvers + * @param resolvers the placeholder resolvers * @return the placeholder resolver * @since 4.10.0 */ - static @NotNull PlaceholderResolver combining(final @NotNull Iterable placeholderResolvers) { - final List placeholderResolverList = new ArrayList<>(); + static @NotNull PlaceholderResolver combining(final @NotNull Iterable resolvers) { + final List copiedResolvers = new ArrayList<>(); - for (final PlaceholderResolver placeholderResolver : Objects.requireNonNull(placeholderResolvers, "placeholderResolvers")) { - placeholderResolverList.add(Objects.requireNonNull(placeholderResolver, "placeholderResolvers cannot contain null elements")); + for (final PlaceholderResolver resolver : Objects.requireNonNull(resolvers, "resolvers")) { + copiedResolvers.add(Objects.requireNonNull(resolver, "resolvers cannot contain null elements")); } - final int size = placeholderResolverList.size(); + final int size = copiedResolvers.size(); if (size == 0) return empty(); - if (size == 1) return placeholderResolverList.get(0); - return new GroupedPlaceholderResolver(placeholderResolvers); + if (size == 1) return copiedResolvers.get(0); + return new GroupedPlaceholderResolver(copiedResolvers); } /** diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java index 8eae3cab4..5288b6b7f 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/Replacement.java @@ -38,7 +38,6 @@ */ @ApiStatus.NonExtendable public interface Replacement extends Examinable { - /** * Creates a replacement that inserts a MiniMessage string. The inserted string will impact * the rest of the parse process. @@ -59,7 +58,7 @@ public interface Replacement extends Examinable { * @since 4.10.0 */ static @NotNull Replacement raw(final @NotNull String raw) { - return Replacement.component(Component.text(raw)); + return component(Component.text(raw)); } /** From 63955019f210dd2943aa3bff75e4fbfc65571075 Mon Sep 17 00:00:00 2001 From: zml Date: Wed, 12 Jan 2022 18:43:27 -0800 Subject: [PATCH 05/49] text-minimessage: Add a builder for PlaceholderResolvers Closes GH-656 --- .../placeholder/PlaceholderResolver.java | 109 +++++++++++++++++ .../PlaceholderResolverBuilderImpl.java | 112 ++++++++++++++++++ .../minimessage/PlaceholderResolverTest.java | 78 ++++++++++++ 3 files changed, 299 insertions(+) create mode 100644 text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolverBuilderImpl.java create mode 100644 text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/PlaceholderResolverTest.java diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java index 950e51020..42860f53a 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java @@ -33,6 +33,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static java.util.Objects.requireNonNull; + /** * A resolver for user-defined placeholders. * @@ -40,6 +42,16 @@ */ @FunctionalInterface public interface PlaceholderResolver { + /** + * Create a new builder for a {@link PlaceholderResolver}. + * + * @return a new builder + * @since 4.10.0 + */ + static @NotNull Builder builder() { + return new PlaceholderResolverBuilderImpl(); + } + /** * Constructs a placeholder resolver from a map. * @@ -159,4 +171,101 @@ public interface PlaceholderResolver { * @since 4.10.0 */ @Nullable Replacement resolve(final @NotNull String key); + + /** + * A builder to gradually construct placeholder resolvers. + * + *

Entries added later will take priority over entries added earlier.

+ * + * @since 4.10.0 + */ + interface Builder { + /** + * Add a single placeholder to this resolver. + * + * @param placeholder the placeholder + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder placeholder(final @NotNull Placeholder placeholder); + + /** + * Add placeholders to this resolver. + * + * @param placeholders placeholders to add + * @return this builder + * @since 4.10.0 + */ + default @NotNull Builder placeholders(final @NotNull Placeholder @NotNull... placeholders) { + return this.placeholders(Arrays.asList(requireNonNull(placeholders, "placeholders"))); + } + + /** + * Add placeholders to this resolver. + * + * @param placeholders placeholders to add + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder placeholders(final @NotNull Iterable> placeholders); + + /** + * Add placeholders to this resolver. + * + *

A snapshot of the map will be added to this resolver, rather than a live view.

+ * + * @param replacements placeholders to add + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder placeholders(final @NotNull Map> replacements); + + /** + * Add a placeholder resolver to those queried by the result of this builder. + * + * @param resolver the resolver to add + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder resolver(final @NotNull PlaceholderResolver resolver); + + /** + * Add placeholder resolvers to those queried by the result of this builder. + * + * @param resolvers the resolvers to add + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder resolvers(final @NotNull PlaceholderResolver@NotNull... resolvers); + + /** + * Add placeholder resolvers to those queried by the result of this builder. + * + * @param resolvers the resolvers to add + * @return this builder + * @since 4.10.0 + */ + @NotNull Builder resolvers(final @NotNull Iterable resolvers); + + /** + * Add a resolver that dynamically queries and caches based on the provided function. + * + * @param dynamic the function to query for replacements + * @return this builder + * @since 4.10.0 + */ + default @NotNull Builder dynamic(final @NotNull Function> dynamic) { + return this.resolver(PlaceholderResolver.dynamic(dynamic)); + } + + /** + * Create a placeholder resolver based on the input. + * + *

If no elements are added, this may return an empty resolver.

+ * + * @return the resolver + * @since 4.10.0 + */ + @NotNull PlaceholderResolver build(); + } } diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolverBuilderImpl.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolverBuilderImpl.java new file mode 100644 index 000000000..f4f12e79c --- /dev/null +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolverBuilderImpl.java @@ -0,0 +1,112 @@ +/* + * 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.placeholder; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.jetbrains.annotations.NotNull; + +import static java.util.Objects.requireNonNull; + +class PlaceholderResolverBuilderImpl implements PlaceholderResolver.Builder { + private final Map> replacements = new HashMap<>(); + private final List resolvers = new ArrayList<>(); + + @Override + public @NotNull PlaceholderResolver.Builder placeholder(@NotNull final Placeholder placeholder) { + this.replacements.put( + placeholder.key(), placeholder + ); + return this; + } + + @Override + public @NotNull PlaceholderResolver.Builder placeholders(@NotNull final Iterable> placeholders) { + for (final Placeholder placeholder : requireNonNull(placeholders, "placeholders")) { + this.replacements.put(placeholder.key(), placeholder); + } + return this; + } + + @Override + public @NotNull PlaceholderResolver.Builder placeholders(@NotNull final Map> replacements) { + for (final Map.Entry> entry : replacements.entrySet()) { + this.replacements.put( + requireNonNull(entry.getKey(), "replacements[?].key()"), + requireNonNull(entry.getValue(), () -> "replacements[" + entry.getKey() + "]") + ); + } + return this; + } + + @Override + public @NotNull PlaceholderResolver.Builder resolver(@NotNull final PlaceholderResolver resolver) { + this.popMap(); + this.resolvers.add(requireNonNull(resolver, "resolver")); + return this; + } + + @Override + public @NotNull PlaceholderResolver.Builder resolvers(@NotNull final PlaceholderResolver @NotNull... resolvers) { + this.popMap(); + for (final PlaceholderResolver resolver : requireNonNull(resolvers, "resolvers")) { + this.resolvers.add(requireNonNull(resolver, "resolvers[?]")); + } + return this; + } + + @Override + public @NotNull PlaceholderResolver.Builder resolvers(@NotNull final Iterable resolvers) { + this.popMap(); + for (final PlaceholderResolver resolver : requireNonNull(resolvers, "resolvers")) { + this.resolvers.add(requireNonNull(resolver, "resolvers[?]")); + } + return this; + } + + private void popMap() { + if (!this.replacements.isEmpty()) { + this.resolvers.add(new MapPlaceholderResolver(new HashMap<>(this.replacements))); + this.replacements.clear(); + } + } + + @Override + public @NotNull PlaceholderResolver build() { + this.popMap(); + if (this.resolvers.size() == 0) { + return EmptyPlaceholderResolver.INSTANCE; + } else if (this.resolvers.size() == 1) { + return this.resolvers.get(0); + } else { + final List resolvers = new ArrayList<>(this.resolvers); + Collections.reverse(resolvers); + return new GroupedPlaceholderResolver(resolvers); + } + } + +} diff --git a/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/PlaceholderResolverTest.java b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/PlaceholderResolverTest.java new file mode 100644 index 000000000..f17ae153d --- /dev/null +++ b/text-minimessage/src/test/java/net/kyori/adventure/text/minimessage/PlaceholderResolverTest.java @@ -0,0 +1,78 @@ +/* + * 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; + +import java.util.Arrays; +import java.util.List; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.placeholder.Placeholder; +import net.kyori.adventure.text.minimessage.placeholder.PlaceholderResolver; +import net.kyori.adventure.text.minimessage.placeholder.Replacement; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class PlaceholderResolverTest { + + @Test + void testEmptyBuilder() { + assertEquals(PlaceholderResolver.empty(), PlaceholderResolver.builder().build()); + } + + @Test + void testBuilderUnpacksSingleElement() { + final PlaceholderResolver test = key -> Replacement.miniMessage("hello"); + assertEquals(test, PlaceholderResolver.builder().resolver(test).build()); + } + + @Test + void testSingleAndResolversCombine() { + final List> placeholders = Arrays.asList( + Placeholder.component("foo", Component.text("fizz")), + Placeholder.miniMessage("overlapping", "from list") + ); + final PlaceholderResolver resolver = key -> { + switch (key) { + case "one": return Replacement.miniMessage("fish"); + case "overlapping": return Replacement.miniMessage("from resolver"); + default: return null; + } + }; + + final PlaceholderResolver built = PlaceholderResolver.builder() + .placeholders(placeholders) + .resolver(resolver) + .build(); + + // from placeholders only + assertEquals(Component.text("fizz"), built.resolve("foo").value()); + + // from resolver only + assertEquals("fish", built.resolve("one").value()); + + // shared, resolver takes priority + assertEquals("from resolver", built.resolve("overlapping").value()); + } + +} From e8280949c021f4403976e3d48d62bb255529812a Mon Sep 17 00:00:00 2001 From: zml Date: Thu, 13 Jan 2022 10:03:22 -0800 Subject: [PATCH 06/49] text-minimessage: Make defensive copies of placeholders passed to resolvers --- .../minimessage/placeholder/PlaceholderResolver.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java index 42860f53a..cf85bc93d 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderResolver.java @@ -25,6 +25,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -55,8 +56,8 @@ public interface PlaceholderResolver { /** * Constructs a placeholder resolver from a map. * - *

The provided map is used as the backing for the returned placeholder resolver. - * This means that changes to the map will be reflected in the placeholder resolver.

+ *

The returned placeholder resolver will make a copy of the provided map. + * This means that changes to the map will not be reflected in the placeholder resolver.

* * @param map the map * @return the placeholder resolver @@ -122,7 +123,7 @@ public interface PlaceholderResolver { * @since 4.10.0 */ static @NotNull PlaceholderResolver combining(final @NotNull Iterable resolvers) { - final List copiedResolvers = new ArrayList<>(); + final List copiedResolvers = resolvers instanceof Collection ? new ArrayList<>(((Collection) resolvers).size()) : new ArrayList<>(); for (final PlaceholderResolver resolver : Objects.requireNonNull(resolvers, "resolvers")) { copiedResolvers.add(Objects.requireNonNull(resolver, "resolvers cannot contain null elements")); @@ -145,7 +146,7 @@ public interface PlaceholderResolver { * @return the placeholder resolver * @since 4.10.0 */ - static @NotNull PlaceholderResolver dynamic(final @NotNull Function> resolver) { + static @NotNull PlaceholderResolver dynamic(final @NotNull Function> resolver) { return new DynamicPlaceholderResolver(Objects.requireNonNull(resolver, "resolver")); } From 096a944be15d1e8ed4f3537567ab6c9e8498ff6b Mon Sep 17 00:00:00 2001 From: Riley Park Date: Sat, 15 Jan 2022 12:47:10 -0800 Subject: [PATCH 07/49] Cleanup examination implementations --- .../kyori/adventure/bossbar/BossBarImpl.java | 4 +- .../adventure/identity/IdentityImpl.java | 4 +- .../kyori/adventure/internal/Internals.java | 42 +++++++++++++++++++ .../adventure/internal/package-info.java | 28 +++++++++++++ .../kyori/adventure/inventory/BookImpl.java | 4 +- .../kyori/adventure/pointer/PointerImpl.java | 4 +- .../net/kyori/adventure/sound/SoundImpl.java | 4 +- .../kyori/adventure/sound/SoundStopImpl.java | 4 +- .../adventure/text/AbstractComponent.java | 26 +++--------- .../adventure/text/BlockNBTComponent.java | 12 ++++++ .../adventure/text/BlockNBTComponentImpl.java | 10 ++--- .../net/kyori/adventure/text/Component.java | 10 +++++ .../adventure/text/ComponentInternals.java | 31 ++++++++++++++ .../adventure/text/EntityNBTComponent.java | 12 ++++++ .../text/EntityNBTComponentImpl.java | 12 ++---- .../adventure/text/JoinConfigurationImpl.java | 4 +- .../adventure/text/KeybindComponent.java | 12 ++++++ .../adventure/text/KeybindComponentImpl.java | 12 ++---- .../kyori/adventure/text/NBTComponent.java | 14 +++++++ .../adventure/text/NBTComponentImpl.java | 14 ------- .../kyori/adventure/text/ScoreComponent.java | 14 +++++++ .../adventure/text/ScoreComponentImpl.java | 14 ++----- .../adventure/text/SelectorComponent.java | 13 ++++++ .../adventure/text/SelectorComponentImpl.java | 13 ++---- .../adventure/text/StorageNBTComponent.java | 12 ++++++ .../text/StorageNBTComponentImpl.java | 12 ++---- .../kyori/adventure/text/TextComponent.java | 12 ++++++ .../adventure/text/TextComponentImpl.java | 12 ++---- .../text/TextReplacementConfigImpl.java | 4 +- .../adventure/text/TranslatableComponent.java | 13 ++++++ .../text/TranslatableComponentImpl.java | 12 ++---- .../adventure/text/event/ClickEvent.java | 4 +- .../adventure/text/event/HoverEvent.java | 8 ++-- .../adventure/text/format/StyleImpl.java | 4 +- .../format/TextDecorationAndStateImpl.java | 4 +- .../net/kyori/adventure/title/TitleImpl.java | 6 +-- .../translation/TranslationRegistryImpl.java | 6 +-- .../net/kyori/adventure/util/HSVLikeImpl.java | 4 +- .../placeholder/PlaceholderImpl.java | 4 +- .../placeholder/ReplacementImpl.java | 4 +- .../transformation/Transformation.java | 4 +- 41 files changed, 295 insertions(+), 147 deletions(-) create mode 100644 api/src/main/java/net/kyori/adventure/internal/Internals.java create mode 100644 api/src/main/java/net/kyori/adventure/internal/package-info.java create mode 100644 api/src/main/java/net/kyori/adventure/text/ComponentInternals.java diff --git a/api/src/main/java/net/kyori/adventure/bossbar/BossBarImpl.java b/api/src/main/java/net/kyori/adventure/bossbar/BossBarImpl.java index 29aabe87d..a6a4ee35c 100644 --- a/api/src/main/java/net/kyori/adventure/bossbar/BossBarImpl.java +++ b/api/src/main/java/net/kyori/adventure/bossbar/BossBarImpl.java @@ -33,9 +33,9 @@ import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.Component; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import static java.util.Objects.requireNonNull; @@ -262,6 +262,6 @@ private void forEachListener(final @NotNull Consumer consumer) { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } diff --git a/api/src/main/java/net/kyori/adventure/identity/IdentityImpl.java b/api/src/main/java/net/kyori/adventure/identity/IdentityImpl.java index f2cdd3589..0583f6f41 100644 --- a/api/src/main/java/net/kyori/adventure/identity/IdentityImpl.java +++ b/api/src/main/java/net/kyori/adventure/identity/IdentityImpl.java @@ -24,8 +24,8 @@ package net.kyori.adventure.identity; import java.util.UUID; +import net.kyori.adventure.internal.Internals; import net.kyori.examination.Examinable; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -43,7 +43,7 @@ final class IdentityImpl implements Examinable, Identity { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/internal/Internals.java b/api/src/main/java/net/kyori/adventure/internal/Internals.java new file mode 100644 index 000000000..349eecdca --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/internal/Internals.java @@ -0,0 +1,42 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 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.internal; + +import net.kyori.examination.Examinable; +import net.kyori.examination.string.StringExaminer; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +/** + * Utilities internal to Adventure. + */ +@ApiStatus.Internal +public final class Internals { + private Internals() { + } + + public static @NotNull String toString(final @NotNull Examinable examinable) { + return examinable.examine(StringExaminer.simpleEscaping()); + } +} diff --git a/api/src/main/java/net/kyori/adventure/internal/package-info.java b/api/src/main/java/net/kyori/adventure/internal/package-info.java new file mode 100644 index 000000000..4550cb96a --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/internal/package-info.java @@ -0,0 +1,28 @@ +/* + * This file is part of adventure, licensed under the MIT License. + * + * Copyright (c) 2017-2021 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. + */ +/** + * Utilities that are not considered part of the Public API. + */ +@org.jetbrains.annotations.ApiStatus.Internal +package net.kyori.adventure.internal; diff --git a/api/src/main/java/net/kyori/adventure/inventory/BookImpl.java b/api/src/main/java/net/kyori/adventure/inventory/BookImpl.java index e5d8b1e7a..2a42821da 100644 --- a/api/src/main/java/net/kyori/adventure/inventory/BookImpl.java +++ b/api/src/main/java/net/kyori/adventure/inventory/BookImpl.java @@ -28,9 +28,9 @@ import java.util.Collections; import java.util.List; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.Component; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import static java.util.Objects.requireNonNull; @@ -105,7 +105,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } static final class BuilderImpl implements Book.Builder { diff --git a/api/src/main/java/net/kyori/adventure/pointer/PointerImpl.java b/api/src/main/java/net/kyori/adventure/pointer/PointerImpl.java index 0c3f10463..234671ba2 100644 --- a/api/src/main/java/net/kyori/adventure/pointer/PointerImpl.java +++ b/api/src/main/java/net/kyori/adventure/pointer/PointerImpl.java @@ -23,8 +23,8 @@ */ package net.kyori.adventure.pointer; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -49,7 +49,7 @@ final class PointerImpl implements Pointer { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/sound/SoundImpl.java b/api/src/main/java/net/kyori/adventure/sound/SoundImpl.java index a9f35d9eb..2429ee716 100644 --- a/api/src/main/java/net/kyori/adventure/sound/SoundImpl.java +++ b/api/src/main/java/net/kyori/adventure/sound/SoundImpl.java @@ -24,9 +24,9 @@ package net.kyori.adventure.sound; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.util.ShadyPines; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -102,6 +102,6 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } diff --git a/api/src/main/java/net/kyori/adventure/sound/SoundStopImpl.java b/api/src/main/java/net/kyori/adventure/sound/SoundStopImpl.java index d6ffada4e..12105ea3a 100644 --- a/api/src/main/java/net/kyori/adventure/sound/SoundStopImpl.java +++ b/api/src/main/java/net/kyori/adventure/sound/SoundStopImpl.java @@ -25,9 +25,9 @@ import java.util.Objects; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -75,6 +75,6 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } diff --git a/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java b/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java index 995e5b353..e7f380b82 100644 --- a/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/AbstractComponent.java @@ -78,27 +78,13 @@ public int hashCode() { return result; } - @SuppressWarnings("unused") - private String debuggerString() { - return StringExaminer.simpleEscaping().examine(this.examinableName(), this.examinablePropertiesWithoutChildren()); - } - - protected Stream examinablePropertiesWithoutChildren() { - return Stream.of(ExaminableProperty.of("style", this.style)); - } - @Override - public @NotNull Stream examinableProperties() { - return Stream.concat( - this.examinablePropertiesWithoutChildren(), - Stream.of( - ExaminableProperty.of("children", this.children) - ) - ); - } + public abstract String toString(); - @Override - public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + @SuppressWarnings("unused") + private String debuggerString() { + final Stream examinablePropertiesWithoutChildren = this.examinableProperties() + .filter(property -> !property.name().equals(ComponentInternals.CHILDREN_PROPERTY)); + return StringExaminer.simpleEscaping().examine(this.examinableName(), examinablePropertiesWithoutChildren); } } diff --git a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java index 50f953121..053bf7c58 100644 --- a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java @@ -24,7 +24,9 @@ package net.kyori.adventure.text; import java.util.regex.Matcher; +import java.util.stream.Stream; import net.kyori.examination.Examinable; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -118,6 +120,16 @@ public interface BlockNBTComponent extends NBTComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("pos", this.pos()) + ), + NBTComponent.super.examinableProperties() + ); + } + /** * An NBT component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponentImpl.java index f6db7d0b9..38749d531 100644 --- a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponentImpl.java @@ -27,6 +27,7 @@ import java.util.Objects; import java.util.regex.Pattern; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.util.ShadyPines; import net.kyori.examination.ExaminableProperty; @@ -102,13 +103,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("pos", this.pos) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/Component.java b/api/src/main/java/net/kyori/adventure/text/Component.java index 436bd6ad4..968caa835 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -39,6 +39,7 @@ import java.util.function.UnaryOperator; import java.util.regex.Pattern; import java.util.stream.Collector; +import java.util.stream.Stream; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; @@ -55,6 +56,7 @@ import net.kyori.adventure.util.IntFunction2; import net.kyori.adventure.util.MonkeyBars; import net.kyori.examination.Examinable; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -2336,4 +2338,12 @@ default void componentBuilderApply(final @NotNull ComponentBuilder compone default @NotNull HoverEvent asHoverEvent(final @NotNull UnaryOperator op) { return HoverEvent.showText(op.apply(this)); } + + @Override + default @NotNull Stream examinableProperties() { + return Stream.of( + ExaminableProperty.of("style", this.style()), + ExaminableProperty.of(ComponentInternals.CHILDREN_PROPERTY, this.children()) + ); + } } diff --git a/api/src/main/java/net/kyori/adventure/text/ComponentInternals.java b/api/src/main/java/net/kyori/adventure/text/ComponentInternals.java new file mode 100644 index 000000000..467ff7775 --- /dev/null +++ b/api/src/main/java/net/kyori/adventure/text/ComponentInternals.java @@ -0,0 +1,31 @@ +/* + * 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; + +final class ComponentInternals { + static final String CHILDREN_PROPERTY = "children"; + + private ComponentInternals() { + } +} diff --git a/api/src/main/java/net/kyori/adventure/text/EntityNBTComponent.java b/api/src/main/java/net/kyori/adventure/text/EntityNBTComponent.java index 4ba069f4a..40192dc96 100644 --- a/api/src/main/java/net/kyori/adventure/text/EntityNBTComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/EntityNBTComponent.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -60,6 +62,16 @@ public interface EntityNBTComponent extends NBTComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("selector", this.selector()) + ), + NBTComponent.super.examinableProperties() + ); + } + /** * An entity NBT component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/EntityNBTComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/EntityNBTComponentImpl.java index ceee2babd..4d3de4850 100644 --- a/api/src/main/java/net/kyori/adventure/text/EntityNBTComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/EntityNBTComponentImpl.java @@ -25,9 +25,8 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -101,13 +100,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("selector", this.selector) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java b/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java index 494d69c30..a39575f3f 100644 --- a/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/JoinConfigurationImpl.java @@ -28,8 +28,8 @@ import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -131,7 +131,7 @@ private JoinConfigurationImpl(final @NotNull BuilderImpl builder) { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Contract(pure = true) diff --git a/api/src/main/java/net/kyori/adventure/text/KeybindComponent.java b/api/src/main/java/net/kyori/adventure/text/KeybindComponent.java index a0a581b59..856594d54 100644 --- a/api/src/main/java/net/kyori/adventure/text/KeybindComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/KeybindComponent.java @@ -24,6 +24,8 @@ package net.kyori.adventure.text; import java.util.Objects; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -70,6 +72,16 @@ public interface KeybindComponent extends BuildableComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("keybind", this.keybind()) + ), + BuildableComponent.super.examinableProperties() + ); + } + /** * Something that can provide a keybind identifier. * diff --git a/api/src/main/java/net/kyori/adventure/text/KeybindComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/KeybindComponentImpl.java index ee12dee25..ceeec2b5c 100644 --- a/api/src/main/java/net/kyori/adventure/text/KeybindComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/KeybindComponentImpl.java @@ -25,9 +25,8 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -79,13 +78,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("keybind", this.keybind) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/NBTComponent.java b/api/src/main/java/net/kyori/adventure/text/NBTComponent.java index ed3144ee8..59c756b9a 100644 --- a/api/src/main/java/net/kyori/adventure/text/NBTComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/NBTComponent.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -102,4 +104,16 @@ public interface NBTComponent, B extends NBTCompone * @since 4.8.0 */ @NotNull C separator(final @Nullable ComponentLike separator); + + @Override + default @NotNull Stream examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("nbtPath", this.nbtPath()), + ExaminableProperty.of("interpret", this.interpret()), + ExaminableProperty.of("separator", this.separator()) + ), + BuildableComponent.super.examinableProperties() + ); + } } diff --git a/api/src/main/java/net/kyori/adventure/text/NBTComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/NBTComponentImpl.java index d6ca69b6c..64631aada 100644 --- a/api/src/main/java/net/kyori/adventure/text/NBTComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/NBTComponentImpl.java @@ -25,9 +25,7 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -74,18 +72,6 @@ public int hashCode() { return result; } - @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("nbtPath", this.nbtPath), - ExaminableProperty.of("interpret", this.interpret), - ExaminableProperty.of("separator", this.separator) - ), - super.examinablePropertiesWithoutChildren() - ); - } - static abstract class BuilderImpl, B extends NBTComponentBuilder> extends AbstractComponentBuilder implements NBTComponentBuilder { protected @Nullable String nbtPath; protected boolean interpret = INTERPRET_DEFAULT; diff --git a/api/src/main/java/net/kyori/adventure/text/ScoreComponent.java b/api/src/main/java/net/kyori/adventure/text/ScoreComponent.java index 82b20eee6..efdd8afd4 100644 --- a/api/src/main/java/net/kyori/adventure/text/ScoreComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/ScoreComponent.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -108,6 +110,18 @@ public interface ScoreComponent extends BuildableComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("name", this.name()), + ExaminableProperty.of("objective", this.objective()), + ExaminableProperty.of("value", this.value()) + ), + BuildableComponent.super.examinableProperties() + ); + } + /** * A score component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/ScoreComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/ScoreComponentImpl.java index 83c7e4017..fd78dece5 100644 --- a/api/src/main/java/net/kyori/adventure/text/ScoreComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/ScoreComponentImpl.java @@ -25,9 +25,8 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -113,15 +112,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("name", this.name), - ExaminableProperty.of("objective", this.objective), - ExaminableProperty.of("value", this.value) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/SelectorComponent.java b/api/src/main/java/net/kyori/adventure/text/SelectorComponent.java index d392c3d54..e4c6f58e1 100644 --- a/api/src/main/java/net/kyori/adventure/text/SelectorComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/SelectorComponent.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -78,6 +80,17 @@ public interface SelectorComponent extends BuildableComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("pattern", this.pattern()), + ExaminableProperty.of("separator", this.separator()) + ), + BuildableComponent.super.examinableProperties() + ); + } + /** * A selector component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/SelectorComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/SelectorComponentImpl.java index 40df51e59..5e71557ed 100644 --- a/api/src/main/java/net/kyori/adventure/text/SelectorComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/SelectorComponentImpl.java @@ -25,9 +25,8 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -92,14 +91,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("pattern", this.pattern), - ExaminableProperty.of("separator", this.separator) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/StorageNBTComponent.java b/api/src/main/java/net/kyori/adventure/text/StorageNBTComponent.java index 6f5159373..26e05f08e 100644 --- a/api/src/main/java/net/kyori/adventure/text/StorageNBTComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/StorageNBTComponent.java @@ -23,7 +23,9 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; import net.kyori.adventure.key.Key; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -61,6 +63,16 @@ public interface StorageNBTComponent extends NBTComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("storage", this.storage()) + ), + NBTComponent.super.examinableProperties() + ); + } + /** * A command storage NBT component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/StorageNBTComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/StorageNBTComponentImpl.java index 2e2067306..9bb23744d 100644 --- a/api/src/main/java/net/kyori/adventure/text/StorageNBTComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/StorageNBTComponentImpl.java @@ -25,10 +25,9 @@ import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -102,13 +101,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("storage", this.storage) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/TextComponent.java b/api/src/main/java/net/kyori/adventure/text/TextComponent.java index bdaac9d8d..1a7291e7a 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/TextComponent.java @@ -23,6 +23,8 @@ */ package net.kyori.adventure.text; +import java.util.stream.Stream; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -71,6 +73,16 @@ public interface TextComponent extends BuildableComponent examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("content", this.content()) + ), + BuildableComponent.super.examinableProperties() + ); + } + /** * A text component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java index 0e2bf7388..4b6305035 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java @@ -26,10 +26,9 @@ import java.util.Collections; import java.util.List; import java.util.Objects; -import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.util.Nag; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.VisibleForTesting; @@ -109,13 +108,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("content", this.content) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java index 75b95f684..5f4b7bd40 100644 --- a/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TextReplacementConfigImpl.java @@ -27,8 +27,8 @@ import java.util.regex.MatchResult; import java.util.regex.Pattern; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -70,7 +70,7 @@ TextReplacementRenderer.State createState() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } static final class Builder implements TextReplacementConfig.Builder { diff --git a/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java b/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java index 19bf104dc..a8082f20e 100644 --- a/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/TranslatableComponent.java @@ -26,10 +26,12 @@ import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.stream.Stream; import net.kyori.adventure.audience.Audience; import net.kyori.adventure.translation.GlobalTranslator; import net.kyori.adventure.translation.Translatable; import net.kyori.adventure.translation.TranslationRegistry; +import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -117,6 +119,17 @@ public interface TranslatableComponent extends BuildableComponent args); + @Override + default @NotNull Stream examinableProperties() { + return Stream.concat( + Stream.of( + ExaminableProperty.of("key", this.key()), + ExaminableProperty.of("args", this.args()) + ), + BuildableComponent.super.examinableProperties() + ); + } + /** * A text component builder. * diff --git a/api/src/main/java/net/kyori/adventure/text/TranslatableComponentImpl.java b/api/src/main/java/net/kyori/adventure/text/TranslatableComponentImpl.java index e01f93856..c47aae19b 100644 --- a/api/src/main/java/net/kyori/adventure/text/TranslatableComponentImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/TranslatableComponentImpl.java @@ -29,8 +29,8 @@ import java.util.Objects; import java.util.stream.Collectors; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; -import net.kyori.examination.ExaminableProperty; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -105,14 +105,8 @@ public int hashCode() { } @Override - protected @NotNull Stream examinablePropertiesWithoutChildren() { - return Stream.concat( - Stream.of( - ExaminableProperty.of("key", this.key), - ExaminableProperty.of("args", this.args) - ), - super.examinablePropertiesWithoutChildren() - ); + public String toString() { + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/event/ClickEvent.java b/api/src/main/java/net/kyori/adventure/text/event/ClickEvent.java index e771bd773..5adbcd0e7 100644 --- a/api/src/main/java/net/kyori/adventure/text/event/ClickEvent.java +++ b/api/src/main/java/net/kyori/adventure/text/event/ClickEvent.java @@ -26,12 +26,12 @@ import java.net.URL; import java.util.Objects; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.format.Style; import net.kyori.adventure.text.format.StyleBuilderApplicable; import net.kyori.adventure.util.Index; import net.kyori.examination.Examinable; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -206,7 +206,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } /** diff --git a/api/src/main/java/net/kyori/adventure/text/event/HoverEvent.java b/api/src/main/java/net/kyori/adventure/text/event/HoverEvent.java index 997f705ad..bd5f7c486 100644 --- a/api/src/main/java/net/kyori/adventure/text/event/HoverEvent.java +++ b/api/src/main/java/net/kyori/adventure/text/event/HoverEvent.java @@ -27,6 +27,7 @@ import java.util.UUID; import java.util.function.UnaryOperator; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; import net.kyori.adventure.key.Keyed; import net.kyori.adventure.nbt.api.BinaryTagHolder; @@ -38,7 +39,6 @@ import net.kyori.adventure.util.Index; import net.kyori.examination.Examinable; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Range; @@ -318,7 +318,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } /** @@ -480,7 +480,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } @@ -654,7 +654,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } diff --git a/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java b/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java index cb9a825b2..85d0ac815 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/format/StyleImpl.java @@ -27,12 +27,12 @@ import java.util.Objects; import java.util.Set; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.event.HoverEvent; import net.kyori.adventure.text.event.HoverEventSource; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -239,7 +239,7 @@ public boolean isEmpty() { @Override public @NotNull String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/text/format/TextDecorationAndStateImpl.java b/api/src/main/java/net/kyori/adventure/text/format/TextDecorationAndStateImpl.java index ddd9f5812..6011e8319 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/TextDecorationAndStateImpl.java +++ b/api/src/main/java/net/kyori/adventure/text/format/TextDecorationAndStateImpl.java @@ -23,7 +23,7 @@ */ package net.kyori.adventure.text.format; -import net.kyori.examination.string.StringExaminer; +import net.kyori.adventure.internal.Internals; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -52,7 +52,7 @@ final class TextDecorationAndStateImpl implements TextDecorationAndState { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Override diff --git a/api/src/main/java/net/kyori/adventure/title/TitleImpl.java b/api/src/main/java/net/kyori/adventure/title/TitleImpl.java index 9a1eb3fac..256a8ccc0 100644 --- a/api/src/main/java/net/kyori/adventure/title/TitleImpl.java +++ b/api/src/main/java/net/kyori/adventure/title/TitleImpl.java @@ -26,9 +26,9 @@ import java.time.Duration; import java.util.Objects; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.Component; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; @@ -105,7 +105,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } static class TimesImpl implements Times { @@ -163,7 +163,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } } diff --git a/api/src/main/java/net/kyori/adventure/translation/TranslationRegistryImpl.java b/api/src/main/java/net/kyori/adventure/translation/TranslationRegistryImpl.java index d68555079..978fb4dbf 100644 --- a/api/src/main/java/net/kyori/adventure/translation/TranslationRegistryImpl.java +++ b/api/src/main/java/net/kyori/adventure/translation/TranslationRegistryImpl.java @@ -29,10 +29,10 @@ import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.key.Key; import net.kyori.examination.Examinable; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -103,7 +103,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } final class Translation implements Examinable { @@ -159,7 +159,7 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } } diff --git a/api/src/main/java/net/kyori/adventure/util/HSVLikeImpl.java b/api/src/main/java/net/kyori/adventure/util/HSVLikeImpl.java index 74dfe42d9..c897a8d66 100644 --- a/api/src/main/java/net/kyori/adventure/util/HSVLikeImpl.java +++ b/api/src/main/java/net/kyori/adventure/util/HSVLikeImpl.java @@ -24,7 +24,7 @@ package net.kyori.adventure.util; import java.util.Objects; -import net.kyori.examination.string.StringExaminer; +import net.kyori.adventure.internal.Internals; import org.jetbrains.annotations.Nullable; final class HSVLikeImpl implements HSVLike { @@ -78,6 +78,6 @@ public int hashCode() { @Override public String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } } diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java index 9c9eb560e..8566b2696 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/PlaceholderImpl.java @@ -26,8 +26,8 @@ import java.util.Locale; import java.util.Objects; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; final class PlaceholderImpl implements Placeholder { @@ -65,6 +65,6 @@ static void checkKey(final @NotNull String key) { @Override public String toString() { - return StringExaminer.simpleEscaping().examine(this); + return Internals.toString(this); } } diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/ReplacementImpl.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/ReplacementImpl.java index ce9aa8df2..3fc64ed52 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/ReplacementImpl.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/placeholder/ReplacementImpl.java @@ -24,8 +24,8 @@ package net.kyori.adventure.text.minimessage.placeholder; import java.util.stream.Stream; +import net.kyori.adventure.internal.Internals; import net.kyori.examination.ExaminableProperty; -import net.kyori.examination.string.StringExaminer; import org.jetbrains.annotations.NotNull; final class ReplacementImpl implements Replacement { @@ -47,6 +47,6 @@ final class ReplacementImpl implements Replacement { @Override public String toString() { - return StringExaminer.simpleEscaping().examine(this); + return Internals.toString(this); } } diff --git a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/Transformation.java b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/Transformation.java index 8174a627c..a293323ef 100644 --- a/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/Transformation.java +++ b/text-minimessage/src/main/java/net/kyori/adventure/text/minimessage/transformation/Transformation.java @@ -23,9 +23,9 @@ */ package net.kyori.adventure.text.minimessage.transformation; +import net.kyori.adventure.internal.Internals; import net.kyori.adventure.text.Component; import net.kyori.examination.Examinable; -import net.kyori.examination.string.StringExaminer; /** * A transformation that can be applied while parsing a message. @@ -50,7 +50,7 @@ protected Transformation() { @Override public final String toString() { - return this.examine(StringExaminer.simpleEscaping()); + return Internals.toString(this); } @Override From 1e681b0f4c3c6f3f31eb686a1db8aaa9df302292 Mon Sep 17 00:00:00 2001 From: Riley Park Date: Thu, 20 Jan 2022 19:09:38 -0800 Subject: [PATCH 08/49] oui --- api/src/main/java/net/kyori/adventure/internal/Internals.java | 2 +- .../main/java/net/kyori/adventure/internal/package-info.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/internal/Internals.java b/api/src/main/java/net/kyori/adventure/internal/Internals.java index 349eecdca..1d2e3bca9 100644 --- a/api/src/main/java/net/kyori/adventure/internal/Internals.java +++ b/api/src/main/java/net/kyori/adventure/internal/Internals.java @@ -1,7 +1,7 @@ /* * This file is part of adventure, licensed under the MIT License. * - * Copyright (c) 2017-2021 KyoriPowered + * 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 diff --git a/api/src/main/java/net/kyori/adventure/internal/package-info.java b/api/src/main/java/net/kyori/adventure/internal/package-info.java index 4550cb96a..d7cc37b01 100644 --- a/api/src/main/java/net/kyori/adventure/internal/package-info.java +++ b/api/src/main/java/net/kyori/adventure/internal/package-info.java @@ -1,7 +1,7 @@ /* * This file is part of adventure, licensed under the MIT License. * - * Copyright (c) 2017-2021 KyoriPowered + * 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 From c2a72a6b5cc3d0b4fa406158fb0da39858fcf3a1 Mon Sep 17 00:00:00 2001 From: zml Date: Thu, 20 Jan 2022 19:48:59 -0800 Subject: [PATCH 09/49] build: Suppress Javadoc style errors in internal package --- .checkstyle/suppressions.xml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.checkstyle/suppressions.xml b/.checkstyle/suppressions.xml index df0c57a6a..48291911d 100644 --- a/.checkstyle/suppressions.xml +++ b/.checkstyle/suppressions.xml @@ -4,10 +4,13 @@ - + + + + From 420d99842244f7aa68bf0e37b8cec9e3550b0bb3 Mon Sep 17 00:00:00 2001 From: KingSimon <19822231+KingOfSquares@users.noreply.github.com> Date: Wed, 22 Dec 2021 16:42:53 +0100 Subject: [PATCH 10/49] chore: Rename some static constructer methods --- .../adventure/nbt/api/BinaryTagHolder.java | 15 +++++ .../adventure/text/BlockNBTComponent.java | 64 ++++++++++++++++--- .../net/kyori/adventure/text/Component.java | 2 +- .../adventure/text/ComponentBuilder.java | 2 +- .../adventure/text/format/NamedTextColor.java | 33 ++++++++++ .../kyori/adventure/text/format/Style.java | 26 ++++++-- .../adventure/text/format/TextColor.java | 2 +- .../TranslatableComponentRenderer.java | 2 +- .../translation/GlobalTranslator.java | 14 ++++ .../java/net/kyori/adventure/util/Codec.java | 30 +++++++++ .../net/kyori/adventure/util/HSVLike.java | 17 +++++ .../adventure/text/BlockNBTComponentTest.java | 26 ++++---- .../adventure/text/format/StyleTest.java | 2 +- .../translation/GlobalTranslatorTest.java | 20 +++--- .../net/kyori/adventure/util/HSVLikeTest.java | 12 ++-- .../net/kyori/adventure/key/KeyedValue.java | 17 +++++ .../kyori/adventure/key/KeyedValueTest.java | 6 +- .../HoverEventShowItemSerializer.java | 2 +- .../HoverEventSerializersTest.java | 2 +- .../HoverEventShowItemSerializer.java | 2 +- .../HoverEventSerializersTest.java | 2 +- .../inbuild/HoverTransformation.java | 2 +- .../NBTLegacyHoverEventSerializerImpl.java | 2 +- .../ComponentDeserializationBenchmark.java | 2 +- .../gson/ComponentSerializationBenchmark.java | 2 +- .../serializer/gson/ShowItemSerializer.java | 4 +- .../gson/ShowItemSerializerTest.java | 2 +- 27 files changed, 250 insertions(+), 62 deletions(-) diff --git a/api/src/main/java/net/kyori/adventure/nbt/api/BinaryTagHolder.java b/api/src/main/java/net/kyori/adventure/nbt/api/BinaryTagHolder.java index 4bdf67284..f0ab06b36 100644 --- a/api/src/main/java/net/kyori/adventure/nbt/api/BinaryTagHolder.java +++ b/api/src/main/java/net/kyori/adventure/nbt/api/BinaryTagHolder.java @@ -24,6 +24,7 @@ package net.kyori.adventure.nbt.api; import net.kyori.adventure.util.Codec; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; /** @@ -52,13 +53,27 @@ public interface BinaryTagHolder { return new BinaryTagHolderImpl(codec.encode(nbt)); } + /** + * Creates an encoded binary tag holder. + * + * @param string the encoded binary tag value + * @return the encoded binary tag + * @since 4.10.0 + */ + static @NotNull BinaryTagHolder binaryTagHolder(final @NotNull String string) { + return new BinaryTagHolderImpl(string); + } + /** * Creates an encoded binary tag holder. * * @param string the encoded binary tag value * @return the encoded binary tag * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #binaryTagHolder(String)} instead. */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") static @NotNull BinaryTagHolder of(final @NotNull String string) { return new BinaryTagHolderImpl(string); } diff --git a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java index 053bf7c58..75ba64189 100644 --- a/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java +++ b/api/src/main/java/net/kyori/adventure/text/BlockNBTComponent.java @@ -27,6 +27,7 @@ import java.util.stream.Stream; import net.kyori.examination.Examinable; import net.kyori.examination.ExaminableProperty; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; @@ -75,7 +76,7 @@ public interface BlockNBTComponent extends NBTComponent { */ @Contract("_, _, _ -> this") default @NotNull Builder localPos(final double left, final double up, final double forwards) { - return this.pos(LocalPos.of(left, up, forwards)); + return this.pos(LocalPos.localPos(left, up, forwards)); } /** @@ -171,7 +172,7 @@ interface Builder extends NBTComponentBuilder { */ @Contract("_, _, _ -> this") default @NotNull Builder worldPos(final WorldPos.@NotNull Coordinate x, final WorldPos.@NotNull Coordinate y, final WorldPos.@NotNull Coordinate z) { - return this.pos(WorldPos.of(x, y, z)); + return this.pos(WorldPos.worldPos(x, y, z)); } /** @@ -224,7 +225,7 @@ interface Pos extends Examinable { static @NotNull Pos fromString(final @NotNull String input) throws IllegalArgumentException { final Matcher localMatch = BlockNBTComponentImpl.Tokens.LOCAL_PATTERN.matcher(input); if (localMatch.matches()) { - return BlockNBTComponent.LocalPos.of( + return BlockNBTComponent.LocalPos.localPos( Double.parseDouble(localMatch.group(1)), Double.parseDouble(localMatch.group(3)), Double.parseDouble(localMatch.group(5)) @@ -233,7 +234,7 @@ interface Pos extends Examinable { final Matcher worldMatch = BlockNBTComponentImpl.Tokens.WORLD_PATTERN.matcher(input); if (worldMatch.matches()) { - return BlockNBTComponent.WorldPos.of( + return BlockNBTComponent.WorldPos.worldPos( BlockNBTComponentImpl.Tokens.deserializeCoordinate(worldMatch.group(1), worldMatch.group(2)), BlockNBTComponentImpl.Tokens.deserializeCoordinate(worldMatch.group(3), worldMatch.group(4)), BlockNBTComponentImpl.Tokens.deserializeCoordinate(worldMatch.group(5), worldMatch.group(6)) @@ -259,6 +260,19 @@ interface Pos extends Examinable { * @since 4.0.0 */ interface LocalPos extends Pos { + /** + * Creates a local position with the given values. + * + * @param left the left value + * @param up the up value + * @param forwards the forwards value + * @return a local position + * @since 4.10.0 + */ + static @NotNull LocalPos localPos(final double left, final double up, final double forwards) { + return new BlockNBTComponentImpl.LocalPosImpl(left, up, forwards); + } + /** * Creates a local position with the given values. * @@ -267,7 +281,10 @@ interface LocalPos extends Pos { * @param forwards the forwards value * @return a local position * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #localPos(double, double, double)} instead. */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") static @NotNull LocalPos of(final double left, final double up, final double forwards) { return new BlockNBTComponentImpl.LocalPosImpl(left, up, forwards); } @@ -303,6 +320,19 @@ interface LocalPos extends Pos { * @since 4.0.0 */ interface WorldPos extends Pos { + /** + * Creates a world position with the given coordinates. + * + * @param x the x coordinate + * @param y the y coordinate + * @param z the z coordinate + * @return a world position + * @since 4.10.0 + */ + static @NotNull WorldPos worldPos(final @NotNull Coordinate x, final @NotNull Coordinate y, final @NotNull Coordinate z) { + return new BlockNBTComponentImpl.WorldPosImpl(x, y, z); + } + /** * Creates a world position with the given coordinates. * @@ -311,7 +341,10 @@ interface WorldPos extends Pos { * @param z the z coordinate * @return a world position * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #worldPos(WorldPos.Coordinate, WorldPos.Coordinate, WorldPos.Coordinate)} instead. */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") static @NotNull WorldPos of(final @NotNull Coordinate x, final @NotNull Coordinate y, final @NotNull Coordinate z) { return new BlockNBTComponentImpl.WorldPosImpl(x, y, z); } @@ -354,7 +387,7 @@ interface Coordinate extends Examinable { * @since 4.0.0 */ static @NotNull Coordinate absolute(final int value) { - return of(value, Type.ABSOLUTE); + return coordinate(value, Type.ABSOLUTE); } /** @@ -365,7 +398,19 @@ interface Coordinate extends Examinable { * @since 4.0.0 */ static @NotNull Coordinate relative(final int value) { - return of(value, Type.RELATIVE); + return coordinate(value, Type.RELATIVE); + } + + /** + * Creates a coordinate with the given value and type. + * + * @param value the value + * @param type the type + * @return a coordinate + * @since 4.10.0 + */ + static @NotNull Coordinate coordinate(final int value, final @NotNull Type type) { + return new BlockNBTComponentImpl.WorldPosImpl.CoordinateImpl(value, type); } /** @@ -375,7 +420,10 @@ interface Coordinate extends Examinable { * @param type the type * @return a coordinate * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #coordinate(int, Coordinate.Type)} instead. */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") static @NotNull Coordinate of(final int value, final @NotNull Type type) { return new BlockNBTComponentImpl.WorldPosImpl.CoordinateImpl(value, type); } diff --git a/api/src/main/java/net/kyori/adventure/text/Component.java b/api/src/main/java/net/kyori/adventure/text/Component.java index 968caa835..72dd19917 100644 --- a/api/src/main/java/net/kyori/adventure/text/Component.java +++ b/api/src/main/java/net/kyori/adventure/text/Component.java @@ -1809,7 +1809,7 @@ default void detectCycle(final @NotNull Component that) { */ @Contract(pure = true) default @NotNull Component mergeStyle(final @NotNull Component that, final Style.@NotNull Merge@NotNull... merges) { - return this.mergeStyle(that, Style.Merge.of(merges)); + return this.mergeStyle(that, Style.Merge.merges(merges)); } /** diff --git a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java index ca3bd32f1..e4a795376 100644 --- a/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java +++ b/api/src/main/java/net/kyori/adventure/text/ComponentBuilder.java @@ -362,7 +362,7 @@ public interface ComponentBuilder, B extends */ @Contract("_, _ -> this") default @NotNull B mergeStyle(final @NotNull Component that, final Style.@NotNull Merge@NotNull... merges) { - return this.mergeStyle(that, Style.Merge.of(merges)); + return this.mergeStyle(that, Style.Merge.merges(merges)); } /** diff --git a/api/src/main/java/net/kyori/adventure/text/format/NamedTextColor.java b/api/src/main/java/net/kyori/adventure/text/format/NamedTextColor.java index 1c9a453c6..aea34f152 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/NamedTextColor.java +++ b/api/src/main/java/net/kyori/adventure/text/format/NamedTextColor.java @@ -30,6 +30,7 @@ import net.kyori.adventure.util.HSVLike; import net.kyori.adventure.util.Index; import net.kyori.examination.ExaminableProperty; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -163,13 +164,45 @@ public final class NamedTextColor implements TextColor { */ public static final Index NAMES = Index.create(constant -> constant.name, VALUES); + /** + * Gets the named color exactly matching the provided color. + * + * @param value the color to match + * @return the matched color, or null + * @since 4.10.0 + */ + public static @Nullable NamedTextColor namedColor(final int value) { + switch (value) { + case BLACK_VALUE: return BLACK; + case DARK_BLUE_VALUE: return DARK_BLUE; + case DARK_GREEN_VALUE: return DARK_GREEN; + case DARK_AQUA_VALUE: return DARK_AQUA; + case DARK_RED_VALUE: return DARK_RED; + case DARK_PURPLE_VALUE: return DARK_PURPLE; + case GOLD_VALUE: return GOLD; + case GRAY_VALUE: return GRAY; + case DARK_GRAY_VALUE: return DARK_GRAY; + case BLUE_VALUE: return BLUE; + case GREEN_VALUE: return GREEN; + case AQUA_VALUE: return AQUA; + case RED_VALUE: return RED; + case LIGHT_PURPLE_VALUE: return LIGHT_PURPLE; + case YELLOW_VALUE: return YELLOW; + case WHITE_VALUE: return WHITE; + default: return null; + } + } + /** * Gets the named color exactly matching the provided color. * * @param value the color to match * @return the matched color, or null * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #namedColor(int)} instead */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") public static @Nullable NamedTextColor ofExact(final int value) { switch (value) { case BLACK_VALUE: return BLACK; diff --git a/api/src/main/java/net/kyori/adventure/text/format/Style.java b/api/src/main/java/net/kyori/adventure/text/format/Style.java index b4ba3b300..264b9e26c 100644 --- a/api/src/main/java/net/kyori/adventure/text/format/Style.java +++ b/api/src/main/java/net/kyori/adventure/text/format/Style.java @@ -478,7 +478,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ default @NotNull Style merge(final @NotNull Style that, final @NotNull Merge@NotNull... merges) { - return this.merge(that, Merge.of(merges)); + return this.merge(that, Merge.merges(merges)); } /** @@ -491,7 +491,7 @@ default boolean hasDecoration(final @NotNull TextDecoration decoration) { * @since 4.0.0 */ default @NotNull Style merge(final @NotNull Style that, final Merge.@NotNull Strategy strategy, final @NotNull Merge@NotNull... merges) { - return this.merge(that, strategy, Merge.of(merges)); + return this.merge(that, strategy, Merge.merges(merges)); } /** @@ -571,8 +571,8 @@ enum Merge { */ FONT; - static final Set ALL = of(values()); - static final Set COLOR_AND_DECORATIONS = of(COLOR, DECORATIONS); + static final Set ALL = merges(values()); + static final Set COLOR_AND_DECORATIONS = merges(COLOR, DECORATIONS); /** * Gets a merge set of all merge types. @@ -594,13 +594,27 @@ enum Merge { return COLOR_AND_DECORATIONS; } + /** + * Creates a merge set. + * + * @param merges the merge parts + * @return a merge set + * @since 4.10.0 + */ + public static @Unmodifiable @NotNull Set merges(final Merge@NotNull... merges) { + return MonkeyBars.enumSet(Merge.class, merges); + } + /** * Creates a merge set. * * @param merges the merge parts * @return a merge set * @since 4.0.0 + * @deprecated for removal since 4.10.0, use {@link #merges(Style.Merge...)} instead. */ + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "5.0.0") public static @Unmodifiable @NotNull Set of(final Merge@NotNull... merges) { return MonkeyBars.enumSet(Merge.class, merges); } @@ -816,7 +830,7 @@ interface Builder extends Buildable.Builder