feat: URL clickies or something

refactor: some stuff with MESSAGE replacing (not sure if it will break some configs)
This commit is contained in:
Chayapak Supasakul 2025-01-02 08:58:33 +07:00
parent 1d09eab935
commit 275aca9d05
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
2 changed files with 50 additions and 8 deletions

View file

@ -83,6 +83,8 @@ public class Configuration {
public @NotNull Component format = public @NotNull Component format =
Component.translatable("chat.type.text", Component.translatable("chat.type.text",
Component.selector("UUID"), Component.selector("UUID"),
Component.text("MESSAGE")); Component.empty()
.append(Component.text("MESSAGE"))
);
} }
} }

View file

@ -4,7 +4,10 @@ import com.google.common.hash.Hashing;
import land.chipmunk.chipmunkmod.ChipmunkMod; import land.chipmunk.chipmunkmod.ChipmunkMod;
import net.kyori.adventure.text.Component; 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.NamedTextColor;
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;
import net.minecraft.client.MinecraftClient; import net.minecraft.client.MinecraftClient;
@ -14,10 +17,46 @@ import net.minecraft.client.network.ClientPlayerEntity;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
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 GsonComponentSerializer GSON = GsonComponentSerializer.gson(); 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; private final MinecraftClient client;
public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance()); public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance());
@ -89,12 +128,14 @@ public class CustomChat {
.replace("\\", "\\\\") .replace("\\", "\\\\")
.replace("\"", "\\\""); .replace("\"", "\\\"");
final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand();
final String randomized = String.valueOf(Math.random()); final String randomized = String.valueOf(Math.random());
final Component deserialized = serializer.deserialize(message); final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand();
final String messageWithColor = GsonComponentSerializer.gson().serialize(deserialized).replace("MESSAGE", randomized);
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; final String key = ChipmunkMod.CONFIG.bots.chomens.formatKey;
@ -122,9 +163,8 @@ public class CustomChat {
.replace("USERNAME", username) .replace("USERNAME", username)
.replace("UUID", player.getUuidAsString()) .replace("UUID", player.getUuidAsString())
.replace("HASH", hash) .replace("HASH", hash)
.replace("{\"text\":\"MESSAGE\"}", messageWithColor) .replace("\"extra\":[\"MESSAGE\"]", "\"extra\":[" + stringMessage + "]")
.replace("\"extra\":[\"MESSAGE\"],\"color\":", "\"extra\":[" + messageWithColor + "],\"color\":") .replace("MESSAGE", sanitizedMessage)
.replace("MESSAGE", sanitizedMessage.replaceAll("&.", ""))
.replace(randomized, "MESSAGE"); // ohio ohio .replace(randomized, "MESSAGE"); // ohio ohio
CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + sanitizedFormat); CommandCore.INSTANCE.run((KaboomCheck.INSTANCE.isKaboom ? "minecraft:tellraw @a " : "tellraw @a ") + sanitizedFormat);