Skip to content

Commit 0f125a3

Browse files
committed
feat: infer some config strings if translations are not presetn
1 parent 1eebf44 commit 0f125a3

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/main/java/io/github/jamalam360/jamlib/config/JamLibConfig.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@
7373
* Based on <a href="https://github.com/Minenash/TinyConfig">https://github.com/Minenash/TinyConfig</a>
7474
* Credits to Minenash
7575
* <p>
76-
* Adapted by Jamalam for JamLib
76+
* <p>
77+
* Adapted by Jamalam for use in JamLib
7778
*/
7879

7980
@SuppressWarnings({"unchecked", "unused"})
@@ -162,14 +163,23 @@ else if (type == String.class || type == List.class) {
162163
}, func);
163164
} else if (type.isEnum()) {
164165
List<?> values = Arrays.asList(field.getType().getEnumConstants());
165-
Function<Object, Text> func = value -> Text.translatable(modid + ".jamlibconfig." + "enum." + type.getSimpleName() + "." + info.value.toString());
166+
Function<Object, Text> func = (value) -> {
167+
String translationKey = modid + ".jamlibconfig.enum." + type.getSimpleName() + "." + info.value.toString();
168+
169+
if (I18n.hasTranslation(translationKey)) {
170+
return Text.translatable(translationKey);
171+
} else {
172+
return Text.literal(info.value.toString());
173+
}
174+
};
166175
info.widget = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
167176
int index = values.indexOf(info.value) + 1;
168177
info.value = values.get(index >= values.size() ? 0 : index);
169178
button.setMessage(func.apply(info.value));
170179
}, func);
171180
}
172181
}
182+
173183
entries.add(info);
174184
}
175185

@@ -232,7 +242,7 @@ public static Screen getScreen(Screen parent, String modid) {
232242
@Environment(EnvType.CLIENT)
233243
private static class MidnightConfigScreen extends Screen {
234244
protected MidnightConfigScreen(Screen parent, String modid) {
235-
super(Text.translatable(modid + ".jamlibconfig." + "title"));
245+
super(getConfigScreenTitle(modid));
236246
this.parent = parent;
237247
this.modid = modid;
238248
this.translationPrefix = modid + ".jamlibconfig.";
@@ -390,6 +400,15 @@ else if (I18n.hasTranslation(key) && text.equals(name)) {
390400
}
391401
super.render(matrices, mouseX, mouseY, delta);
392402
}
403+
404+
@SuppressWarnings("OptionalGetWithoutIsPresent")
405+
private static Text getConfigScreenTitle(String modid) {
406+
if (I18n.hasTranslation(modid + ".jamlibconfig.title")) {
407+
return Text.translatable(modid + ".jamlibconfig.title");
408+
} else {
409+
return Text.literal(FabricLoader.getInstance().getModContainer(modid).get().getMetadata().getName());
410+
}
411+
}
393412
}
394413

395414
@Environment(EnvType.CLIENT)

0 commit comments

Comments
 (0)