#default values in Lists [Cloth Config]

1 messages · Page 1 of 1 (latest)

steady moth
#

I am attempting to make a list within cloth Config for default values, however, i can't find a way around these issues:

//this method overwrites previous configs
public List<String> list = Arrays.asList("default");

//this other method adds a new repeated instance of the default value every restart
public List<String> list = new ArrayList<>(Arrays.asList("minecraft:overworld"));

I am currently looking for possible solutions involving filtering out the repeated inputs but am unsure of how to do it.

how can I safely add a Default value to a list using shedaniel's Autoconfig?

EDIT: at this point, i'm just using this post to share what i learn regarding config validations of lists in Cloth config... Both topics have almost no documentation on and are extremely useful configuration tools. If you are here to learn, i hope this post finds you well ;)

NOTE: EVERY CODE LINE ON THIS POST USES THE OFFICIAL MOJANG MAPPINGS

steady moth
#

After studying through other repositories, i found a different but useful solution: how to add a non removable item to the list...

Example:

@Config(name = MOD_ID)
public class ModConfig implements ConfigData {
  //the 'Arrays.asList' segment will add the list to the configs every restart
  public List<String> worldList = new ArrayList<>(Arrays.asList("minecraft:overworld"));

//[...]

  @Override
  public void validatePostLoad() throws ValidationException {
    //this segment validates the list, removing repeated values
    this.common.worldList = this.common.worldList.stream().distinct().collect(Collectors.toList());

    ConfigData.super.validatePostLoad();
  }
}
#

I also found a way of making a default value to make sure lists aren't empty

@Override
public void validatePostLoad() throws ValidationException {
     //if empty, add default value
    if (this.common.worldList.isEmpty()) { this.common.worldList.add("minecraft:overworld"); }
    
    //remove repeated items
    this.common.worldList = this.common.worldList.stream().distinct().collect(Collectors.toList());

    ConfigData.super.validatePostLoad();
}