new ChatUtil.joinGrammatically() method, using components

This commit is contained in:
Marc Baloup 2023-06-20 18:38:07 +02:00
parent 5edd8cdfec
commit a4b33a1af7
2 changed files with 36 additions and 16 deletions

View File

@ -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 :
* <code>"a thing<u>, </u>a thing<u>and </u>a thing"</code>
* (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<ComponentLike> 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<Component> spacedRows = new ArrayList<>(data.size());
for (List<Component> 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;

View File

@ -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 : <code>"a thing<u>, </u>a thing<u> and </u>a thing"</code>
* It is useful when enumerating things in a sentence, for instance :
* <code>"a thing<u>, </u>a thing<u> and </u>a thing"</code>
* (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<String> strings) {
int size = strings == null ? 0 : strings.size();