Updated to 1.21 by switching to cosmos
This commit is contained in:
@@ -3,11 +3,11 @@ package com.alttd.chat.util;
|
||||
import com.alttd.chat.ChatAPI;
|
||||
import com.alttd.chat.config.Config;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import net.kyori.adventure.text.ComponentLike;
|
||||
import net.kyori.adventure.text.format.TextDecoration;
|
||||
import net.kyori.adventure.text.minimessage.MiniMessage;
|
||||
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
|
||||
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags;
|
||||
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||
import net.luckperms.api.LuckPerms;
|
||||
import net.luckperms.api.model.group.Group;
|
||||
import net.luckperms.api.model.user.User;
|
||||
@@ -16,19 +16,17 @@ import net.luckperms.api.node.Node;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Utility {
|
||||
|
||||
private static final List<String> EMPTY_LIST = new ArrayList<>();
|
||||
static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(https?://)?[-a-zA-Z0-9@:%._+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)");
|
||||
static final Pattern URL_SCHEME_PATTERN = Pattern.compile("^[a-z][a-z0-9+\\-.]*:");
|
||||
|
||||
private static MiniMessage miniMessage = null;
|
||||
|
||||
public static String stringRegen = "\\{#[A-Fa-f0-9]{6}(<)?(>)?}";
|
||||
public static HashMap<String, String> colors;
|
||||
private static LegacyComponentSerializer legacySerializer;
|
||||
|
||||
static { // this might be in minimessage already?
|
||||
colors = new HashMap<>();
|
||||
colors.put("&0", "<black>");
|
||||
@@ -48,41 +46,44 @@ public class Utility {
|
||||
colors.put("&e", "<yellow>");
|
||||
colors.put("&f", "<white>");
|
||||
}
|
||||
|
||||
|
||||
public static HashMap<String, Pair<TagResolver, List<String>>> formattingPerms = new HashMap<>();
|
||||
|
||||
static {
|
||||
formattingPerms.put("chat.format.color",
|
||||
new Pair<>(StandardTags.color(), colors.values().stream().toList()));
|
||||
new Pair<>(StandardTags.color(), colors.values().stream().toList()));
|
||||
formattingPerms.put("chat.format.bold",
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.BOLD), List.of("<bold>", "<b>")));
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.BOLD), List.of("<bold>", "<b>")));
|
||||
formattingPerms.put("chat.format.italic",
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.ITALIC), List.of("<italic>", "<i>")));
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.ITALIC), List.of("<italic>", "<i>")));
|
||||
formattingPerms.put("chat.format.underlined",
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.UNDERLINED), List.of("<underlined>", "<u>")));
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.UNDERLINED), List.of("<underlined>", "<u>")));
|
||||
formattingPerms.put("chat.format.strikethrough",
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.STRIKETHROUGH), List.of("<strikethrough>", "<st>")));
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.STRIKETHROUGH), List.of("<strikethrough>", "<st>")));
|
||||
formattingPerms.put("chat.format.obfuscated",
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.OBFUSCATED), List.of("<obfuscated>", "<obf>")));
|
||||
new Pair<>(StandardTags.decorations(TextDecoration.OBFUSCATED), List.of("<obfuscated>", "<obf>")));
|
||||
formattingPerms.put("chat.format.gradient",
|
||||
new Pair<>(StandardTags.gradient(), EMPTY_LIST));
|
||||
new Pair<>(StandardTags.gradient(), EMPTY_LIST));
|
||||
formattingPerms.put("chat.format.font",
|
||||
new Pair<>(StandardTags.font(), EMPTY_LIST));
|
||||
new Pair<>(StandardTags.font(), EMPTY_LIST));
|
||||
formattingPerms.put("chat.format.rainbow",
|
||||
new Pair<>(StandardTags.rainbow(), List.of("<rainbow>")));
|
||||
new Pair<>(StandardTags.rainbow(), List.of("<rainbow>")));
|
||||
formattingPerms.put("chat.format.hover",
|
||||
new Pair<>(StandardTags.hoverEvent(), EMPTY_LIST));
|
||||
new Pair<>(StandardTags.hoverEvent(), EMPTY_LIST));
|
||||
formattingPerms.put("chat.format.click",
|
||||
new Pair<>(StandardTags.clickEvent(), EMPTY_LIST));
|
||||
new Pair<>(StandardTags.clickEvent(), EMPTY_LIST));
|
||||
formattingPerms.put("chat.format.transition",
|
||||
new Pair<>(StandardTags.transition(), EMPTY_LIST));
|
||||
new Pair<>(StandardTags.transition(), EMPTY_LIST));
|
||||
formattingPerms.put("chat.format.reset",
|
||||
new Pair<>(StandardTags.reset(), List.of("<reset>", "<r>")));
|
||||
new Pair<>(StandardTags.reset(), List.of("<reset>", "<r>")));
|
||||
formattingPerms.put("chat.format.newline",
|
||||
new Pair<>(StandardTags.newline(), List.of("<newline>")));
|
||||
new Pair<>(StandardTags.newline(), List.of("<newline>")));
|
||||
}
|
||||
|
||||
public static String parseColors(String message) {
|
||||
if (message == null) return "";
|
||||
if (message == null) {
|
||||
return "";
|
||||
}
|
||||
// split string in sections and check those vs looping hashmap?:/
|
||||
// think this is better, but will check numbers on this
|
||||
for (String key : colors.keySet()) {
|
||||
@@ -93,15 +94,17 @@ public class Utility {
|
||||
return message;
|
||||
}
|
||||
|
||||
public static Component getPrefix(UUID uuid, boolean single) {
|
||||
public static ComponentLike getPrefix(UUID uuid, boolean single) {
|
||||
StringBuilder prefix = new StringBuilder();
|
||||
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
List<String> prefixGroups = Config.PREFIXGROUPS;
|
||||
if(user == null) return Component.empty();
|
||||
if(!single) {
|
||||
if (user == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
if (!single) {
|
||||
Collection<Group> inheritedGroups = user.getInheritedGroups(user.getQueryOptions());
|
||||
if(inheritedGroups.stream().map(Group::getName).collect(Collectors.toList()).contains("eventleader")) {
|
||||
if (inheritedGroups.stream().anyMatch(group -> group.getName().equals("eventleader"))) {
|
||||
prefixGroups.remove("eventteam"); // hardcoded for now, new prefix system would load this from config
|
||||
}
|
||||
inheritedGroups.stream()
|
||||
@@ -113,21 +116,23 @@ public class Utility {
|
||||
});
|
||||
}
|
||||
prefix.append(getUserPrefix(user));
|
||||
// prefix.append(user.getCachedData().getMetaData().getPrefix());
|
||||
|
||||
return applyColor(prefix.toString());
|
||||
}
|
||||
|
||||
public static Component getStaffPrefix(UUID uuid) {
|
||||
public static ComponentLike getStaffPrefix(UUID uuid) {
|
||||
StringBuilder prefix = new StringBuilder();
|
||||
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
if(user == null) return Component.empty();
|
||||
if(user.getCachedData().getPermissionData().checkPermission("group." + Config.MINIMIUMSTAFFRANK).asBoolean()) {
|
||||
if (user == null) {
|
||||
return Component.empty();
|
||||
}
|
||||
if (user.getCachedData().getPermissionData().checkPermission("group." + Config.MINIMIUMSTAFFRANK).asBoolean()) {
|
||||
Group group = luckPerms.getGroupManager().getGroup(user.getPrimaryGroup());
|
||||
if(group != null)
|
||||
if (group != null) {
|
||||
prefix.append(getGroupPrefix(group));
|
||||
// prefix.append(group.getCachedData().getMetaData().getPrefix());
|
||||
}
|
||||
// prefix.append(group.getCachedData().getMetaData().getPrefix());
|
||||
}
|
||||
return applyColor(prefix.toString());
|
||||
}
|
||||
@@ -138,26 +143,32 @@ public class Utility {
|
||||
if (group == null) {
|
||||
return "";
|
||||
}
|
||||
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", user.getCachedData().getMetaData().getPrefix());
|
||||
}
|
||||
|
||||
public static String getGroupPrefix(String groupName) {
|
||||
Group group = ChatAPI.get().getLuckPerms().getGroupManager().getGroup(groupName);
|
||||
if (group == null) {
|
||||
String prefix = user.getCachedData().getMetaData().getPrefix();
|
||||
if (prefix == null) {
|
||||
ALogger.warn("User " + user.getUsername() + " has no prefix set!");
|
||||
return "";
|
||||
}
|
||||
return getGroupPrefix(group);
|
||||
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", prefix);
|
||||
}
|
||||
|
||||
public static String getGroupPrefix(Group group) {
|
||||
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", group.getCachedData().getMetaData().getPrefix());
|
||||
String prefix = group.getCachedData().getMetaData().getPrefix();
|
||||
if (prefix == null) {
|
||||
ALogger.warn("Group " + group.getName() + " has no prefix set!");
|
||||
return "";
|
||||
}
|
||||
return ChatAPI.get().getPrefixes().get(group.getName()).replace("<prefix>", prefix);
|
||||
}
|
||||
|
||||
public static String getDisplayName(UUID uuid, String playerName) {
|
||||
if (!playerName.isBlank()) return playerName;
|
||||
if (!playerName.isBlank()) {
|
||||
return playerName;
|
||||
}
|
||||
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
if(user == null) return "";
|
||||
if (user == null) {
|
||||
return "";
|
||||
}
|
||||
return user.getUsername();
|
||||
}
|
||||
|
||||
@@ -165,20 +176,22 @@ public class Utility {
|
||||
ChatAPI.get().getLuckPerms().getUserManager().modifyUser(uuid, user -> {
|
||||
// Add the permission
|
||||
user.data().add(Node.builder(permission)
|
||||
.value(!user.getCachedData().getPermissionData().checkPermission(permission).asBoolean()).build());
|
||||
.value(!user.getCachedData().getPermissionData().checkPermission(permission).asBoolean()).build());
|
||||
});
|
||||
}
|
||||
|
||||
public static boolean hasPermission(UUID uuid, String permission) {
|
||||
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
|
||||
User user = luckPerms.getUserManager().getUser(uuid);
|
||||
if(user == null) return false;
|
||||
if (user == null) {
|
||||
return false;
|
||||
}
|
||||
return user.getCachedData().getPermissionData().checkPermission(permission).asBoolean();
|
||||
}
|
||||
|
||||
public static Component applyColor(String message) {
|
||||
public static ComponentLike applyColor(String message) {
|
||||
String hexColor1 = "";
|
||||
String hexColor2 = "";
|
||||
String hexColor2;
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
message = parseColors(message);
|
||||
boolean startsWithColor = false;
|
||||
@@ -196,7 +209,7 @@ public class Utility {
|
||||
for (String s : split) {
|
||||
nextIndex += s.length();
|
||||
int tmp = message.indexOf("}", nextIndex);
|
||||
if (tmp < message.length() && tmp>=0) {
|
||||
if (tmp < message.length() && tmp >= 0) {
|
||||
list.add(message.substring(nextIndex, tmp + 1));
|
||||
nextIndex = tmp + 1;
|
||||
}
|
||||
@@ -220,7 +233,7 @@ public class Utility {
|
||||
} else if (bigger || lesser) {
|
||||
hexColor2 = s.substring(1, s.length() - 2);
|
||||
} else {
|
||||
hexColor2 = s.substring(1, s.length() -1);
|
||||
hexColor2 = s.substring(1, s.length() - 1);
|
||||
}
|
||||
|
||||
if (firstLoop) {
|
||||
@@ -241,12 +254,12 @@ public class Utility {
|
||||
lastColorMatters = bigger;
|
||||
i++;
|
||||
}
|
||||
if (split.length > i){
|
||||
if (split.length > i) {
|
||||
stringBuilder.append("<").append(hexColor1).append(">").append(split[i]);
|
||||
}
|
||||
}
|
||||
return stringBuilder.length() == 0 ? Utility.parseMiniMessage(message)
|
||||
: Utility.parseMiniMessage(stringBuilder.toString());
|
||||
return stringBuilder.isEmpty() ? Utility.parseMiniMessage(message)
|
||||
: Utility.parseMiniMessage(stringBuilder.toString());
|
||||
}
|
||||
|
||||
public static boolean checkNickBrightEnough(String nickname) {
|
||||
@@ -294,10 +307,9 @@ public class Utility {
|
||||
Matcher matcher = DEFAULT_URL_PATTERN.matcher(message);
|
||||
while (matcher.find()) {
|
||||
String url = matcher.group();
|
||||
String displayUrl = url;
|
||||
String urlFormat = Config.URLFORMAT;
|
||||
message = message.replace(url, urlFormat
|
||||
.replaceAll("<url>", "<u>" + displayUrl + "</u>")
|
||||
.replaceAll("<url>", "<u>" + url + "</u>")
|
||||
.replaceAll("<clickurl>", formatUrl(url)));
|
||||
}
|
||||
return message;
|
||||
@@ -310,11 +322,11 @@ public class Utility {
|
||||
return "https://" + url;
|
||||
}
|
||||
|
||||
public static Component parseMiniMessage(String message) {
|
||||
public static ComponentLike parseMiniMessage(String message) {
|
||||
return getMiniMessage().deserialize(message);
|
||||
}
|
||||
|
||||
public static Component parseMiniMessage(String message, TagResolver placeholders) {
|
||||
public static ComponentLike parseMiniMessage(String message, TagResolver placeholders) {
|
||||
if (placeholders == null) {
|
||||
return getMiniMessage().deserialize(message);
|
||||
} else {
|
||||
@@ -322,7 +334,7 @@ public class Utility {
|
||||
}
|
||||
}
|
||||
|
||||
public static Component parseMiniMessage(String message, TagResolver ... placeholders) {
|
||||
public static ComponentLike parseMiniMessage(String message, TagResolver... placeholders) {
|
||||
if (placeholders == null) {
|
||||
return getMiniMessage().deserialize(message);
|
||||
} else {
|
||||
@@ -335,7 +347,9 @@ public class Utility {
|
||||
}
|
||||
|
||||
public static MiniMessage getMiniMessage() {
|
||||
if (miniMessage == null) miniMessage = MiniMessage.miniMessage();
|
||||
if (miniMessage == null) {
|
||||
miniMessage = MiniMessage.miniMessage();
|
||||
}
|
||||
return miniMessage;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user