TextUtils.java 5.0 KB

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