From 275aca9d0593f33866b30ccb5d366141456daaf8 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+chomens@users.noreply.github.com> Date: Thu, 2 Jan 2025 08:58:33 +0700 Subject: [PATCH] feat: URL clickies or something refactor: some stuff with MESSAGE replacing (not sure if it will break some configs) --- .../chipmunk/chipmunkmod/Configuration.java | 4 +- .../chipmunkmod/modules/CustomChat.java | 54 ++++++++++++++++--- 2 files changed, 50 insertions(+), 8 deletions(-) diff --git a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java index a7a4ccf..4f06a0a 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/Configuration.java +++ b/src/main/java/land/chipmunk/chipmunkmod/Configuration.java @@ -83,6 +83,8 @@ public class Configuration { public @NotNull Component format = Component.translatable("chat.type.text", Component.selector("UUID"), - Component.text("MESSAGE")); + Component.empty() + .append(Component.text("MESSAGE")) + ); } } diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java b/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java index 8599206..18c1507 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java @@ -4,7 +4,10 @@ import com.google.common.hash.Hashing; import land.chipmunk.chipmunkmod.ChipmunkMod; import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.TextReplacementConfig; +import net.kyori.adventure.text.event.ClickEvent; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.format.TextDecoration; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.minecraft.client.MinecraftClient; @@ -14,10 +17,46 @@ import net.minecraft.client.network.ClientPlayerEntity; import java.nio.charset.StandardCharsets; import java.util.Timer; import java.util.TimerTask; +import java.util.regex.Pattern; public class CustomChat { private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson(); + // 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; + } + + if (b.groupCount() < 1) { + return null; + } + + final String content = b.group(1); + final String url; + + /* + Minecraft doesn't accept "www.google.com" as a URL + in click events + */ + if (content.contains("://")) { + url = content; + } else { + url = "https://" + content; + } + + return Component.text(content, NamedTextColor.BLUE) + .decorate(TextDecoration.UNDERLINED) + .clickEvent(ClickEvent.openUrl(url)); + }) + .build(); + private final MinecraftClient client; public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance()); @@ -89,12 +128,14 @@ public class CustomChat { .replace("\\", "\\\\") .replace("\"", "\\\""); - final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand(); - final String randomized = String.valueOf(Math.random()); - final Component deserialized = serializer.deserialize(message); - final String messageWithColor = GsonComponentSerializer.gson().serialize(deserialized).replace("MESSAGE", randomized); + final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand(); + + final Component messageWithColors = serializer.deserialize(message); + final Component completeMessage = messageWithColors.replaceText(URL_REPLACEMENT_CONFIG); + + final String stringMessage = GSON.serialize(completeMessage).replace("MESSAGE", randomized); final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey; @@ -122,9 +163,8 @@ public class CustomChat { .replace("USERNAME", username) .replace("UUID", player.getUuidAsString()) .replace("HASH", hash) - .replace("{\"text\":\"MESSAGE\"}", messageWithColor) - .replace("\"extra\":[\"MESSAGE\"],\"color\":", "\"extra\":[" + messageWithColor + "],\"color\":") - .replace("MESSAGE", sanitizedMessage.replaceAll("&.", "")) + .replace("\"extra\":[\"MESSAGE\"]", "\"extra\":[" + stringMessage + "]") + .replace("MESSAGE", sanitizedMessage) .replace(randomized, "MESSAGE"); // ohio ohio CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + sanitizedFormat);