Skip to content

Commit f042aa8

Browse files
committed
refactor: replace § color codes with Minecraft Text API and add custom styling
1 parent d027c32 commit f042aa8

3 files changed

Lines changed: 81 additions & 80 deletions

File tree

src/main/java/org/damon233/performtrackermod/command/PtrackerCommand.java

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
88
import net.minecraft.server.command.CommandManager;
99
import net.minecraft.server.command.ServerCommandSource;
10+
import net.minecraft.text.MutableText;
1011
import net.minecraft.text.Text;
1112
import org.damon233.performtrackermod.PerformTracker;
1213
import org.damon233.performtrackermod.collector.SystemInfoCollector;
@@ -101,26 +102,24 @@ private enum ConfigType {
101102
void sendSuccess(ServerCommandSource source, Object value) {
102103
Text name = Text.translatable(translationKey);
103104
Text val = value instanceof Boolean ?
104-
Text.translatable((Boolean) value ? "performtracker.config.value.true" : "performtracker.config.value.false") :
105-
Text.literal(TranslationService.colorValue(String.valueOf(value)));
105+
Text.translatable(Boolean.TRUE.equals(value) ? "performtracker.config.value.true" : "performtracker.config.value.false") :
106+
TranslationService.colorValue(String.valueOf(value));
106107
source.sendFeedback(() -> Text.translatable("performtracker.config.set.success",
107-
Text.literal(TranslationService.colorLabel(name.getString())), val), false);
108+
TranslationService.colorLabel(name.getString()), val), false);
108109
}
109110

110111
void sendInfo(ServerCommandSource source) {
111112
source.sendFeedback(() -> Text.translatable("performtracker.config.info",
112-
Text.literal(TranslationService.colorLabel(Text.translatable(translationKey).getString())),
113-
Text.literal(TranslationService.colorValue(String.valueOf(getter.get()))),
114-
Text.literal(TranslationService.colorValue(String.valueOf(defaultGetter.get())))), false);
113+
TranslationService.colorLabel(Text.translatable(translationKey).getString()),
114+
TranslationService.colorValue(String.valueOf(getter.get())),
115+
TranslationService.colorValue(String.valueOf(defaultGetter.get()))), false);
115116
}
116117

117118
void sendLine(ServerCommandSource source) {
118-
Text current = Text.literal(TranslationService.colorValue(getter.get() instanceof Boolean ?
119-
((Boolean) getter.get() ? "true" : "false") :
120-
String.valueOf(getter.get())));
121-
Text def = Text.literal(TranslationService.colorValue(defaultGetter.get() instanceof Boolean ?
122-
((Boolean) defaultGetter.get() ? "true" : "false") :
123-
String.valueOf(defaultGetter.get())));
119+
MutableText current = TranslationService.colorValue(
120+
Boolean.TRUE.equals(getter.get()) ? "true" : "false");
121+
MutableText def = TranslationService.colorValue(
122+
Boolean.TRUE.equals(defaultGetter.get()) ? "true" : "false");
124123
source.sendFeedback(() -> Text.translatable(translationKey).append(": ").append(current).append(" (default: ").append(def).append(")"), false);
125124
}
126125
}
@@ -205,27 +204,27 @@ public static void register() {
205204
ptracker.then(CommandManager.literal("deviceinfo").executes(ctx -> {
206205
SystemInfo info = SystemInfoCollector.collect();
207206

208-
String title = Text.translatable("performtracker.device.title").getString();
207+
MutableText title = TranslationService.colorLabel(Text.translatable("performtracker.device.title").getString());
209208
String unknown = Text.translatable("performtracker.device.unknown").getString();
210-
String typeLabel = Text.translatable("performtracker.device.type").getString();
211-
String modelLabel = Text.translatable("performtracker.device.model").getString();
212-
String cpuLabel = Text.translatable("performtracker.device.cpu").getString();
213-
String gpuLabel = Text.translatable("performtracker.device.gpu").getString();
214-
String coresLabel = Text.translatable("performtracker.device.cpu_cores").getString();
215-
String memLabel = Text.translatable("performtracker.device.memory").getString();
216-
String osLabel = Text.translatable("performtracker.device.os").getString();
217-
String javaLabel = Text.translatable("performtracker.device.java").getString();
209+
MutableText typeLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.type").getString());
210+
MutableText modelLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.model").getString());
211+
MutableText cpuLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.cpu").getString());
212+
MutableText gpuLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.gpu").getString());
213+
MutableText coresLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.cpu_cores").getString());
214+
MutableText memLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.memory").getString());
215+
MutableText osLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.os").getString());
216+
MutableText javaLabel = TranslationService.colorLabel(Text.translatable("performtracker.device.java").getString());
218217
String typeValue = Text.translatable("performtracker.device.type." + info.deviceType().getCode()).getString();
219218

220-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(title)), false);
221-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(typeLabel) + TranslationService.colorValue(typeValue)), false);
222-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(modelLabel) + TranslationService.colorValue(info.deviceModel() != null ? info.deviceModel() : unknown)), false);
223-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(cpuLabel) + TranslationService.colorValue(info.cpuName() != null ? info.cpuName() : unknown)), false);
224-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(gpuLabel) + TranslationService.colorValue(info.gpuName() != null ? info.gpuName() : unknown)), false);
225-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(coresLabel) + TranslationService.colorValue(String.valueOf(info.cpuCores()))), false);
226-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(memLabel) + TranslationService.colorValue(SystemInfo.formatBytes(info.totalMemoryBytes()))), false);
227-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(osLabel) + TranslationService.colorValue(info.osName() + " " + info.osVersion() + " (" + info.osArch() + ")")), false);
228-
ctx.getSource().sendFeedback(() -> Text.literal(TranslationService.colorLabel(javaLabel) + TranslationService.colorValue(info.javaVersion())), false);
219+
ctx.getSource().sendFeedback(() -> title, false);
220+
ctx.getSource().sendFeedback(() -> typeLabel.append(TranslationService.colorValue(typeValue)), false);
221+
ctx.getSource().sendFeedback(() -> modelLabel.append(TranslationService.colorValue(info.deviceModel() != null ? info.deviceModel() : unknown)), false);
222+
ctx.getSource().sendFeedback(() -> cpuLabel.append(TranslationService.colorValue(info.cpuName() != null ? info.cpuName() : unknown)), false);
223+
ctx.getSource().sendFeedback(() -> gpuLabel.append(TranslationService.colorValue(info.gpuName() != null ? info.gpuName() : unknown)), false);
224+
ctx.getSource().sendFeedback(() -> coresLabel.append(TranslationService.colorValue(String.valueOf(info.cpuCores()))), false);
225+
ctx.getSource().sendFeedback(() -> memLabel.append(TranslationService.colorValue(SystemInfo.formatBytes(info.totalMemoryBytes()))), false);
226+
ctx.getSource().sendFeedback(() -> osLabel.append(TranslationService.colorValue(info.osName() + " " + info.osVersion() + " (" + info.osArch() + ")")), false);
227+
ctx.getSource().sendFeedback(() -> javaLabel.append(TranslationService.colorValue(info.javaVersion())), false);
229228

230229
if (!SystemInfoCollector.isChipRulesValid()) {
231230
ctx.getSource().sendFeedback(() -> TranslationService.chatError("performtracker.error.chip_rules_modified"), false);

src/main/java/org/damon233/performtrackermod/controller/TrackerController.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
1111
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
1212
import net.minecraft.server.MinecraftServer;
13+
import net.minecraft.text.MutableText;
1314
import net.minecraft.text.Text;
1415

1516
import org.damon233.performtrackermod.PerformTracker;
@@ -44,7 +45,8 @@ public class TrackerController {
4445
private String currentOutputFormat;
4546

4647
private final Object[] rowValuesBuffer = new Object[6];
47-
private final StringBuilder chatMessageBuilder = new StringBuilder(256);
48+
private final MutableText[] chatMessageParts = new MutableText[10];
49+
private int chatMessagePartCount;
4850

4951
public TrackerController(ServerMetricsCollector serverCollector, IFpsProvider fpsProvider) {
5052
this.serverCollector = serverCollector;
@@ -199,9 +201,9 @@ private void outputMetrics() {
199201
long timestamp = System.currentTimeMillis();
200202

201203
if (ConfigAccess.isChatEnabled() && server != null) {
202-
String chatMsg = buildChatMessage(metrics);
204+
MutableText chatMsg = TranslationService.chatWithMetrics(buildChatMessage(metrics));
203205
server.getPlayerManager().getPlayerList().forEach(player ->
204-
player.sendMessage(TranslationService.chatWithMetrics(chatMsg))
206+
player.sendMessage(chatMsg)
205207
);
206208
}
207209

@@ -223,37 +225,35 @@ private void outputMetrics() {
223225
}
224226
}
225227

226-
private String buildChatMessage(PerformanceMetrics metrics) {
227-
chatMessageBuilder.setLength(0);
228-
boolean first = true;
228+
private MutableText buildChatMessage(PerformanceMetrics metrics) {
229+
chatMessagePartCount = 0;
229230
if (ConfigAccess.isCollectFps()) {
230-
if (!first) chatMessageBuilder.append(" | ");
231-
chatMessageBuilder.append(TranslationService.colorLabel("FPS: ")).append(TranslationService.colorValue(String.format("%.1f", metrics.fps())));
232-
first = false;
231+
addChatPart("FPS: ", String.format("%.1f", metrics.fps()));
233232
}
234233
if (ConfigAccess.isCollectTps()) {
235-
if (!first) chatMessageBuilder.append(" | ");
236-
chatMessageBuilder.append(TranslationService.colorLabel("TPS: ")).append(TranslationService.colorValue(PerformanceMetrics.formatValue(metrics.tps())));
237-
first = false;
234+
addChatPart("TPS: ", PerformanceMetrics.formatValue(metrics.tps()));
238235
}
239236
if (ConfigAccess.isCollectMspt()) {
240-
if (!first) chatMessageBuilder.append(" | ");
241-
chatMessageBuilder.append(TranslationService.colorLabel("MSPT: ")).append(TranslationService.colorValue(PerformanceMetrics.formatValue(metrics.mspt())));
242-
first = false;
237+
addChatPart("MSPT: ", PerformanceMetrics.formatValue(metrics.mspt()));
243238
}
244239
if (ConfigAccess.isCollectHeap()) {
245-
if (!first) chatMessageBuilder.append(" | ");
246-
chatMessageBuilder.append(TranslationService.colorLabel("Heap: "))
247-
.append(TranslationService.colorValue(PerformanceMetrics.formatMemoryMB(metrics.heapUsed())))
248-
.append(" / ")
249-
.append(TranslationService.colorValue(PerformanceMetrics.formatMemoryMB(metrics.heapMax())));
240+
addChatPart("Heap: ", PerformanceMetrics.formatMemoryMB(metrics.heapUsed()) + " / " + PerformanceMetrics.formatMemoryMB(metrics.heapMax()));
250241
}
251242
if (ConfigAccess.isCollectCpu()) {
252-
if (!first) chatMessageBuilder.append(" | ");
253-
chatMessageBuilder.append(TranslationService.colorLabel("CPU: "))
254-
.append(TranslationService.colorValue(String.format("%.1f%%", metrics.cpuUsage())));
243+
addChatPart("CPU: ", String.format("%.1f%%", metrics.cpuUsage()));
255244
}
256-
return chatMessageBuilder.toString();
245+
246+
MutableText result = Text.empty();
247+
for (int i = 0; i < chatMessagePartCount; i++) {
248+
if (i > 0) result = result.append(Text.literal(" | ").withColor(0x888888));
249+
result = result.append(chatMessageParts[i]);
250+
}
251+
return result;
252+
}
253+
254+
private void addChatPart(String label, String value) {
255+
if (chatMessagePartCount >= chatMessageParts.length) return;
256+
chatMessageParts[chatMessagePartCount++] = Text.literal(label).withColor(0x888888).append(TranslationService.colorValue(value));
257257
}
258258

259259
private Object[] buildRowValues(PerformanceMetrics metrics) {

src/main/java/org/damon233/performtrackermod/utils/TranslationService.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@
22

33
import net.minecraft.text.MutableText;
44
import net.minecraft.text.Text;
5+
import net.minecraft.util.Formatting;
56

67
public class TranslationService {
78
private static final String PREFIX = "performtracker.";
8-
private static final String RESET = "\u00A7r";
9-
private static final String GOLD = "\u00A76";
10-
private static final String GREEN = "\u00A7a";
11-
private static final String RED = "\u00A7c";
12-
private static final String AQUA = "\u00A7b";
13-
private static final String GRAY = "\u00A77";
14-
15-
private static final String CHAT_PREFIX = GOLD + "[⚓]" + RESET;
9+
10+
private static final MutableText PREFIX_TEXT = Text.literal("[").withColor(getColorValue(Formatting.DARK_GRAY))
11+
.append(Text.literal("\uD83E\uDE9D").withColor(getColorValue(Formatting.GOLD)))
12+
.append(Text.literal("] ").withColor(getColorValue(Formatting.DARK_GRAY)));
13+
14+
private static int getColorValue(Formatting formatting) {
15+
Integer color = formatting.getColorValue();
16+
return color != null ? color : 0xFFFFFF;
17+
}
1618

1719
public static MutableText get(String key) {
1820
return Text.translatable(PREFIX + key);
@@ -23,38 +25,38 @@ public static MutableText get(String key, Object... args) {
2325
}
2426

2527
public static MutableText chat(String key) {
26-
return Text.literal(CHAT_PREFIX + " ").append(get(key));
28+
return Text.literal("").append(PREFIX_TEXT).append(get(key));
2729
}
2830

2931
public static MutableText chat(String key, Object... args) {
30-
return Text.literal(CHAT_PREFIX + " ").append(get(key, args));
32+
return Text.literal("").append(PREFIX_TEXT).append(get(key, args));
3133
}
32-
34+
3335
public static MutableText chatSuccess(String key) {
34-
return Text.literal(CHAT_PREFIX + " " + GREEN).append(get(key)).append(RESET);
36+
return Text.literal("").append(PREFIX_TEXT).append(Text.literal("").withColor(getColorValue(Formatting.GREEN))).append(get(key));
3537
}
36-
38+
3739
public static MutableText chatSuccess(String key, Object... args) {
38-
return Text.literal(CHAT_PREFIX + " " + GREEN).append(get(key, args)).append(RESET);
40+
return Text.literal("").append(PREFIX_TEXT).append(Text.literal("").withColor(getColorValue(Formatting.GREEN))).append(get(key, args));
3941
}
40-
42+
4143
public static MutableText chatError(String key) {
42-
return Text.literal(CHAT_PREFIX + " " + RED).append(get(key)).append(RESET);
44+
return Text.literal("").append(PREFIX_TEXT).append(Text.literal("").withColor(getColorValue(Formatting.RED))).append(get(key));
4345
}
44-
46+
4547
public static MutableText chatError(String key, Object... args) {
46-
return Text.literal(CHAT_PREFIX + " " + RED).append(get(key, args)).append(RESET);
48+
return Text.literal("").append(PREFIX_TEXT).append(Text.literal("").withColor(getColorValue(Formatting.RED))).append(get(key, args));
4749
}
4850

49-
public static MutableText chatWithMetrics(String metricsText) {
50-
return Text.literal(CHAT_PREFIX + " ").append(Text.literal(metricsText));
51+
public static MutableText chatWithMetrics(MutableText metricsText) {
52+
return Text.literal("").append(PREFIX_TEXT).append(metricsText);
5153
}
52-
53-
public static String colorValue(String value) {
54-
return AQUA + value + RESET;
54+
55+
public static MutableText colorValue(String value) {
56+
return Text.literal(value).withColor(0x75B5FF).formatted(Formatting.BOLD);
5557
}
56-
57-
public static String colorLabel(String label) {
58-
return GRAY + label + RESET;
58+
59+
public static MutableText colorLabel(String label) {
60+
return Text.literal(label).withColor(getColorValue(Formatting.GRAY));
5961
}
6062
}

0 commit comments

Comments
 (0)