Advertisement
Dori_mon

Untitled

Jun 6th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.69 KB | None | 0 0
  1. package com.dorilahav.elusive.configuration;
  2.  
  3. import com.fasterxml.jackson.annotation.JsonCreator;
  4. import com.fasterxml.jackson.annotation.JsonIgnore;
  5. import com.fasterxml.jackson.annotation.JsonInclude;
  6. import com.fasterxml.jackson.annotation.JsonProperty;
  7. import lombok.AccessLevel;
  8. import lombok.AllArgsConstructor;
  9. import lombok.Getter;
  10. import net.md_5.bungee.api.chat.BaseComponent;
  11. import net.md_5.bungee.api.chat.ComponentBuilder;
  12. import net.md_5.bungee.api.chat.HoverEvent;
  13. import net.md_5.bungee.api.chat.TextComponent;
  14. import org.bukkit.ChatColor;
  15. import org.bukkit.command.CommandSender;
  16. import org.bukkit.entity.Player;
  17.  
  18. import java.awt.*;
  19. import java.beans.ConstructorProperties;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.concurrent.ConcurrentHashMap;
  25. import java.util.function.Function;
  26. import java.util.regex.Pattern;
  27. import java.util.stream.Collectors;
  28.  
  29. public class Message {
  30.  
  31. private final String
  32. content;
  33.  
  34. @JsonProperty(required = false)
  35. @JsonInclude(value = JsonInclude.Include.NON_NULL)
  36. private String
  37. hoverText;
  38.  
  39. private final boolean
  40. error;
  41.  
  42. private final List<String>
  43. availableVariables;
  44.  
  45.  
  46. @JsonCreator
  47. @ConstructorProperties({ "content", "hoverText", "error", "availableVariables" })
  48. public Message(String content, String hoverText, boolean error, List<String> availableVariables) {
  49. this.content = content;
  50. this.hoverText = hoverText;
  51. this.error = error;
  52. this.availableVariables = availableVariables;
  53. }
  54.  
  55. @JsonIgnore
  56. public Message(String content, String hoverText, boolean error, String... availableVariables) {
  57. this(content, hoverText, error, Arrays.asList(availableVariables));
  58. }
  59.  
  60. @JsonIgnore
  61. public Message(String content, boolean error, String... availableVariables) {
  62. this(content, null, error, availableVariables);
  63. }
  64.  
  65.  
  66. // Parses the content that was specified in the object with the entered variables and processes color changes on the content.
  67. public String parseContent(Object... variables) {
  68. return parse(content, variables);
  69. }
  70.  
  71. // Parses the content that was specified in the object with the entered variables and processes color changes on the content.
  72. public String parse(String content, Object... variables) {
  73. return parseColors(parseVariables(content, variables), (error ? errorColor : mainColor), argumentsColor);
  74. }
  75.  
  76. // Parses the content that was specified in the object with the entered variables.
  77. public String parseVariables(String content, Object... variables) {
  78. String output = content;
  79.  
  80. for (int i = 0; i < availableVariables.size(); i++) {
  81.  
  82. if (i >= variables.length)
  83. break;
  84.  
  85. String name = availableVariables.get(i);
  86. Object value = variables[i];
  87.  
  88. output = parseVariable(output, name, value);
  89. }
  90.  
  91. return output;
  92. }
  93.  
  94. // Parses the content with one specific variable with the specified name and value.
  95. public String parseVariable(String content, String name, Object value) {
  96. String output = content;
  97.  
  98. if (output.toLowerCase().contains("%" + name.toLowerCase() + "%"))
  99. output = output.replaceAll("(?i)" + Pattern.quote("%" + name + "%"), value.toString());
  100.  
  101. Set<SubArgument> subArguments = SUB_ARGUMENTS.stream().filter(sa -> sa.getType().isInstance(value)).collect(Collectors.toSet());
  102. if (subArguments.isEmpty())
  103. return output;
  104.  
  105. for (SubArgument subArgument : subArguments)
  106. if (output.toLowerCase().contains("%" + name.toLowerCase() + ":" + subArgument.getName().toLowerCase() + "%"))
  107. output = output.replaceAll("(?i)" + Pattern.quote("%" + name + ":" + subArgument.getName() + "%"), subArgument.process.apply(value).toString());
  108.  
  109. return output;
  110. }
  111.  
  112. // Replaces the content with the specified colors whenever there is a {} with a text between it.
  113. public String parseColors(String content, String bodyColor, String argumentsColor) {
  114. String output = content;
  115.  
  116. output = bodyColor + output;
  117. output = output.replaceAll("\\{(.+)}", argumentsColor + "$1" + bodyColor);
  118.  
  119. return ChatColor.translateAlternateColorCodes('&', output);
  120. }
  121.  
  122. public BaseComponent[] parseWithHover(Object... variables) {
  123. BaseComponent[] components = TextComponent.fromLegacyText(parseContent(variables));
  124. BaseComponent[] hoverComponents = TextComponent.fromLegacyText(parse(hoverText, variables));
  125.  
  126. for (BaseComponent component : components)
  127. component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponents));
  128.  
  129. return components;
  130. }
  131.  
  132. // Sends the message to the specified sender and uses the specified variables as the variables to parse.
  133. public void send(CommandSender sender, Object... variables) {
  134. if (sender instanceof Player && hoverText != null) {
  135.  
  136. for (Object variable : variables)
  137. System.out.println(variable.getClass());
  138.  
  139. ((Player) sender).spigot().sendMessage(parseWithHover(variables));
  140. return;
  141. }
  142. sender.sendMessage(parseContent(variables));
  143. }
  144.  
  145.  
  146. // This variable contains all the sub arguments that has been registered!
  147. @JsonIgnore
  148. private static final Set<SubArgument>
  149. SUB_ARGUMENTS = ConcurrentHashMap.newKeySet();
  150.  
  151. public static void registerSubArgument(SubArgument<?> subArgument) {
  152. SUB_ARGUMENTS.add(subArgument);
  153. }
  154.  
  155. public static <T> void registerSubArgument(Class<T> type, String name, Function<T, Object> process) {
  156. registerSubArgument(new SubArgument<>(type, name, process));
  157. }
  158.  
  159. private static String
  160. mainColor = "",
  161. errorColor = "",
  162. argumentsColor = "";
  163.  
  164. public static void initializeColors(String mainColor, String errorColor, String argumentsColor) {
  165. Message.mainColor = mainColor;
  166. Message.errorColor = errorColor;
  167. Message.argumentsColor = argumentsColor;
  168. }
  169.  
  170. // Create sub variables to get values out of an argument.
  171. @AllArgsConstructor
  172. public static class SubArgument<T> {
  173.  
  174. @Getter(value = AccessLevel.PROTECTED)
  175. private final Class<T>
  176. type;
  177.  
  178. @Getter(value = AccessLevel.PROTECTED)
  179. private final String
  180. name;
  181.  
  182. @Getter(value = AccessLevel.PROTECTED)
  183. private final Function<T, Object>
  184. process;
  185.  
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement