Skip to content

Commit 0c2264f

Browse files
committed
feat: add chat formatter
1 parent 02eac10 commit 0c2264f

File tree

7 files changed

+86
-1
lines changed

7 files changed

+86
-1
lines changed

common/src/main/java/com/euphony/better_client/client/events/BeautifiedChatEvent.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,4 @@ public static Component processMessage(Component message) {
3636
}
3737
return message.copy();
3838
}
39-
4039
}

common/src/main/java/com/euphony/better_client/config/Config.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public class Config {
9797

9898
public boolean enableSuspiciousStewTooltip = true;
9999

100+
public boolean enableChatFormatter = true;
101+
public String posFormat = "{x}, {y}, {z}";
102+
100103
public Config() {}
101104

102105
public static Config create() {

common/src/main/java/com/euphony/better_client/config/screen/category/ChatConfigScreen.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import dev.isxander.yacl3.api.YetAnotherConfigLib;
99
import dev.isxander.yacl3.api.controller.ColorControllerBuilder;
1010
import dev.isxander.yacl3.api.controller.IntegerFieldControllerBuilder;
11+
import dev.isxander.yacl3.api.controller.StringControllerBuilder;
1112
import net.minecraft.client.gui.screens.Screen;
1213
import net.minecraft.network.chat.Component;
1314

@@ -21,6 +22,7 @@
2122
public class ChatConfigScreen {
2223
private static final String BETTER_CHAT_GROUP = "better_chat";
2324
private static final String TIMESTAMP_GROUP = "timestamp";
25+
private static final String CHAT_FORMATTER_GROUP = "chat_formatter";
2426

2527
public static Screen generateScreen(Screen parent) {
2628
// Better Chat
@@ -60,6 +62,21 @@ public static Screen generateScreen(Screen parent) {
6062
newVal -> config.enableChatHistoryRetention = newVal
6163
);
6264

65+
// Chat Formatter
66+
Option<Boolean> enableChatFormatterOpt = ConfigUtils.buildBooleanOption(
67+
"enableChatFormatter",
68+
DEFAULTS.enableChatFormatter,
69+
() -> config.enableChatFormatter,
70+
newVal -> config.enableChatFormatter = newVal
71+
);
72+
73+
Option<String> posFormatOpt = ConfigUtils.<String>getGenericOption("posFormat")
74+
.binding(DEFAULTS.posFormat,
75+
() -> config.posFormat,
76+
newVal -> config.posFormat = newVal)
77+
.controller(StringControllerBuilder::create)
78+
.build();
79+
6380
return YetAnotherConfigLib.createBuilder()
6481
.title(Component.translatable("yacl3.config.better_client:config"))
6582
.category(ConfigCategory.createBuilder()
@@ -79,6 +96,13 @@ public static Screen generateScreen(Screen parent) {
7996
timeStampColorOpt
8097
))
8198
.build())
99+
.group(OptionGroup.createBuilder()
100+
.name(ConfigUtils.getGroupName(CLIENT_CATEGORY, CHAT_FORMATTER_GROUP))
101+
.options(List.of(
102+
enableChatFormatterOpt,
103+
posFormatOpt
104+
))
105+
.build())
82106
.build())
83107
.save(Config::save)
84108
.build()

common/src/main/java/com/euphony/better_client/mixin/ClientPacketListenerMixin.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
package com.euphony.better_client.mixin;
22

33
import com.euphony.better_client.client.events.TradingHudEvent;
4+
import com.euphony.better_client.utils.FormatUtils;
45
import com.euphony.better_client.utils.data.MerchantInfo;
56
import net.minecraft.client.Minecraft;
67
import net.minecraft.client.multiplayer.ClientPacketListener;
8+
import net.minecraft.core.BlockPos;
79
import net.minecraft.network.protocol.game.ClientboundMerchantOffersPacket;
810
import net.minecraft.network.protocol.game.ClientboundOpenScreenPacket;
911
import net.minecraft.network.protocol.game.ServerboundContainerClosePacket;
12+
import net.minecraft.world.entity.player.Player;
1013
import net.minecraft.world.inventory.MenuType;
1114
import org.spongepowered.asm.mixin.Mixin;
1215
import org.spongepowered.asm.mixin.Unique;
1316
import org.spongepowered.asm.mixin.injection.At;
1417
import org.spongepowered.asm.mixin.injection.Inject;
18+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
1519
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1620

21+
import java.util.Map;
22+
1723
import static com.euphony.better_client.BetterClient.config;
1824

1925
/**
@@ -66,4 +72,24 @@ public void onHandleOpenScreen(ClientboundOpenScreenPacket packet, CallbackInfo
6672
minecraft.player.connection.send(new ServerboundContainerClosePacket(containerId));
6773
}
6874
}
75+
76+
@ModifyVariable(method = "sendChat", at = @At("HEAD"), argsOnly = true)
77+
public String sendPublicMessage(String message) {
78+
if (!config.enableChatFormatter) return message;
79+
80+
Player player = Minecraft.getInstance().player;
81+
if (player == null) {
82+
return message;
83+
}
84+
85+
BlockPos pos = player.getOnPos();
86+
87+
return FormatUtils.format(message, Map.of(
88+
"pos", FormatUtils.format(config.posFormat, Map.of(
89+
"x", pos.getX(),
90+
"y", pos.getY(),
91+
"z", pos.getZ()
92+
))
93+
));
94+
}
6995
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.euphony.better_client.utils;
2+
3+
import java.util.Map;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
public class FormatUtils {
8+
private static final Pattern pattern = Pattern.compile("\\{\\s*(\\w+)\\s*\\}");
9+
10+
public static String format(String template, Map<String, Object> values) {
11+
Matcher matcher = pattern.matcher(template);
12+
StringBuffer sb = new StringBuffer();
13+
while (matcher.find()) {
14+
String key = matcher.group(1);
15+
Object replacement = values.getOrDefault(key, "{" + key + "}");
16+
matcher.appendReplacement(sb, Matcher.quoteReplacement(String.valueOf(replacement)));
17+
}
18+
matcher.appendTail(sb);
19+
return sb.toString();
20+
}
21+
}

common/src/main/resources/assets/better_client/lang/en_us.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
"yacl3.config.better_client:config.enableChatHistoryRetention": "Enable Chat History Retention",
7878
"yacl3.config.better_client:config.enableChatHistoryRetention.desc": "Leaving the world no longer clears your chat history.",
7979

80+
"yacl3.config.better_client:config.category.client.group.chat_formatter": "Chat Formatter",
81+
"yacl3.config.better_client:config.enableChatFormatter": "Enable Chat Formatter",
82+
"yacl3.config.better_client:config.enableChatFormatter.desc": "Automatic replacement of placeholders like {pos} with actual player coordinates.",
83+
"yacl3.config.better_client:config.posFormat": "Pos Format",
84+
"yacl3.config.better_client:config.posFormat.desc": "Defines how coordinates are formatted when replacing {pos}. Supports placeholders {x}, {y}, and {z}.",
85+
8086
"yacl3.config.better_client:config.category.client.group.biome_title": "Biome Title",
8187
"yacl3.config.better_client:config.enableBiomeTitle": "Enable Biome Title",
8288
"yacl3.config.better_client:config.enableBiomeTitle.desc": "Displays a title when entering a new biome.",

common/src/main/resources/assets/better_client/lang/zh_cn.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@
7777
"yacl3.config.better_client:config.enableChatHistoryRetention": "启用聊天历史保留",
7878
"yacl3.config.better_client:config.enableChatHistoryRetention.desc": "离开世界将不再清除聊天历史。",
7979

80+
"yacl3.config.better_client:config.category.client.group.chat_formatter": "聊天格式化",
81+
"yacl3.config.better_client:config.enableChatFormatter": "启用聊天格式化",
82+
"yacl3.config.better_client:config.enableChatFormatter.desc": "自动将 {pos} 等占位符替换为玩家实际坐标",
83+
"yacl3.config.better_client:config.posFormat": "坐标格式",
84+
"yacl3.config.better_client:config.posFormat.desc": "定义替换 {pos} 时坐标的格式化规则。支持 {x}、{y} 和 {z} 占位符。",
85+
8086
"yacl3.config.better_client:config.category.client.group.biome_title": "生物群系标题",
8187
"yacl3.config.better_client:config.enableBiomeTitle": "启用生物群系标题",
8288
"yacl3.config.better_client:config.enableBiomeTitle.desc": "进入新生物群系时显示标题。",

0 commit comments

Comments
 (0)