Skip to content

Commit

Permalink
Bug Fixes
Browse files Browse the repository at this point in the history
- Fixed a bug with loading the config
- Fixed a crash with EnumWidget
- Fixed a bug with the reset button states
- Fixed a bug with NumberEntry default value
- Fixed a bug with SliderNumberWidget
  • Loading branch information
MehradN committed Jun 30, 2023
1 parent 8b093c7 commit 2e455ca
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,7 @@ public MehradConfigScreen buildForInstance(MehradConfig instance) {
try {
config.load();
} catch (IOException e) {
MehradConfigEntrypoint.LOGGER.error("Failed to load the config for \"" + config.modId + "\"!", e);
return null;
MehradConfigEntrypoint.LOGGER.warn("Failed to load the config for \"" + config.modId + "\"!", e);
}

ScreenProperties properties = buildProperties(wrapOnSave(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void init() {
.bounds(x, y, 50, 20)
.tooltip(Tooltip.create(Component.translatable("mehrad-config.resetButton.tooltip")))
.build());
resetButton.active = !widget.entry.isDefault();
widget.onValueChange((entry) -> resetButton.active = !entry.isDefault());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ protected void updateWidgetNarration(NarrationElementOutput narrationElementOutp
private T next(T value) {
T[] values = this.enumClass.getEnumConstants();
int i = value.ordinal() + 1;
return (i > values.length ? values[0] : values[i]);
return (i >= values.length ? values[0] : values[i]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public SliderNumberWidget(int x, int y, int width, int height,
((Widget)this.widget).setReport(this::reportValueChange);
}

@Override
public void reportValueChange() {
super.reportValueChange();
((Widget)this.widget).updateSlider();
}

private static class Widget extends AbstractSliderButton {
private final int min;
private final int max;
Expand All @@ -41,6 +47,10 @@ public void setReport(Runnable report) {
this.report = report;
}

public void updateSlider() {
this.value = valueToSlider(this.min, this.max, this.entry.get());
}

@Override
protected void updateMessage() {
this.report.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ public abstract class DefaultValueEntry <T> implements ConfigEntry<T> {

/**
* @param name the name of the entry
* @param defaultValue the default value of the entry.
* @param defaultValue the default value of the entry, it is expected to be pre-trimmed
*/
protected DefaultValueEntry(String name, @NotNull T defaultValue) {
this.name = name;
this.defaultValue = trim(defaultValue);
this.defaultValue = defaultValue;
this.value = this.defaultValue;
}

/**
* Creates an optional entry from this config entry.
*
* @param fallbackEntry the config entry that should provide the default value
* @param fallbackEntry the config entry that should provide the default value, avoid using a config entry from the same config
* @return an optional entry
* @see OptionalEntry
*/
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/ir/mehradn/mehradconfig/entry/NumberEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class NumberEntry extends DefaultValueEntry<Integer> {
* @param defaultValue the default value of the entry.
*/
public NumberEntry(String name, int min, int max, int defaultValue) {
super(name, defaultValue);
super(name, trim(defaultValue, min, max));
if (max < min)
throw new IllegalArgumentException("Min cannot be more than max!");
this.min = min;
Expand Down Expand Up @@ -64,7 +64,11 @@ public NumberTypeInfo entryTypeInfo() {

@Override
protected Integer trim(Integer value) {
return Mth.clamp(value, this.min, this.max);
return trim(value, this.min, this.max);
}

private static int trim(int value, int min, int max) {
return Mth.clamp(value, min, max);
}

public record NumberTypeInfo(int min, int max) implements EntryTypeInfo<Integer> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class OptionalEntry <T> implements ConfigEntry<T> {
* <b>Note:</b> Always at the initial state after the constructor, the value of the optional entry is ignored.
*
* @param optionalEntry the config entry that should hold the optional value
* @param fallbackEntry the config entry that should provide the default value
* @param fallbackEntry the config entry that should provide the default value, avoid using a config entry from the same config
*/
public OptionalEntry(ConfigEntry<T> optionalEntry, ConfigEntry<T> fallbackEntry) {
this.optionalEntry = optionalEntry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ public class MehradConfigTest implements ModInitializer {
@Override
public void onInitialize() {
ConfigScreenBuilder.DefaultScreens defaultScreen =
ConfigScreenBuilder.DefaultScreens.RESETTABLE; // Change to compact if you don't need reset buttons
ConfigScreenBuilder.DefaultScreens.RESETTABLE;
//ConfigScreenBuilder.DefaultScreens.COMPACT;
ConfigScreenBuilder builder = new ConfigScreenBuilder()
.setScreenType(defaultScreen)
//.setWidgetWidth(175)
//.setButtonWidth(175)
//.setDescriptionY((width, height, font) -> height / 2)
//.setDescriptionWidth(500)
//.setOnSave((minecraft, thisScreen, parentScreen) -> LOGGER.info("SAVED!"))
//.setOnCancel((minecraft, thisScreen, parentScreen) -> LOGGER.info("CANCELED!"))
//.setOnSave((minecraft, thisScreen, parentScreen) -> { LOGGER.info("SAVED!"); minecraft.setScreen(parentScreen); })
//.setOnCancel((minecraft, thisScreen, parentScreen) -> { LOGGER.info("CANCELED!"); minecraft.setScreen(parentScreen); })
;
ModMenuEntrypoint.register(MOD_ID, TestConfig::new, builder);
}
Expand Down

0 comments on commit 2e455ca

Please sign in to comment.