Compare commits

...

2 Commits

Author SHA1 Message Date
Martin Prokoph
3db1c1eb23 fix: crash on servers
- This makes overriding the screen less flexible, but out of all options, this was the best way to do it. Previously, the `@EnvType` annotation took care of keeping this out of the server code, but NeoForge decided to break perfectly functional behaviour once again, requiring me to remove the annotations 🫠
2025-10-03 14:55:40 +02:00
Martin Prokoph
c07c466398 fix: make tooltip translation key respect custom entry names 2025-10-02 16:52:30 +02:00
6 changed files with 34 additions and 16 deletions

View File

@@ -16,12 +16,12 @@ public class EntryInfo {
public MidnightConfig.Condition[] conditions;
public final Field field;
public final Class<?> dataType;
public final String modid, fieldName;
public final String modid, fieldName, translationKey;
int listIndex;
Object defaultValue, value, function;
String tempValue; // The value visible in the config screen
boolean inLimits = true;
Text name, error;
Text error;
ClickableWidget actionButton; // color picker button / explorer button
Tab tab;
boolean conditionsMet = true;
@@ -41,9 +41,10 @@ public class EntryInfo {
}
if (entry != null && !entry.name().isEmpty())
this.name = Text.translatable(entry.name());
this.translationKey = entry.name();
else if (comment != null && !comment.name().isEmpty())
this.name = Text.translatable(comment.name());
this.translationKey = comment.name();
else this.translationKey = modid + ".midnightconfig." + fieldName;
}
public void setValue(Object value) {
@@ -97,7 +98,7 @@ public class EntryInfo {
}
public Tooltip getTooltip(boolean isButton) {
String key = this.modid + ".midnightconfig." + this.fieldName + (!isButton ? ".label" : "") + ".tooltip";
String key = translationKey + (!isButton ? ".label" : "") + ".tooltip";
return Tooltip.of(isButton && this.error != null ? this.error : I18n.hasTranslation(key) ? Text.translatable(key) : Text.empty());
}
}

View File

@@ -37,7 +37,7 @@ public abstract class MidnightConfig {
private static final Pattern DECIMAL_ONLY = Pattern.compile("-?(\\d+\\.?\\d*|\\d*\\.?\\d+|\\.)");
private static final Pattern HEXADECIMAL_ONLY = Pattern.compile("(-?[#0-9a-fA-F]*)");
private static final Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.TRANSIENT).excludeFieldsWithModifiers(Modifier.PRIVATE)
.excludeFieldsWithModifiers(Modifier.TRANSIENT).excludeFieldsWithModifiers(Modifier.PRIVATE).excludeFieldsWithModifiers(Modifier.FINAL)
.addSerializationExclusionStrategy(new ExclusionStrategy() {
public boolean shouldSkipClass(Class<?> clazz) { return false; }
public boolean shouldSkipField(FieldAttributes fieldAttributes) { return fieldAttributes.getAnnotation(Entry.class) == null; }
@@ -70,13 +70,12 @@ public abstract class MidnightConfig {
MidnightConfig instance = createInstance(modid, config);
for (Field field : config.getFields()) {
EntryInfo info = new EntryInfo(field, modid);
//noinspection ConstantValue
if ((field.isAnnotationPresent(Entry.class) || field.isAnnotationPresent(Comment.class))
&& !field.isAnnotationPresent(Server.class)
&& !field.isAnnotationPresent(Hidden.class)
&& PlatformFunctions.isClientEnv())
instance.addClientEntry(field, info);
instance.addClientEntry(field, new EntryInfo(field, modid));
}
instance.loadValuesFromJson();
}
@@ -212,10 +211,10 @@ public abstract class MidnightConfig {
public void onTabInit(String tabName, MidnightConfigListWidget list, MidnightConfigScreen screen) {
}
public static Screen getScreen(Screen parent, String modid) {
public static MidnightConfigScreen getScreen(Screen parent, String modid) {
return configInstances.get(modid).getScreen(parent);
}
public Screen getScreen(Screen parent) {
public MidnightConfigScreen getScreen(Screen parent) {
return new MidnightConfigScreen(parent, modid);
}

View File

@@ -75,7 +75,7 @@ public class MidnightConfigScreen extends Screen {
}
scrollProgress = list.getScrollY();
for (EntryInfo info : MidnightConfig.entries.values())
info.updateFieldValue();
if (Objects.equals(modid, info.modid)) info.updateFieldValue();
updateButtons();
if (instance.reloadScreen) {
updateList();
@@ -154,7 +154,6 @@ public class MidnightConfigScreen extends Screen {
if (!visibleButLocked) continue;
}
if (info.modid.equals(modid) && (info.tab == null || info.tab == tabManager.getCurrentTab())) {
Text name = Objects.requireNonNullElseGet(info.name, () -> Text.translatable(translationPrefix + info.fieldName));
TextIconButtonWidget resetButton = TextIconButtonWidget.builder(Text.translatable("controls.reset"), (button -> {
info.value = info.defaultValue;
info.listIndex = 0;
@@ -245,8 +244,8 @@ public class MidnightConfigScreen extends Screen {
widgets.add(cycleButton);
}
if (!info.conditionsMet) widgets.forEach(w -> w.active = false);
this.list.addButton(widgets, name, info);
} else this.list.addButton(List.of(), name, info);
this.list.addButton(widgets, Text.translatable(info.translationKey), info);
} else this.list.addButton(List.of(), Text.translatable(info.translationKey), info);
}
list.setScrollY(scrollProgress);
updateButtons();

View File

@@ -31,6 +31,11 @@ loom {
configName = "Test Minecraft Client"
source sourceSets.test
}
testServer {
server()
configName = "Test Minecraft Server"
source sourceSets.test
}
}
}

View File

@@ -6,7 +6,7 @@ yarn_mappings=1.21.9+build.1
enabled_platforms=fabric,neoforge
archives_base_name=midnightlib
mod_version=1.8.1
mod_version=1.8.3
maven_group=eu.midnightdust
release_type=release
curseforge_id=488090
@@ -15,7 +15,7 @@ modrinth_id=codAaoxh
fabric_loader_version=0.17.2
fabric_api_version=0.133.14+1.21.9
neoforge_version=21.9.3-beta
neoforge_version=21.9.9-beta
yarn_mappings_patch_neoforge_version = 1.21+build.4
mod_menu_version = 9.0.0

View File

@@ -39,6 +39,20 @@ loom {
client()
name = "Test Minecraft Client"
mods {
create('midnightlib') {
sourceSet sourceSets.main
}
create('modid') { // test mod
sourceSet sourceSets.test
}
}
source sourceSets.test
}
testServer {
server()
name = "Test Minecraft Server"
mods {
create('midnightlib') {
sourceSet sourceSets.main