Skip to content

Commit

Permalink
Fix forge config being temperamental
Browse files Browse the repository at this point in the history
  • Loading branch information
quat1024 committed Oct 24, 2023
1 parent b9640cd commit ba4050c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
18 changes: 15 additions & 3 deletions src/main/java/vazkii/zeta/config/ValueDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
import java.util.List;
import java.util.function.Predicate;

import org.jetbrains.annotations.Nullable;

//TODO: maybe we need "boolean equals(T thing1, T thing2)"
public class ValueDefinition<T> extends Definition {
public final T defaultValue;

public final Predicate<Object> validator;
public final @Nullable Predicate<Object> validator;

public ValueDefinition(String name, List<String> comment, T defaultValue, Predicate<Object> validator) {
public ValueDefinition(String name, List<String> comment, T defaultValue, @Nullable Predicate<Object> validator) {
super(name, comment);
this.defaultValue = defaultValue;
this.validator = validator;
}

public ValueDefinition(String name, List<String> comment, T defaultValue) {
this(name, comment, defaultValue, __ -> true);
this(name, comment, defaultValue, null);
}

public boolean validate(Object underTest) {
//you HAVE to start with this condition lest forge's config api explode into a million pieces
if(underTest == null || !defaultValue.getClass().isAssignableFrom(underTest.getClass()))
return false;
else if(validator == null)
return true;
else
return validator.test(underTest);
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/vazkii/zeta/config/WeirdConfigSingleton.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ public WeirdConfigSingleton(Zeta z, Object rootPojo) {

for(ZetaCategory category : modules.getInhabitedCategories()) {
//category enablement option
//TODO: why aren't these showing up?
ValueDefinition<Boolean> categoryEnabled = rootConfig.getOrCreateSubsection("categories", List.of("cateogyr enabled :O")).addValue(category.name, List.of("is it enabed"), true);
ValueDefinition<Boolean> categoryEnabled = rootConfig.getOrCreateSubsection("categories", List.of()).addValue(category.name, List.of(), true);
categoryEnabledOptions.put(category, categoryEnabled);
fieldUpdaters.add(i -> setCategoryEnabled(category, i.get(categoryEnabled)));

Expand Down
14 changes: 2 additions & 12 deletions src/main/java/vazkii/zetaimplforge/config/ForgeBackedConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;

import net.minecraftforge.common.ForgeConfigSpec;
import vazkii.quark.base.Quark;
import vazkii.zeta.config.Definition;
import vazkii.zeta.config.IZetaConfigInternals;
import vazkii.zeta.config.SectionDefinition;
Expand Down Expand Up @@ -36,21 +34,13 @@ private void walkSection(SectionDefinition sect, ForgeConfigSpec.Builder builder
}

private <T> void addValue(ValueDefinition<T> val, ForgeConfigSpec.Builder builder) {
Predicate<Object> validator = obj -> {
try {
return val.validator.test(obj);
} catch (Exception e) {
return false;
}
};

builder.comment(val.commentToArray());

ForgeConfigSpec.ConfigValue<?> forge;
if(val.defaultValue instanceof List<?> list)
forge = builder.defineList(val.name, list, validator);
forge = builder.defineList(val.name, list, val::validate);
else
forge = builder.define(val.name, val.defaultValue, validator);
forge = builder.define(List.of(val.name), () -> val.defaultValue, val::validate, val.defaultValue.getClass()); //forge is weird

definitionsToValues.put(val, forge);
}
Expand Down

0 comments on commit ba4050c

Please sign in to comment.