- fix bug: avoid the crash due to Identifier syntax not legitimate.

- new feature: multiple expect values for `@Condition`
- change: use `net.minecraft.client.gui.widget.CheckboxWidget` for boolean `@Entry` instead of `net.minecraft.client.gui.widget.ButtonWidget`
- misc: cleanup for `en_us.json` and example code & bump version
This commit is contained in:
Jaffe2718
2025-04-13 18:32:35 +08:00
parent 57d881db1e
commit 1606646e87
4 changed files with 24 additions and 25 deletions

View File

@@ -97,7 +97,7 @@ public abstract class MidnightConfig {
this.conditionsMet = false;
String requiredOption = condition.requiredOption().contains(":") ? condition.requiredOption() : (this.modid + ":" + condition.requiredOption());
if (entries.get(requiredOption) instanceof EntryInfo info)
this.conditionsMet &= condition.requiredValue().equals(info.tempValue);
this.conditionsMet &= List.of(condition.requiredValue()).contains(info.tempValue);
if (!this.conditionsMet) break;
}
if (prevConditionState != this.conditionsMet) reloadScreen = true;
@@ -162,10 +162,7 @@ public abstract class MidnightConfig {
else if (info.dataType == double.class) textField(info, Double::parseDouble, DECIMAL_ONLY, e.min(), e.max(), false);
else if (info.dataType == String.class || info.dataType == Identifier.class) textField(info, String::length, null, Math.min(e.min(), 0), Math.max(e.max(), 1), true);
else if (info.dataType == boolean.class) {
Function<Object, Text> func = value -> Text.translatable((Boolean) value ? "gui.yes" : "gui.no").formatted((Boolean) value ? Formatting.GREEN : Formatting.RED);
info.function = new AbstractMap.SimpleEntry<ButtonWidget.PressAction, Function<Object, Text>>(button -> {
info.setValue(!(Boolean) info.value); button.setMessage(func.apply(info.value));
}, func);
info.function = (CheckboxWidget.Callback) (ch, b) -> info.setValue(b);
} else if (info.dataType.isEnum()) {
List<?> values = Arrays.asList(field.getType().getEnumConstants());
Function<Object, Text> func = value -> {
@@ -215,7 +212,11 @@ public abstract class MidnightConfig {
b.active = entries.values().stream().allMatch(e -> e.inLimits);
if (inLimits) {
if (info.dataType == Identifier.class) info.setValue(Identifier.tryParse(s));
if (info.dataType == Identifier.class) { // avoid the crash due to Identifier syntax not legitimate
Identifier id = Identifier.tryParse(s);
if (id == null) return false;
info.setValue(Identifier.tryParse(s));
}
else info.setValue(isNumber ? value : s);
}
@@ -296,7 +297,7 @@ public abstract class MidnightConfig {
if (entry.buttons.get(0) instanceof ClickableWidget widget)
if (widget.isFocused() || widget.isHovered()) widget.setTooltip(getTooltip(entry.info, true));
if (entry.buttons.get(1) instanceof ButtonWidget button)
button.active = !Objects.equals(entry.info.value.toString(), entry.info.defaultValue.toString());
button.active = !Objects.equals(String.valueOf(entry.info.value), String.valueOf(entry.info.defaultValue));
}}}}
@Override
public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
@@ -354,17 +355,16 @@ public abstract class MidnightConfig {
if (info.function != null) {
ClickableWidget widget;
Entry e = info.entry;
if (info.function instanceof Map.Entry) { // Enums & booleans
if (info.dataType == boolean.class) {
widget = CheckboxWidget.builder(Text.empty(), textRenderer).pos(width - 185, 0).callback((CheckboxWidget.Callback) info.function).checked((boolean) info.value).build();
} else if (info.dataType.isEnum()) {
var values = (Map.Entry<ButtonWidget.PressAction, Function<Object, Text>>) info.function;
if (info.dataType.isEnum())
values.setValue(value -> Text.translatable(translationPrefix + "enum." + info.dataType.getSimpleName() + "." + info.value.toString()));
values.setValue(value -> Text.translatable(translationPrefix + "enum." + info.dataType.getSimpleName() + "." + info.value.toString()));
widget = ButtonWidget.builder(values.getValue().apply(info.value), values.getKey()).dimensions(width - 185, 0, 150, 20).tooltip(getTooltip(info, true)).build();
}
else if (e.isSlider())
} else if (e.isSlider())
widget = new MidnightSliderWidget(width - 185, 0, 150, 20, Text.of(info.tempValue), (Double.parseDouble(info.tempValue) - e.min()) / (e.max() - e.min()), info);
else widget = new TextFieldWidget(textRenderer, width - 185, 0, 150, 20, Text.empty());
else
widget = new TextFieldWidget(textRenderer, width - 185, 0, 150, 20, Text.empty());
if (widget instanceof TextFieldWidget textField) {
textField.setMaxLength(e.width()); textField.setText(info.tempValue);
Predicate<String> processor = ((BiFunction<TextFieldWidget, ButtonWidget, Predicate<String>>) info.function).apply(textField, done);
@@ -589,7 +589,7 @@ public abstract class MidnightConfig {
public @interface Condition {
String requiredModId() default "";
String requiredOption() default "";
String requiredValue() default "true";
String[] requiredValue() default {"true"};
boolean visibleButLocked() default false;
}