mirror of
https://github.com/darksaid98/Carbon.git
synced 2026-12-03 22:15:45 +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;
|
package net.draycia.carbon.listeners.events;
|
||||||
|
|
||||||
import net.draycia.carbon.events.CarbonEvents;
|
import net.draycia.carbon.events.CarbonEvents;
|
||||||
import net.draycia.carbon.events.api.ChatComponentEvent;
|
import net.draycia.carbon.events.api.ChatFormatEvent;
|
||||||
import net.kyori.adventure.text.TextComponent;
|
|
||||||
import net.kyori.adventure.text.event.ClickEvent;
|
|
||||||
import net.kyori.event.PostOrders;
|
import net.kyori.event.PostOrders;
|
||||||
|
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
public class UrlLinkHandler {
|
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() {
|
public UrlLinkHandler() {
|
||||||
CarbonEvents.register(ChatComponentEvent.class, PostOrders.NORMAL, true, event -> {
|
// TODO: permission to make links clickable, maybe?
|
||||||
final TextComponent newComponent = (TextComponent) event.component()
|
CarbonEvents.register(ChatFormatEvent.class, PostOrders.NORMAL, true, event -> {
|
||||||
.replaceText(URL_PATTERN, url -> url.clickEvent(ClickEvent.openUrl(url.content())));
|
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
|
||||||
event.component(newComponent);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user