Skip to content

Commit 5d70257

Browse files
committed
Check for non-root translation keys
1 parent 0f532ca commit 5d70257

8 files changed

Lines changed: 155 additions & 52 deletions

File tree

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.6.7
4+
5+
- Added detection for non-root translation keys
6+
37
## 2.6.6
48

59
- Fixed format code translation not resetting format on color change

common/src/main/java/dev/terminalmc/chatnotify/gui/widget/list/root/notif/trigger/TriggerEditorList.java

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ public class TriggerEditorList extends OptionList {
5757
private boolean restyle;
5858
private MultiLineTextField textDisplayField;
5959
private String displayText = "";
60-
private TextField keyDisplayField;
61-
private String displayKey = "";
60+
private MultiLineTextField keyDisplayField;
61+
private String displayKeys = "";
6262

6363
public TriggerEditorList(
6464
Minecraft mc,
@@ -116,13 +116,17 @@ protected void addEntries() {
116116
));
117117

118118
// Key display field
119-
keyDisplayField = new TextField(dynWideEntryX, 0, dynWideEntryWidth, entryHeight);
120-
keyDisplayField.setMaxLength(256);
121-
keyDisplayField.setValue(displayKey);
122-
addEntry(new Entry.DisplayField(
119+
keyDisplayField = new MultiLineTextField(
123120
dynWideEntryX,
121+
0,
124122
dynWideEntryWidth,
125-
entryHeight,
123+
entryHeight
124+
);
125+
keyDisplayField.setValue(displayKeys);
126+
addSpacedEntry(new Entry.DisplayField(
127+
dynWideEntryX,
128+
dynWideEntryWidth,
129+
entryHeight + itemHeight,
126130
keyDisplayField,
127131
localized("option", "notif.trigger.editor.display.key")
128132
));
@@ -141,9 +145,9 @@ private void setTextDisplayValue(String text) {
141145
textDisplayField.setValue(displayText);
142146
}
143147

144-
private void setKeyDisplayValue(String key) {
145-
displayKey = key;
146-
keyDisplayField.setValue(displayKey);
148+
private void setKeyDisplayValue(String keys) {
149+
displayKeys = keys;
150+
keyDisplayField.setValue(displayKeys);
147151
}
148152

149153
// Chat message list
@@ -157,6 +161,7 @@ private void addChatMessages(List<Component> recentChat) {
157161
for (Component msg : recentChat) {
158162
Component restyledMsg = msg.copy();
159163
Matcher matcher = null;
164+
Component keyMatch = null;
160165
String msgStr = FormatUtil.stripCodes(msg.getString());
161166
boolean hit = switch (trigger.type) {
162167
case NORMAL -> {
@@ -171,7 +176,10 @@ private void addChatMessages(List<Component> recentChat) {
171176
yield false;
172177
}
173178
}
174-
case KEY -> MessageUtil.keySearch(msg, trigger.string);
179+
case KEY -> {
180+
keyMatch = MessageUtil.keySearch(msg, trigger.string);
181+
yield keyMatch != null;
182+
}
175183
};
176184
if (filter && !hit)
177185
continue;
@@ -184,8 +192,15 @@ else if (restyle && hit) {
184192
trigger.styleTarget.tryParseIndexes();
185193
}
186194
}
187-
restyledMsg =
188-
StyleUtil.restyle(msg, msgStr, trigger, matcher, textStyle, restyleAll);
195+
restyledMsg = StyleUtil.restyle(
196+
msg,
197+
msgStr,
198+
trigger,
199+
matcher,
200+
keyMatch,
201+
textStyle,
202+
restyleAll
203+
);
189204
}
190205
displayChat.add(new Pair<>(msg, restyledMsg));
191206
}
@@ -271,10 +286,10 @@ private static class TriggerOptions extends Entry {
271286
triggerField.regexValidator();
272287
triggerField.setValueListener((str) -> {
273288
trigger.string = str.strip();
274-
if (list.children().size() > 4) {
289+
if (list.children().size() > 5) {
275290
list.children().removeIf((entry) -> entry instanceof MessageEntry
276291
|| entry instanceof Text
277-
|| (entry instanceof Space && list.children().indexOf(entry) > 4));
292+
|| (entry instanceof Space && list.children().indexOf(entry) > 5));
278293
list.addChatMessages(list.recentChat);
279294
}
280295
});
@@ -503,12 +518,26 @@ private static class MessageEntry extends Entry {
503518
@Override
504519
public boolean mouseClicked(double mouseX, double mouseY, int button) {
505520
list.setTextDisplayValue(FormatUtil.stripCodes(msg.getString()));
506-
list.setKeyDisplayValue(msg.getContents() instanceof TranslatableContents tc
507-
? tc.getKey()
508-
: localized("option", "notif.trigger.editor.display.key.none").getString());
521+
522+
List<String> keys = new ArrayList<>();
523+
getKeys(msg, keys);
524+
list.setKeyDisplayValue(keys.isEmpty()
525+
? localized("option", "notif.trigger.editor.display.key.none").getString()
526+
: String.join("\n", keys));
527+
509528
list.setScrollAmount(0);
510529
return true;
511530
}
531+
532+
private static void getKeys(Component msg, List<String> keys) {
533+
if (msg.getContents() instanceof TranslatableContents tc) {
534+
keys.add(tc.getKey());
535+
}
536+
537+
for (Component sibling : msg.getSiblings()) {
538+
getKeys(sibling, keys);
539+
}
540+
}
512541
}
513542
}
514543
}

common/src/main/java/dev/terminalmc/chatnotify/util/text/FormatUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ private static MutableComponent convertToLiteral(MutableComponent text)
156156
// Indexed placeholder
157157
int argIdx = Integer.parseInt(m.group(1));
158158
if (argIdx < 1 || argIdx > originalArgs.length) {
159-
ChatNotify.LOG.warn("{}",
159+
ChatNotify.LOG.warn(
160160
"Translation specifies arg number {} out of range for length {}",
161161
argIdx,
162162
originalArgs.length

common/src/main/java/dev/terminalmc/chatnotify/util/text/MessageUtil.java

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private MessageUtil() {
7474
// Save message for trigger editor
7575
if (ChatNotify.unmodifiedChat.size() > 30)
7676
ChatNotify.unmodifiedChat.poll();
77-
ChatNotify.unmodifiedChat.add(msg);
77+
ChatNotify.unmodifiedChat.add(msg.copy());
7878

7979
if (debug) {
8080
ChatNotify.LOG.warn("Processing new message");
@@ -173,7 +173,7 @@ private static String checkOwner(String cleanStr) {
173173
}
174174
if (recentStart != -1) {
175175
if (debug)
176-
ChatNotify.LOG.warn("{}",
176+
ChatNotify.LOG.warn(
177177
"Matched recent message '{}' at index {}",
178178
recentMessages.get(i).getSecond(),
179179
recentStart
@@ -184,7 +184,7 @@ private static String checkOwner(String cleanStr) {
184184
Matcher triggerMatcher = normalSearch(prefix, t.string);
185185
if (triggerMatcher.find()) {
186186
if (debug)
187-
ChatNotify.LOG.warn("{}",
187+
ChatNotify.LOG.warn(
188188
"Matched trigger '{}' at index {}",
189189
t.string,
190190
triggerMatcher.start()
@@ -244,6 +244,7 @@ private static String checkOwner(String cleanStr) {
244244
if (trig.string.isBlank())
245245
continue;
246246
Matcher matcher = null;
247+
Component keyMatch = null;
247248
boolean hit = switch (trig.type) {
248249
case NORMAL -> {
249250
if (normalSearch(cleanOwnedStr, trig.string).find()) {
@@ -258,7 +259,10 @@ private static String checkOwner(String cleanStr) {
258259
matcher = trig.pattern.matcher(cleanStr);
259260
yield matcher.find();
260261
}
261-
case KEY -> keySearch(msg, trig.string);
262+
case KEY -> {
263+
keyMatch = keySearch(msg, trig.string);
264+
yield keyMatch != null;
265+
}
262266
};
263267
if (!hit)
264268
continue;
@@ -273,7 +277,7 @@ private static String checkOwner(String cleanStr) {
273277
case NORMAL -> normalSearch(cleanOwnedStr, inTrig.string).find();
274278
case REGEX -> inTrig.pattern == null
275279
|| inTrig.pattern.matcher(cleanStr).find();
276-
case KEY -> keySearch(msg, inTrig.string);
280+
case KEY -> keySearch(msg, inTrig.string) != null;
277281
});
278282
if (inMiss)
279283
break;
@@ -292,7 +296,7 @@ private static String checkOwner(String cleanStr) {
292296
case NORMAL -> normalSearch(cleanOwnedStr, exTrig.string).find();
293297
case REGEX -> exTrig.pattern != null
294298
&& exTrig.pattern.matcher(cleanStr).find();
295-
case KEY -> keySearch(msg, exTrig.string);
299+
case KEY -> keySearch(msg, exTrig.string) != null;
296300
};
297301
if (exHit)
298302
break;
@@ -317,7 +321,15 @@ private static String checkOwner(String cleanStr) {
317321
sendResponses(notif, subsMatcher);
318322

319323
// Restyle
320-
msg = StyleUtil.restyle(msg, cleanStr, trig, matcher, notif.textStyle, restyleAll);
324+
msg = StyleUtil.restyle(
325+
msg,
326+
cleanStr,
327+
trig,
328+
matcher,
329+
keyMatch,
330+
notif.textStyle,
331+
restyleAll
332+
);
321333

322334
// Send custom messages, after restyle in case of forwarding
323335
// the entire message. Reset match by subsMatcher.find(0)
@@ -374,13 +386,19 @@ private static String checkOwner(String cleanStr) {
374386
* @param key the key (or partial key) to search for.
375387
* @return {@code true} if the key matches the message, {@code false} otherwise.
376388
*/
377-
public static boolean keySearch(Component msg, String key) {
389+
public static @Nullable Component keySearch(Component msg, String key) {
378390
if (key.equals(".")) {
379-
return true;
380-
} else if (msg.getContents() instanceof TranslatableContents tc) {
381-
return tc.getKey().contains(key);
391+
return msg;
392+
} else if (msg.getContents() instanceof TranslatableContents tc && tc.getKey().contains(key)) {
393+
return msg;
394+
} else {
395+
for (Component sibling : msg.getSiblings()) {
396+
Component keyMatch = keySearch(sibling, key);
397+
if (keyMatch != null)
398+
return keyMatch;
399+
}
382400
}
383-
return false;
401+
return null;
384402
}
385403

386404
/**

0 commit comments

Comments
 (0)