TextUtils.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package com.gmail.nossr50.util.text;
  2. import net.kyori.adventure.text.Component;
  3. import net.kyori.adventure.text.ComponentBuilder;
  4. import net.kyori.adventure.text.TextComponent;
  5. import net.kyori.adventure.text.event.HoverEvent;
  6. import net.kyori.adventure.text.format.NamedTextColor;
  7. import org.jetbrains.annotations.NotNull;
  8. import org.jetbrains.annotations.Nullable;
  9. import java.util.List;
  10. public class TextUtils {
  11. /**
  12. * Makes a single component from an array of components, can optionally add prefixes and suffixes to come before and after each component
  13. * @param componentsArray target array
  14. * @return a component with optional styling built from an array
  15. */
  16. static @NotNull Component fromArray(@NotNull Component[] componentsArray, @Nullable Component prefixComponent, @Nullable Component suffixComponent) {
  17. TextComponent.Builder componentBuilder = Component.text();
  18. for(Component component : componentsArray) {
  19. if(component == null) //Individual elements can be null
  20. continue;
  21. if(prefixComponent != null)
  22. componentBuilder.append(prefixComponent);
  23. componentBuilder.append(component);
  24. if(suffixComponent != null)
  25. componentBuilder.append(suffixComponent);
  26. }
  27. return componentBuilder.build();
  28. }
  29. /**
  30. * Takes a list of components and splits them into arrays each with a maximum element limit
  31. * Individual elements in [][X] may be null
  32. *
  33. * @param components target component list
  34. * @param groupsSize maximum size per array
  35. * @return a 2D array with components split into groups
  36. */
  37. static @NotNull Component[][] splitComponentsIntoGroups(@NotNull List<Component> components, int groupsSize) {
  38. int groupCount = (int) Math.ceil((double) components.size() / (double) groupsSize);
  39. Component[][] splitGroups = new Component[groupCount][groupsSize];
  40. int groupsFinished = 0;
  41. while (groupsFinished < groupCount) {
  42. //Fill group with members
  43. for(int i = 0; i < groupsSize; i++) {
  44. int indexOfPotentialMember = i + (groupsFinished * 3); //Groups don't always fill all members neatly
  45. //Some groups won't have entirely non-null elements
  46. if(indexOfPotentialMember > components.size()-1) {
  47. break;
  48. }
  49. Component potentialMember = components.get(indexOfPotentialMember);
  50. //Make sure the potential member exists because of rounding
  51. if(potentialMember != null) {
  52. splitGroups[groupsFinished][i] = potentialMember;
  53. }
  54. }
  55. //Another group is finished
  56. groupsFinished++;
  57. }
  58. return splitGroups;
  59. }
  60. static void addChildWebComponent(@NotNull ComponentBuilder<?, ?> webTextComponent, @NotNull String childName) {
  61. TextComponent childComponent = Component.text(childName).color(NamedTextColor.BLUE);
  62. webTextComponent.append(childComponent);
  63. }
  64. static void addNewHoverComponentToTextComponent(@NotNull TextComponent.Builder textComponent, @NotNull Component baseComponent) {
  65. textComponent.hoverEvent(HoverEvent.showText(baseComponent));
  66. }
  67. }