Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.dorilahav.elusive.configuration;
- import com.fasterxml.jackson.annotation.JsonCreator;
- import com.fasterxml.jackson.annotation.JsonIgnore;
- import com.fasterxml.jackson.annotation.JsonInclude;
- import com.fasterxml.jackson.annotation.JsonProperty;
- import lombok.AccessLevel;
- import lombok.AllArgsConstructor;
- import lombok.Getter;
- import net.md_5.bungee.api.chat.BaseComponent;
- import net.md_5.bungee.api.chat.ComponentBuilder;
- import net.md_5.bungee.api.chat.HoverEvent;
- import net.md_5.bungee.api.chat.TextComponent;
- import org.bukkit.ChatColor;
- import org.bukkit.command.CommandSender;
- import org.bukkit.entity.Player;
- import java.awt.*;
- import java.beans.ConstructorProperties;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.function.Function;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Message {
- private final String
- content;
- @JsonProperty(required = false)
- @JsonInclude(value = JsonInclude.Include.NON_NULL)
- private String
- hoverText;
- private final boolean
- error;
- private final List<String>
- availableVariables;
- @JsonCreator
- @ConstructorProperties({ "content", "hoverText", "error", "availableVariables" })
- public Message(String content, String hoverText, boolean error, List<String> availableVariables) {
- this.content = content;
- this.hoverText = hoverText;
- this.error = error;
- this.availableVariables = availableVariables;
- }
- @JsonIgnore
- public Message(String content, String hoverText, boolean error, String... availableVariables) {
- this(content, hoverText, error, Arrays.asList(availableVariables));
- }
- @JsonIgnore
- public Message(String content, boolean error, String... availableVariables) {
- this(content, null, error, availableVariables);
- }
- // Parses the content that was specified in the object with the entered variables and processes color changes on the content.
- public String parseContent(Object... variables) {
- return parse(content, variables);
- }
- // Parses the content that was specified in the object with the entered variables and processes color changes on the content.
- public String parse(String content, Object... variables) {
- return parseColors(parseVariables(content, variables), (error ? errorColor : mainColor), argumentsColor);
- }
- // Parses the content that was specified in the object with the entered variables.
- public String parseVariables(String content, Object... variables) {
- String output = content;
- for (int i = 0; i < availableVariables.size(); i++) {
- if (i >= variables.length)
- break;
- String name = availableVariables.get(i);
- Object value = variables[i];
- output = parseVariable(output, name, value);
- }
- return output;
- }
- // Parses the content with one specific variable with the specified name and value.
- public String parseVariable(String content, String name, Object value) {
- String output = content;
- if (output.toLowerCase().contains("%" + name.toLowerCase() + "%"))
- output = output.replaceAll("(?i)" + Pattern.quote("%" + name + "%"), value.toString());
- Set<SubArgument> subArguments = SUB_ARGUMENTS.stream().filter(sa -> sa.getType().isInstance(value)).collect(Collectors.toSet());
- if (subArguments.isEmpty())
- return output;
- for (SubArgument subArgument : subArguments)
- if (output.toLowerCase().contains("%" + name.toLowerCase() + ":" + subArgument.getName().toLowerCase() + "%"))
- output = output.replaceAll("(?i)" + Pattern.quote("%" + name + ":" + subArgument.getName() + "%"), subArgument.process.apply(value).toString());
- return output;
- }
- // Replaces the content with the specified colors whenever there is a {} with a text between it.
- public String parseColors(String content, String bodyColor, String argumentsColor) {
- String output = content;
- output = bodyColor + output;
- output = output.replaceAll("\\{(.+)}", argumentsColor + "$1" + bodyColor);
- return ChatColor.translateAlternateColorCodes('&', output);
- }
- public BaseComponent[] parseWithHover(Object... variables) {
- BaseComponent[] components = TextComponent.fromLegacyText(parseContent(variables));
- BaseComponent[] hoverComponents = TextComponent.fromLegacyText(parse(hoverText, variables));
- for (BaseComponent component : components)
- component.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, hoverComponents));
- return components;
- }
- // Sends the message to the specified sender and uses the specified variables as the variables to parse.
- public void send(CommandSender sender, Object... variables) {
- if (sender instanceof Player && hoverText != null) {
- for (Object variable : variables)
- System.out.println(variable.getClass());
- ((Player) sender).spigot().sendMessage(parseWithHover(variables));
- return;
- }
- sender.sendMessage(parseContent(variables));
- }
- // This variable contains all the sub arguments that has been registered!
- @JsonIgnore
- private static final Set<SubArgument>
- SUB_ARGUMENTS = ConcurrentHashMap.newKeySet();
- public static void registerSubArgument(SubArgument<?> subArgument) {
- SUB_ARGUMENTS.add(subArgument);
- }
- public static <T> void registerSubArgument(Class<T> type, String name, Function<T, Object> process) {
- registerSubArgument(new SubArgument<>(type, name, process));
- }
- private static String
- mainColor = "",
- errorColor = "",
- argumentsColor = "";
- public static void initializeColors(String mainColor, String errorColor, String argumentsColor) {
- Message.mainColor = mainColor;
- Message.errorColor = errorColor;
- Message.argumentsColor = argumentsColor;
- }
- // Create sub variables to get values out of an argument.
- @AllArgsConstructor
- public static class SubArgument<T> {
- @Getter(value = AccessLevel.PROTECTED)
- private final Class<T>
- type;
- @Getter(value = AccessLevel.PROTECTED)
- private final String
- name;
- @Getter(value = AccessLevel.PROTECTED)
- private final Function<T, Object>
- process;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement