Advertisement
Dori_mon

Untitled

Jun 6th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 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 lombok.AccessLevel;
  6. import lombok.AllArgsConstructor;
  7. import lombok.Getter;
  8. import org.bukkit.command.CommandSender;
  9.  
  10. import java.beans.ConstructorProperties;
  11. import java.util.Arrays;
  12. import java.util.List;
  13. import java.util.Map;
  14. import java.util.Set;
  15. import java.util.concurrent.ConcurrentHashMap;
  16. import java.util.function.Function;
  17. import java.util.regex.Pattern;
  18.  
  19. public class Message {
  20.  
  21. private final String
  22. content;
  23.  
  24. private final boolean
  25. error;
  26.  
  27. private final List<String>
  28. availableVariables;
  29.  
  30. @JsonCreator
  31. @ConstructorProperties({ "content", "error", "availableVariables" })
  32. public Message(String content, boolean error, List<String> availableVariables) {
  33. this.content = content;
  34. this.error = error;
  35. this.availableVariables = availableVariables;
  36. }
  37.  
  38. @JsonIgnore
  39. public Message(String content, boolean error, String... availableVariables) {
  40. this.content = content;
  41. this.error = error;
  42. this.availableVariables = Arrays.asList(availableVariables);
  43. }
  44.  
  45.  
  46. // Parses the content that was specified in the object with the entered variables and processes color changes on the content.
  47. public String parse(Object... variables) {
  48. return parseColors(parseVariables(content, variables), (error ? Config.MESSAGE_ERROR_COLOR : Config.MESSAGE_MAIN_COLOR), Config.MESSAGE_ARGUMENTS_COLOR);
  49. }
  50.  
  51. // Parses the content that was specified in the object with the entered variables.
  52. public String parseVariables(String content, Object... variables) {
  53. String output = content;
  54.  
  55. for (int i = 0; i < availableVariables.size(); i++) {
  56.  
  57. if (i >= variables.length)
  58. break;
  59.  
  60. String name = availableVariables.get(i);
  61. Object value = variables[i];
  62.  
  63. output = parseVariable(output, name, value);
  64. }
  65.  
  66. return output;
  67. }
  68.  
  69. // Parses the content with one specific variable with the specified name and value.
  70. public String parseVariable(String content, String name, Object value) {
  71. String output = content;
  72.  
  73. if (output.toLowerCase().contains("%" + name.toLowerCase() + "%"))
  74. output = output.replaceAll("(?i)" + Pattern.quote("%" + name + "%"), value.toString());
  75.  
  76. if (!subArguments.containsKey(value.getClass()))
  77. return output;
  78.  
  79. for (SubArgument subArgument : subArguments.get(value.getClass()))
  80. if (output.toLowerCase().contains("%" + name.toLowerCase() + ":" + subArgument.getName().toLowerCase() + "%"))
  81. output = output.replaceAll("(?i)" + Pattern.quote("%" + name + ":" + subArgument.getName() + "%"), subArgument.process.apply(value).toString());
  82.  
  83. return output;
  84. }
  85.  
  86. // Replaces the content with the specified colors whenever there is a {} with a text between it.
  87. public String parseColors(String content, String bodyColor, String argumentsColor) {
  88. String output = content;
  89.  
  90. output = bodyColor + output;
  91. output = output.replaceAll("\\{(.+)}", argumentsColor + "$1" + bodyColor);
  92.  
  93. return output;
  94. }
  95.  
  96. // Sends the message to the specified sender and uses the specified variables as the variables to parse.
  97. public void send(CommandSender sender, Object... variables) {
  98. sender.sendMessage(parse(variables));
  99. }
  100.  
  101. // This variable contains all the sub arguments that has been registered!
  102. @JsonIgnore
  103. private static final Map<Class<?>, Set<SubArgument<?>>>
  104. subArguments = new ConcurrentHashMap<>();
  105.  
  106. public static void registerSubArgument(SubArgument<?> subArgument) {
  107. if (!subArguments.containsKey(subArgument.getType()))
  108. subArguments.put(subArgument.getType(), ConcurrentHashMap.newKeySet());
  109.  
  110. subArguments.get(subArgument.getType()).add(subArgument);
  111. }
  112.  
  113. public static <T> void registerSubArgument(Class<T> type, String name, Function<T, Object> process) {
  114. registerSubArgument(new SubArgument<>(type, name, process));
  115. }
  116.  
  117. // Create sub variables to get values out of an argument.
  118. @AllArgsConstructor
  119. public static class SubArgument<T> {
  120.  
  121. @Getter(value = AccessLevel.PROTECTED)
  122. private final Class<T>
  123. type;
  124.  
  125. @Getter(value = AccessLevel.PROTECTED)
  126. private final String
  127. name;
  128.  
  129. @Getter(value = AccessLevel.PROTECTED)
  130. private final Function<T, Object>
  131. process;
  132.  
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement