Some refactoring

This commit is contained in:
nossr50
2020-11-04 12:12:51 -08:00
parent 329de942b4
commit 15578bb84e
62 changed files with 170 additions and 201 deletions

View File

@ -0,0 +1,87 @@
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));
}
}