Update URL detection and formatting logic in Utility
Enhanced `DEFAULT_URL_PATTERN` for improved URL detection and added `formatUrl` method to ensure URLs include a valid scheme. Adjusted `formatText` to use clickable and underlined URLs in the configured format. Removed unused imports and cleaned up comments.
This commit is contained in:
parent
e74361a7b7
commit
b65fcdb1c7
|
|
@ -12,7 +12,6 @@ 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 org.bukkit.permissions.Permission;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
|
|
@ -22,7 +21,7 @@ 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?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
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;
|
||||
|
|
@ -292,30 +291,25 @@ public class Utility {
|
|||
}
|
||||
|
||||
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 displayUrl = 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));
|
||||
message = message.replace(url, urlFormat
|
||||
.replaceAll("<url>", "<u>" + displayUrl + "</u>")
|
||||
.replaceAll("<clickurl>", formatUrl(url)));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String formatUrl(String url) {
|
||||
if (url.startsWith("http://") || url.startsWith("https://")) {
|
||||
return url;
|
||||
}
|
||||
return "https://" + url;
|
||||
}
|
||||
|
||||
public static Component parseMiniMessage(String message) {
|
||||
return getMiniMessage().deserialize(message);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user