mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-01 22:15:08 +00:00
Change URL handling
This commit is contained in:
1 parent
cd36dcb701
commit
d9ef213c2d
1 file changed
+6
-9
@@ -1,23 +1,20 @@
|
||||
package net.draycia.carbon.listeners.events;
|
||||
|
||||
import net.draycia.carbon.events.CarbonEvents;
|
||||
import net.draycia.carbon.events.api.ChatComponentEvent;
|
||||
import net.kyori.adventure.text.TextComponent;
|
||||
import net.kyori.adventure.text.event.ClickEvent;
|
||||
import net.draycia.carbon.events.api.ChatFormatEvent;
|
||||
import net.kyori.event.PostOrders;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class UrlLinkHandler {
|
||||
|
||||
private static final Pattern URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
|
||||
private static final Pattern URL_PATTERN = Pattern.compile("((?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?)");
|
||||
|
||||
public UrlLinkHandler() {
|
||||
CarbonEvents.register(ChatComponentEvent.class, PostOrders.NORMAL, true, event -> {
|
||||
final TextComponent newComponent = (TextComponent) event.component()
|
||||
.replaceText(URL_PATTERN, url -> url.clickEvent(ClickEvent.openUrl(url.content())));
|
||||
|
||||
event.component(newComponent);
|
||||
// TODO: permission to make links clickable, maybe?
|
||||
CarbonEvents.register(ChatFormatEvent.class, PostOrders.NORMAL, true, event -> {
|
||||
event.message(URL_PATTERN.matcher(event.message()).replaceAll("<click:open_url:'$1'>$1</click>"));
|
||||
// Reminder: Do not use this on event.format(), this breaks hover events when they contain URLs
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user