From a4b33a1af7fd41f4e4ad634cb942d8792ff4e922 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 20 Jun 2023 18:38:07 +0200 Subject: [PATCH] new ChatUtil.joinGrammatically() method, using components --- .../java/fr/pandacube/lib/chat/ChatUtil.java | 35 +++++++++++++++++-- .../fr/pandacube/lib/util/StringUtil.java | 17 ++------- 2 files changed, 36 insertions(+), 16 deletions(-) diff --git a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java index 0e33152..89f74ba 100644 --- a/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java +++ b/pandalib-chat/src/main/java/fr/pandacube/lib/chat/ChatUtil.java @@ -11,6 +11,7 @@ import java.util.TreeSet; import java.util.stream.Collectors; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import net.kyori.adventure.text.TextComponent; import net.kyori.adventure.text.TranslatableComponent; import net.kyori.adventure.text.format.NamedTextColor; @@ -21,6 +22,8 @@ import net.md_5.bungee.api.ChatColor; import fr.pandacube.lib.chat.Chat.FormatableChat; +import static fr.pandacube.lib.chat.ChatStatic.chat; + /** * Provides various methods and properties to manipulate text displayed in chat and other parts of the game. */ @@ -127,7 +130,7 @@ public class ChatUtil { pagesToDisplay.add(i); } - Chat d = ChatStatic.chat().thenLegacyText(prefix); + Chat d = chat().thenLegacyText(prefix); boolean first = true; int previous = 0; @@ -167,6 +170,34 @@ public class ChatUtil { + /** + * Do like {@link String#join(CharSequence, Iterable)}, but for components, and the last separator is different from + * the others. It is useful when enumerating things in a sentence, for instance : + * "a thing, a thingand a thing" + * (the coma being the usual separator, and {@code " and "} being the final separator). + * @param regularSeparator the separator used everywhere except between the two last components to join. + * @param finalSeparator the separator used between the two last components to join. + * @param elements the components to join. + * @return a new {@link Chat} instance with all the provided {@code component} joined using the separators. + */ + public static Chat joinGrammatically(ComponentLike regularSeparator, ComponentLike finalSeparator, List elements) { + int size = elements == null ? 0 : elements.size(); + int last = size - 1; + Chat c = chat(); + for (int i = 0; i < size; i++) { + if (i > 0) { + c.then(i < last ? regularSeparator : finalSeparator); + } + c.then(elements.get(i)); + } + return c; + } + + + + + + @@ -416,7 +447,7 @@ public class ChatUtil { // create the lines with appropriate spacing List spacedRows = new ArrayList<>(data.size()); for (List row : data) { - Chat spacedRow = Chat.chat(); + Chat spacedRow = chat(); for (int i = 0; i < row.size() - 1; i++) { int w = componentWidth(row.get(i), console); int padding = nbPixelPerColumn.get(i) - w; diff --git a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java index a9afe10..423e5ca 100644 --- a/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java +++ b/pandalib-util/src/main/java/fr/pandacube/lib/util/StringUtil.java @@ -23,18 +23,6 @@ public class StringUtil { return String.valueOf(d); } - /** - * Counts the number of occurrence of a specified character in a string. - * @param string the character sequence to search into. - * @param character the character to count. - * @return the number of occurrence of - * @deprecated Because it uses snake_case naming convention. Use {@link #countOccurrences(CharSequence, char)} instead. - */ - @Deprecated(forRemoval = true, since = "2022-07-26") - public static int char_count(CharSequence string, char character) { - return countOccurrences(string, character); - } - /** * Counts the number of occurrence of a specified character in a string. * @param string the character sequence to search into. @@ -54,12 +42,13 @@ public class StringUtil { /** * Do like {@link String#join(CharSequence, Iterable)}, but the last separator is different from the others. - * It is useful when enumerating thins in a sentence, for instance : "a thing, a thing and a thing" + * It is useful when enumerating things in a sentence, for instance : + * "a thing, a thing and a thing" * (the coma being the usual separator, and {@code " and "} being the final separator). * @param regularSeparator the separator used everywhere except between the two last strings to join. * @param finalSeparator the separator used between the two last strings to join. * @param strings the strings to join. - * @return a new string will all the provided {@code strings} joined using the separators. + * @return a new string with all the provided {@code strings} joined using the separators. */ public static String joinGrammatically(CharSequence regularSeparator, CharSequence finalSeparator, List strings) { int size = strings == null ? 0 : strings.size();