Allow urls to be clickable in chat

This commit is contained in:
destro174 2021-08-17 22:54:24 +02:00
parent 47fb205001
commit aebb1b83a5
4 changed files with 57 additions and 2 deletions

View File

@ -211,8 +211,10 @@ public final class Config {
// TODO prefixes need hovers, this hasn't been setup yet!
public static String CHATFORMAT = "<white><light_purple><prefixall> <gray><hover:show_text:Click to message <sendername>><click:suggest_command:/msg <sendername> ><sender></hover>: <white><message>";
public static String URLFORMAT = "<click:OPEN_URL:<clickurl>><url></click>";
private static void Chat() {
CHATFORMAT = getString("chat.format", CHATFORMAT);
URLFORMAT = getString("chat.urlformat", URLFORMAT);
}
public static List<String> GACECOMMANDALIASES = new ArrayList<>();

View File

@ -3,19 +3,30 @@ 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.TextComponent;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
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;
import net.luckperms.api.node.Node;
import net.md_5.bungee.api.ChatColor;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Utility {
static final Pattern DEFAULT_URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
static final Pattern URL_SCHEME_PATTERN = Pattern.compile("^[a-z][a-z0-9+\\-.]*:");
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>");
@ -58,7 +69,7 @@ public class Utility {
.sorted(Comparator.comparingInt(o -> o.getWeight().orElse(0)))
.forEach(group -> {
if (Config.PREFIXGROUPS.contains(group.getName())) {
prefix.append(group.getCachedData().getMetaData().getPrefix());
prefix.append(getGroupPrefix(group));
}
});
}
@ -80,6 +91,11 @@ public class Utility {
return applyColor(prefix.toString());
}
public static String getGroupPrefix(Group group) {
return group.getCachedData().getMetaData().getPrefix();
}
public static String getDisplayName(UUID uuid, String playerName) {
if (!playerName.isBlank()) return playerName;
LuckPerms luckPerms = ChatAPI.get().getLuckPerms();
@ -176,4 +192,30 @@ public class Utility {
return stringBuilder.length()==0 ? miniMessage.parse(message)
: miniMessage.parse(stringBuilder.toString());
}
public static String formatText(String message) {
/*
.match(pattern)
.replacement(url -> {
String clickUrl = url.content();
if (!URL_SCHEME_PATTERN.matcher(clickUrl).find()) {
clickUrl = "http://" + clickUrl;
}
return (style == null ? url : url.style(style)).clickEvent(ClickEvent.openUrl(clickUrl));
})
.build();
*/
Matcher matcher = DEFAULT_URL_PATTERN.matcher(message);
while (matcher.find()) {
String url = matcher.group();
String clickUrl = url;
String urlFormat = Config.URLFORMAT;
if (!URL_SCHEME_PATTERN.matcher(clickUrl).find()) {
clickUrl = "http://" + clickUrl;
}
message = message.replace(url, urlFormat.replaceAll("<url>", url).replaceAll("<clickurl>", clickUrl));
}
return message;
}
}

View File

@ -17,6 +17,7 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.kyori.adventure.text.minimessage.Template;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
@ -58,6 +59,8 @@ public class ChatHandler {
if(updatedMessage.contains("[i]"))
updatedMessage = updatedMessage.replace("[i]", "<[i]>");
updatedMessage = Utility.formatText(updatedMessage);
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", updatedMessage),
Template.of("sendername", player.getName()),
@ -110,6 +113,8 @@ public class ChatHandler {
if(updatedMessage.contains("[i]"))
updatedMessage = updatedMessage.replace("[i]", "<[i]>"); // end of todo
updatedMessage = Utility.formatText(updatedMessage);
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
Template.of("prefix", prefix),
@ -145,6 +150,8 @@ public class ChatHandler {
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
updatedMessage = Utility.formatText(updatedMessage);
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
Template.of("message", updatedMessage),
@ -179,6 +186,8 @@ public class ChatHandler {
if(updatedMessage.contains("[i]")) updatedMessage = updatedMessage.replace("[i]", "<[i]>");
updatedMessage = Utility.formatText(updatedMessage);
List<Template> templates = new ArrayList<>(List.of(
Template.of("sender", senderName),
Template.of("sendername", senderName),

View File

@ -76,6 +76,8 @@ public class ChatListener implements Listener, ChatRenderer {
if(message.contains("[i]"))
message = message.replace("[i]", "<[i]>"); // end of todo
message = Utility.formatText(message);
List<Template> templates = new ArrayList<>(List.of(
Template.of("message", message),
Template.of("[i]", ChatHandler.itemComponent(player.getInventory().getItemInMainHand()))