Add support for URL click events. Fixed #77

This commit is contained in:
Draycia committed 2020-09-12 03:38:16 -07:00
1 parent 39b7c68818
commit 9d21073e49
2 files changed
+26

No files matched your search

@@ -20,6 +20,7 @@ import net.draycia.carbon.listeners.PlaceholderHandler;
import net.draycia.carbon.listeners.PlayerJoinListener;
import net.draycia.carbon.listeners.RelationalPlaceholderHandler;
import net.draycia.carbon.listeners.ShadowMuteHandler;
import net.draycia.carbon.listeners.UrlLinkHandler;
import net.draycia.carbon.listeners.UserFormattingHandler;
import net.draycia.carbon.listeners.WhisperPingHandler;
import net.draycia.carbon.managers.AdventureManager;
@@ -179,6 +180,7 @@ public final class CarbonChat extends JavaPlugin {
pluginManager.registerEvents(new PlayerJoinListener(this), this);
pluginManager.registerEvents(new RelationalPlaceholderHandler(), this);
pluginManager.registerEvents(new ShadowMuteHandler(this), this);
pluginManager.registerEvents(new UrlLinkHandler(), this);
pluginManager.registerEvents(new UserFormattingHandler(), this);
pluginManager.registerEvents(new WhisperPingHandler(this), this);
@@ -0,0 +1,24 @@
package net.draycia.carbon.listeners;
import net.draycia.carbon.events.ChatComponentEvent;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import java.util.regex.Pattern;
public class UrlLinkHandler implements Listener {
private static final Pattern URL_PATTERN = Pattern.compile("(?:(https?)://)?([-\\w_.]+\\.\\w{2,})(/\\S*)?");
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onUrlLink(final ChatComponentEvent event) {
final TextComponent newComponent = (TextComponent) event.component()
.replaceText(URL_PATTERN, url -> url.clickEvent(ClickEvent.openUrl(url.content())));
event.component(newComponent);
}
}