TextUtils.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.gmail.nossr50.util.text;
  2. import com.gmail.nossr50.mcMMO;
  3. import net.kyori.adventure.text.Component;
  4. import net.kyori.adventure.text.ComponentBuilder;
  5. import net.kyori.adventure.text.TextComponent;
  6. import net.kyori.adventure.text.event.HoverEvent;
  7. import net.kyori.adventure.text.format.NamedTextColor;
  8. import net.kyori.adventure.text.format.Style;
  9. import net.kyori.adventure.text.format.TextDecoration;
  10. import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
  11. import net.md_5.bungee.api.chat.BaseComponent;
  12. import org.jetbrains.annotations.NotNull;
  13. import org.jetbrains.annotations.Nullable;
  14. import java.util.List;
  15. public class TextUtils {
  16. private static @Nullable LegacyComponentSerializer customLegacySerializer;
  17. private TextUtils() {
  18. // We don't want any instances of this class.
  19. }
  20. /**
  21. * Makes a single component from an array of components, can optionally add prefixes and suffixes to come before and after each component
  22. * @param componentsArray target array
  23. * @return a component with optional styling built from an array
  24. */
  25. static @NotNull Component fromArray(@NotNull Component[] componentsArray, @Nullable Component prefixComponent, @Nullable Component suffixComponent) {
  26. TextComponent.Builder componentBuilder = Component.text();
  27. for(Component component : componentsArray) {
  28. if(component == null) //Individual elements can be null
  29. continue;
  30. if(prefixComponent != null)
  31. componentBuilder.append(prefixComponent);
  32. componentBuilder.append(component);
  33. if(suffixComponent != null)
  34. componentBuilder.append(suffixComponent);
  35. }
  36. return componentBuilder.build();
  37. }
  38. /**
  39. * Takes a list of components and splits them into arrays each with a maximum element limit
  40. * Individual elements in [][X] may be null
  41. *
  42. * @param components target component list
  43. * @param groupsSize maximum size per array
  44. * @return a 2D array with components split into groups
  45. */
  46. static @NotNull Component[][] splitComponentsIntoGroups(@NotNull List<Component> components, int groupsSize) {
  47. int groupCount = (int) Math.ceil((double) components.size() / (double) groupsSize);
  48. Component[][] splitGroups = new Component[groupCount][groupsSize];
  49. int groupsFinished = 0;
  50. while (groupsFinished < groupCount) {
  51. //Fill group with members
  52. for(int i = 0; i < groupsSize; i++) {
  53. int indexOfPotentialMember = i + (groupsFinished * 3); //Groups don't always fill all members neatly
  54. //Some groups won't have entirely non-null elements
  55. if(indexOfPotentialMember > components.size()-1) {
  56. break;
  57. }
  58. Component potentialMember = components.get(indexOfPotentialMember);
  59. //Make sure the potential member exists because of rounding
  60. if(potentialMember != null) {
  61. splitGroups[groupsFinished][i] = potentialMember;
  62. }
  63. }
  64. //Another group is finished
  65. groupsFinished++;
  66. }
  67. return splitGroups;
  68. }
  69. static void addChildWebComponent(@NotNull ComponentBuilder<?, ?> webTextComponent, @NotNull String childName) {
  70. TextComponent childComponent = Component.text(childName).color(NamedTextColor.BLUE);
  71. webTextComponent.append(childComponent);
  72. }
  73. static void addNewHoverComponentToTextComponent(@NotNull TextComponent.Builder textComponent, @NotNull Component baseComponent) {
  74. textComponent.hoverEvent(HoverEvent.showText(baseComponent));
  75. }
  76. public static BaseComponent[] convertToBungeeComponent(@NotNull String displayName) {
  77. return net.md_5.bungee.api.chat.TextComponent.fromLegacyText(displayName);
  78. }
  79. public static @NotNull TextComponent ofBungeeComponents(@NotNull BaseComponent[] bungeeName) {
  80. return TextComponent.ofChildren(mcMMO.getCompatibilityManager().getBungeeSerializerCompatibilityLayer().deserialize(bungeeName));
  81. }
  82. public static @NotNull TextComponent ofBungeeRawStrings(@NotNull String bungeeRawString) {
  83. return ofBungeeComponents(convertToBungeeComponent(bungeeRawString));
  84. }
  85. public static @NotNull TextComponent ofLegacyTextRaw(@NotNull String rawString) {
  86. return LegacyComponentSerializer.legacySection().deserialize(rawString);
  87. }
  88. public static @NotNull TextComponent colorizeText(@NotNull String rawtext) {
  89. if(customLegacySerializer == null) {
  90. customLegacySerializer = getSerializer();
  91. }
  92. return customLegacySerializer.deserialize(rawtext);
  93. }
  94. @NotNull
  95. private static LegacyComponentSerializer getSerializer() {
  96. return LegacyComponentSerializer.builder()
  97. .hexColors()
  98. .useUnusualXRepeatedCharacterHexFormat()
  99. .character('&')
  100. .hexCharacter('#')
  101. .extractUrls(Style.style()
  102. .decorate(getURLStyle())
  103. .color(NamedTextColor.DARK_AQUA)
  104. .build())
  105. .build();
  106. }
  107. public static @NotNull TextDecoration[] getURLStyle() {
  108. return new TextDecoration[]{TextDecoration.UNDERLINED};
  109. }
  110. public static @NotNull String sanitizeForSerializer(@NotNull String string) {
  111. if(customLegacySerializer == null) {
  112. customLegacySerializer = getSerializer();
  113. }
  114. TextComponent componentForm = ofLegacyTextRaw(string);
  115. return customLegacySerializer.serialize(componentForm);
  116. }
  117. }