mirror of
https://code.chipmunk.land/ChomeNS/chipmunkmod.git
synced 2025-11-13 19:56:15 +00:00
feat: URL clickies or something
refactor: some stuff with MESSAGE replacing (not sure if it will break some configs)
This commit is contained in:
parent
1d09eab935
commit
275aca9d05
2 changed files with 50 additions and 8 deletions
|
|
@ -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"))
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue