refactor: use our own LegacyComponentSerializer with builder extractUrls for url replacement instead of the extras one

This commit is contained in:
Chayapak Supasakul 2025-04-10 14:18:36 +07:00
parent fff76b9cc2
commit a085756c7d
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA

View file

@ -7,9 +7,9 @@ import land.chipmunk.chipmunkmod.modules.Chat;
import land.chipmunk.chipmunkmod.modules.CommandCore;
import land.chipmunk.chipmunkmod.modules.KaboomCheck;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.text.format.TextDecoration;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -21,46 +21,35 @@ import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern;
public class CustomChat {
private static final LegacyComponentSerializer LEGACY = LegacyComponentSerializer.legacyAmpersand();
private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();
private static final CustomChatComponentRenderer RENDERER = new CustomChatComponentRenderer();
// https://github.com/kaboomserver/extras/blob/master/src/main/java/pw/kaboom/extras/modules/player/PlayerChat.java#L49C9-L81C26
private static final TextReplacementConfig URL_REPLACEMENT_CONFIG =
TextReplacementConfig
.builder()
.match(Pattern
.compile("((https?://(ww(w|\\d)\\.)?|ww(w|\\d))[-a-zA-Z0-9@:%._+~#=]{1,256}"
+ "\\.[a-zA-Z0-9]{2,6}\\b([-a-zA-Z0-9@:%_+.~#?&/=]*))"))
.replacement((b, c) -> {
if (c == null) {
return null;
}
private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer
if (b.groupCount() < 1) {
return null;
}
// https://sus.red/abc?a=b&c=d will still break the click events, though
.legacyAmpersand()
final String content = b.group(1);
final String url;
.toBuilder()
/*
Minecraft doesn't accept "www.google.com" as a URL
in click events
*/
if (content.contains("://")) {
url = content;
} else {
url = "https://" + content;
}
.extractUrls(
Style.style(
NamedTextColor.BLUE,
TextDecoration.UNDERLINED,
HoverEvent.showText(
Component
.text("Click here to open the URL")
.color(NamedTextColor.BLUE)
)
)
)
// > `this.useTerriblyStupidHexFormat = true`
// i know it is stupid indeed,
// but i also want the compatibility to the kaboom chat
.useUnusualXRepeatedCharacterHexFormat() // &x&1&2&3&4&5&6abc
return Component.text(content, NamedTextColor.BLUE)
.decorate(TextDecoration.UNDERLINED)
.clickEvent(ClickEvent.openUrl(url));
})
.build();
private final MinecraftClient client;
@ -120,8 +109,7 @@ public class CustomChat {
return;
}
final Component styledMessage = LEGACY.deserialize(message)
.replaceText(URL_REPLACEMENT_CONFIG);
final Component styledMessage = SERIALIZER.deserialize(message);
final String username = MinecraftClient.getInstance().getSession().getUsername();
final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey;