fix: .customchat format not working

refactor: remove useless timer in CustomChat (not even sure why it's there)
This commit is contained in:
Chayapak Supasakul 2025-04-16 17:24:27 +07:00
parent 0b31a366cb
commit 69909de327
Signed by: ChomeNS
SSH key fingerprint: SHA256:0YoxhdyXsgbc0nfeB2N6FYE60mxMU7DS4uCUMaw2mvA
3 changed files with 9 additions and 35 deletions

View file

@ -3,8 +3,10 @@ package land.chipmunk.chipmunkmod.commands;
import com.mojang.brigadier.Command; import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import land.chipmunk.chipmunkmod.ChipmunkMod;
import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat; import land.chipmunk.chipmunkmod.modules.custom_chat.CustomChat;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import net.minecraft.text.Text; import net.minecraft.text.Text;
import static com.mojang.brigadier.arguments.BoolArgumentType.bool; import static com.mojang.brigadier.arguments.BoolArgumentType.bool;
@ -15,6 +17,8 @@ import static land.chipmunk.chipmunkmod.command.CommandManager.argument;
import static land.chipmunk.chipmunkmod.command.CommandManager.literal; import static land.chipmunk.chipmunkmod.command.CommandManager.literal;
public class CustomChatCommand { public class CustomChatCommand {
private static final GsonComponentSerializer GSON = GsonComponentSerializer.gson();
public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) { public static void register (final CommandDispatcher<FabricClientCommandSource> dispatcher) {
dispatcher.register( dispatcher.register(
literal("customchat") literal("customchat")
@ -47,7 +51,7 @@ public class CustomChatCommand {
public static int setFormat (final CommandContext<FabricClientCommandSource> context) { public static int setFormat (final CommandContext<FabricClientCommandSource> context) {
final FabricClientCommandSource source = context.getSource(); final FabricClientCommandSource source = context.getSource();
final String format = getString(context, "format"); final String format = getString(context, "format");
CustomChat.INSTANCE.format = format; CustomChat.INSTANCE.format = GSON.deserializeOr(format, ChipmunkMod.CONFIG.customChat.format);
source.sendFeedback(Text.literal("Set the custom chat format to: " + format)); source.sendFeedback(Text.literal("Set the custom chat format to: " + format));
return Command.SINGLE_SUCCESS; return Command.SINGLE_SUCCESS;

View file

@ -50,7 +50,6 @@ public class ClientPlayNetworkHandlerMixin {
SongPlayer.INSTANCE.coreReady(); SongPlayer.INSTANCE.coreReady();
RainbowName.INSTANCE.init(); RainbowName.INSTANCE.init();
ChomeNSBotCommandSuggestions.INSTANCE.init(); ChomeNSBotCommandSuggestions.INSTANCE.init();
CustomChat.INSTANCE.init();
} }
@Inject(method = "onCommandTree", at = @At("TAIL")) @Inject(method = "onCommandTree", at = @At("TAIL"))

View file

@ -12,16 +12,14 @@ 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;
import net.minecraft.client.network.ClientPlayNetworkHandler;
import net.minecraft.client.network.ClientPlayerEntity; import net.minecraft.client.network.ClientPlayerEntity;
import java.util.Map; import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Pattern; import java.util.regex.Pattern;
public class CustomChat { public class CustomChat {
public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance()); public static final CustomChat INSTANCE = new CustomChat(MinecraftClient.getInstance());
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();
private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer private static final LegacyComponentSerializer SERIALIZER = LegacyComponentSerializer
@ -52,43 +50,16 @@ public class CustomChat {
.useUnusualXRepeatedCharacterHexFormat() // &x&1&2&3&4&5&6abc .useUnusualXRepeatedCharacterHexFormat() // &x&1&2&3&4&5&6abc
.build(); .build();
private final MinecraftClient client; private final MinecraftClient client;
public boolean enabled = true; public boolean enabled = true;
public String format; public Component format = ChipmunkMod.CONFIG.customChat.format;
private Timer timer;
public CustomChat (final MinecraftClient client) { public CustomChat (final MinecraftClient client) {
this.client = client; this.client = client;
} }
public void init () {
final TimerTask task = new TimerTask() {
public void run () {
tick();
}
};
timer = new Timer();
timer.schedule(task, 0, 50);
}
private void tick () {
final ClientPlayNetworkHandler networkHandler = client.getNetworkHandler();
if (networkHandler != null) return;
cleanup();
}
private void cleanup () {
if (timer == null) return;
timer.cancel();
timer.purge();
}
public void chat (final String message) { public void chat (final String message) {
final ClientPlayerEntity player = client.player; final ClientPlayerEntity player = client.player;
if (player == null) return; if (player == null) return;
@ -103,7 +74,7 @@ public class CustomChat {
final CustomChatContext context = new CustomChatContext(player.getUuidAsString(), styledMessage, final CustomChatContext context = new CustomChatContext(player.getUuidAsString(), styledMessage,
Map.of("MESSAGE", message, "USERNAME", username)); Map.of("MESSAGE", message, "USERNAME", username));
final Component renderedFormat = RENDERER.render(ChipmunkMod.CONFIG.customChat.format, context) final Component renderedFormat = RENDERER.render(format, context)
.compact(); .compact();
final String json = GSON.serialize(renderedFormat); final String json = GSON.serialize(renderedFormat);