123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package com.gmail.nossr50.util.text;
- import net.kyori.adventure.text.Component;
- import net.kyori.adventure.text.ComponentBuilder;
- import net.kyori.adventure.text.TextComponent;
- import net.kyori.adventure.text.event.HoverEvent;
- import net.kyori.adventure.text.format.NamedTextColor;
- import org.jetbrains.annotations.NotNull;
- import org.jetbrains.annotations.Nullable;
- import java.util.List;
- public class TextUtils {
- /**
- * Makes a single component from an array of components, can optionally add prefixes and suffixes to come before and after each component
- * @param componentsArray target array
- * @return a component with optional styling built from an array
- */
- static @NotNull Component fromArray(@NotNull Component[] componentsArray, @Nullable Component prefixComponent, @Nullable Component suffixComponent) {
- TextComponent.Builder componentBuilder = Component.text();
- for(Component component : componentsArray) {
- if(component == null) //Individual elements can be null
- continue;
- if(prefixComponent != null)
- componentBuilder.append(prefixComponent);
- componentBuilder.append(component);
- if(suffixComponent != null)
- componentBuilder.append(suffixComponent);
- }
- return componentBuilder.build();
- }
- /**
- * Takes a list of components and splits them into arrays each with a maximum element limit
- * Individual elements in [][X] may be null
- *
- * @param components target component list
- * @param groupsSize maximum size per array
- * @return a 2D array with components split into groups
- */
- static @NotNull Component[][] splitComponentsIntoGroups(@NotNull List<Component> components, int groupsSize) {
- int groupCount = (int) Math.ceil((double) components.size() / (double) groupsSize);
- Component[][] splitGroups = new Component[groupCount][groupsSize];
- int groupsFinished = 0;
- while (groupsFinished < groupCount) {
- //Fill group with members
- for(int i = 0; i < groupsSize; i++) {
- int indexOfPotentialMember = i + (groupsFinished * 3); //Groups don't always fill all members neatly
- //Some groups won't have entirely non-null elements
- if(indexOfPotentialMember > components.size()-1) {
- break;
- }
- Component potentialMember = components.get(indexOfPotentialMember);
- //Make sure the potential member exists because of rounding
- if(potentialMember != null) {
- splitGroups[groupsFinished][i] = potentialMember;
- }
- }
- //Another group is finished
- groupsFinished++;
- }
- return splitGroups;
- }
- static void addChildWebComponent(@NotNull ComponentBuilder<?, ?> webTextComponent, @NotNull String childName) {
- TextComponent childComponent = Component.text(childName).color(NamedTextColor.BLUE);
- webTextComponent.append(childComponent);
- }
- static void addNewHoverComponentToTextComponent(@NotNull TextComponent.Builder textComponent, @NotNull Component baseComponent) {
- textComponent.hoverEvent(HoverEvent.showText(baseComponent));
- }
- }
|