From 8ae88917e1614aa583a1f53d33839c9cc0ca0575 Mon Sep 17 00:00:00 2001 From: ChomeNS <95471003+ChomeNS@users.noreply.github.com> Date: Sat, 6 May 2023 14:14:26 +0700 Subject: [PATCH] add color code support (ohio code) <3 --- build.gradle | 2 ++ .../chipmunk/chipmunkmod/modules/CustomChat.java | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 94302aa..fb1a455 100644 --- a/build.gradle +++ b/build.gradle @@ -31,6 +31,8 @@ dependencies { modImplementation include("net.kyori:adventure-platform-fabric:5.8.0") // for Minecraft 1.19.4 + modImplementation include("net.kyori:adventure-text-serializer-legacy:4.13.1") + // Uncomment the following line to enable the deprecated Fabric API modules. // These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time. diff --git a/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java b/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java index 0f39dfc..d274541 100644 --- a/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java +++ b/src/main/java/land/chipmunk/chipmunkmod/modules/CustomChat.java @@ -6,6 +6,8 @@ import lombok.Getter; import lombok.Setter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; +import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; +import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer; import net.minecraft.client.MinecraftClient; import net.minecraft.client.network.ClientPlayNetworkHandler; @@ -40,6 +42,11 @@ public class CustomChat { .replace("\\", "\\\\") .replace("\"", "\\\""); + final LegacyComponentSerializer serializer = LegacyComponentSerializer.legacyAmpersand(); + + final Component deserialized = serializer.deserialize(sanitizedMessage); + final String messageWithColor = GsonComponentSerializer.gson().serialize(deserialized); + try { // final MutablePlayerListEntry entry = Players.INSTANCE.getEntry(client.getNetworkHandler().getProfile().getId()); @@ -48,11 +55,17 @@ public class CustomChat { // final String prefix = GsonComponentSerializer.gson().serialize(Component.join(JoinConfiguration.separator(Component.empty()), displayNameComponent.children().get(0))); // final String displayName = GsonComponentSerializer.gson().serialize(Component.join(JoinConfiguration.separator(Component.empty()), displayNameComponent.children().get(1))); + // TODO: make this code not ohio code.,., final String sanitizedFormat = format // .replace("\"PREFIX\"", prefix) // .replace("\"DISPLAYNAME\"", displayName) .replace("USERNAME", username) - .replace("MESSAGE", sanitizedMessage); + + // these 2 lines are toooooo ohio code + .replace("{\"text\":\"MESSAGE\"}", messageWithColor) + .replace("\"extra\":[\"MESSAGE\"],\"color\":", "\"extra\":[" + messageWithColor + "],\"color\":") + + .replace("MESSAGE", sanitizedMessage.replaceAll("&.", "")); // TODO: make this not use regex CommandCore.INSTANCE.run("minecraft:tellraw @a " + sanitizedFormat); } catch (Exception e) {