diff --git a/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java b/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java index bce0b6e3..195f50c8 100644 --- a/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java +++ b/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatFormatterPlugin.java @@ -16,11 +16,9 @@ import net.kyori.adventure.platform.AudienceProvider; import net.kyori.adventure.platform.bukkit.BukkitAudiences; import net.kyori.adventure.text.minimessage.MiniMessage; -import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; import org.bstats.bukkit.Metrics; import org.bukkit.Server; import org.bukkit.plugin.Plugin; -import org.bukkit.plugin.java.JavaPlugin; import java.util.concurrent.TimeUnit; diff --git a/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatHandlerImpl.java b/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatHandlerImpl.java index cf9b959b..a1dc93b1 100644 --- a/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatHandlerImpl.java +++ b/chatformatter-core/src/main/java/com/eternalcode/formatter/ChatHandlerImpl.java @@ -1,8 +1,8 @@ package com.eternalcode.formatter; +import com.eternalcode.formatter.adventure.AdventureUrlPostProcessor; import java.util.Optional; import net.kyori.adventure.text.serializer.json.JSONOptions; -import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.builder; import static net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer.legacySection; import com.eternalcode.formatter.adventure.TextColorTagResolver; @@ -75,6 +75,7 @@ class ChatHandlerImpl implements ChatHandler { .build(); private static final MiniMessage EMPTY_MESSAGE_DESERIALIZER = MiniMessage.builder() + .postProcessor(new AdventureUrlPostProcessor()) .tags(TagResolver.empty()) .build(); diff --git a/chatformatter-core/src/main/java/com/eternalcode/formatter/adventure/AdventureUrlPostProcessor.java b/chatformatter-core/src/main/java/com/eternalcode/formatter/adventure/AdventureUrlPostProcessor.java new file mode 100644 index 00000000..77a8ceb2 --- /dev/null +++ b/chatformatter-core/src/main/java/com/eternalcode/formatter/adventure/AdventureUrlPostProcessor.java @@ -0,0 +1,23 @@ +package com.eternalcode.formatter.adventure; + +import java.util.function.UnaryOperator; +import java.util.regex.Pattern; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.event.ClickEvent; +import org.jetbrains.annotations.NotNull; + +public class AdventureUrlPostProcessor implements UnaryOperator { + + private static final Pattern URL_PATTERN = Pattern.compile("https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{1,256}\\.[a-zA-Z0-9()]{1,6}\\b([-a-zA-Z0-9()!@:%_\\+.~#?&\\/\\/=]*)"); + + public static final @NotNull TextReplacementConfig CLICKABLE_URL_CONFIG = TextReplacementConfig.builder() + .match(URL_PATTERN) + .replacement(url -> url.clickEvent(ClickEvent.openUrl(url.content()))) + .build(); + + @Override + public Component apply(Component component) { + return component.replaceText(CLICKABLE_URL_CONFIG); + } +} \ No newline at end of file