Skip to content

Commit 8812f21

Browse files
authored
GH-19 Add AdventureUrlPostProcessor (#19)
* Add AdventureUrlPostProcessor * Add AdventureUrlPostProcessor
1 parent 6237700 commit 8812f21

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

buildSrc/src/main/kotlin/commons-publish.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group = "com.eternalcode"
7-
version = "1.1.3-SNAPSHOT"
7+
version = "1.1.3"
88

99
java {
1010
withSourcesJar()

eternalcode-commons-adventure/src/main/java/com/eternalcode/commons/adventure/AdventureLegacyColorPostProcessor.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
package com.eternalcode.commons.adventure;
22

3-
import java.util.function.Consumer;
43
import java.util.function.UnaryOperator;
54
import java.util.regex.Pattern;
65
import net.kyori.adventure.text.Component;
76
import net.kyori.adventure.text.TextReplacementConfig;
8-
import net.kyori.adventure.text.TextReplacementConfig.Builder;
9-
import org.jetbrains.annotations.NotNull;
107

118
public class AdventureLegacyColorPostProcessor implements UnaryOperator<Component> {
129

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.eternalcode.commons.adventure;
2+
3+
import java.util.function.UnaryOperator;
4+
import java.util.regex.Pattern;
5+
import net.kyori.adventure.text.Component;
6+
import net.kyori.adventure.text.TextReplacementConfig;
7+
import net.kyori.adventure.text.event.ClickEvent;
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class AdventureUrlPostProcessor implements UnaryOperator<Component> {
11+
12+
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()!@:%_\\+.~#?&\\/\\/=]*)");
13+
14+
public static final @NotNull TextReplacementConfig CLICKABLE_URL_CONFIG = TextReplacementConfig.builder()
15+
.match(URL_PATTERN)
16+
.replacement(url -> url.clickEvent(ClickEvent.openUrl(url.content())))
17+
.build();
18+
19+
@Override
20+
public Component apply(Component component) {
21+
return component.replaceText(CLICKABLE_URL_CONFIG);
22+
}
23+
}

0 commit comments

Comments
 (0)