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.CommandCore;
import land.chipmunk.chipmunkmod.modules.KaboomCheck; import land.chipmunk.chipmunkmod.modules.KaboomCheck;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextReplacementConfig; import net.kyori.adventure.text.event.HoverEvent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor; 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.format.TextDecoration;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
@ -21,47 +21,36 @@ import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.regex.Pattern;
public class CustomChat { public class CustomChat {
private static final LegacyComponentSerializer LEGACY = LegacyComponentSerializer.legacyAmpersand();
private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson(); private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();
private static final CustomChatComponentRenderer RENDERER = new CustomChatComponentRenderer(); 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 LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer
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;
}
if (b.groupCount() < 1) { // https://sus.red/abc?a=b&c=d will still break the click events, though
return null; .legacyAmpersand()
}
final String content = b.group(1); .toBuilder()
final String url;
/* .extractUrls(
Minecraft doesn't accept "www.google.com" as a URL Style.style(
in click events NamedTextColor.BLUE,
*/ TextDecoration.UNDERLINED,
if (content.contains("://")) { HoverEvent.showText(
url = content; Component
} else { .text("Click here to open the URL")
url = "https://" + content; .color(NamedTextColor.BLUE)
} )
)
)
return Component.text(content, NamedTextColor.BLUE) // > `this.useTerriblyStupidHexFormat = true`
.decorate(TextDecoration.UNDERLINED) // i know it is stupid indeed,
.clickEvent(ClickEvent.openUrl(url)); // but i also want the compatibility to the kaboom chat
}) .useUnusualXRepeatedCharacterHexFormat() // &x&1&2&3&4&5&6abc
.build();
.build();
private final MinecraftClient client; private final MinecraftClient client;
@ -120,22 +109,21 @@ public class CustomChat {
return; return;
} }
final Component styledMessage = LEGACY.deserialize(message) final Component styledMessage = SERIALIZER.deserialize(message);
.replaceText(URL_REPLACEMENT_CONFIG);
final String username = MinecraftClient.getInstance().getSession().getUsername(); final String username = MinecraftClient.getInstance().getSession().getUsername();
final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey; final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey;
final String hash = key != null ? final String hash = key != null ?
Hashing.sha256() Hashing.sha256()
.hashString(key + total, StandardCharsets.UTF_8) .hashString(key + total, StandardCharsets.UTF_8)
.toString() .toString()
.substring(0, 8) : .substring(0, 8) :
""; "";
total++; total++;
final CustomChatContext context = new CustomChatContext(player.getUuidAsString(), styledMessage, final CustomChatContext context = new CustomChatContext(player.getUuidAsString(), styledMessage,
Map.of("MESSAGE", message, "USERNAME", username, "HASH", hash)); Map.of("MESSAGE", message, "USERNAME", username, "HASH", hash));
final Component renderedFormat = RENDERER.render(ChipmunkMod.CONFIG.customChat.format, context) final Component renderedFormat = RENDERER.render(ChipmunkMod.CONFIG.customChat.format, context)
.compact(); .compact();
final String json = GSON.serialize(renderedFormat); final String json = GSON.serialize(renderedFormat);