Skip to content

Commit

Permalink
Fix ConfigHelper#defineObject not working when the default value was …
Browse files Browse the repository at this point in the history
…an empty list
  • Loading branch information
Commoble committed Sep 2, 2022
1 parent ed9181f commit 7bb92ae
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 3.0.0.5
* Fix ConfigHelper#defineObject not working when the default value was an empty list

# 3.0.0.4
* Fix sublibrary dependencies not working in dev environments

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

modid = databuddy
mod_version = 3.0.0.4
mc_version = 1.19
forge_version = 41.0.98
mod_version = 3.0.0.5
mc_version = 1.19.2
forge_version = 43.1.0
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import commoble.databuddy.config.ConfigHelper;
import commoble.databuddy.config.ConfigHelper.ConfigObject;
import commoble.databuddy.examplecontent.ExampleConfig.TestObject;
import net.minecraft.core.BlockPos;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.ForgeConfigSpec;
Expand All @@ -16,7 +17,8 @@
public record ExampleConfig(
ConfigValue<Integer> bones,
ConfigValue<Double> bananas,
ConfigObject<TestObject> testObject)
ConfigObject<TestObject> testObject,
ConfigObject<List<Long>> list)
{
public static ExampleConfig create(ForgeConfigSpec.Builder builder)
{
Expand All @@ -42,9 +44,12 @@ public static ExampleConfig create(ForgeConfigSpec.Builder builder)
new ResourceLocation("minecraft:iron")),
true));

builder.comment("Empty list");
ConfigObject<List<Long>> list = ConfigHelper.defineObject(builder, "list", Codec.LONG.listOf(), List.of());

builder.pop();

return new ExampleConfig(bones, bananas, testObject);
return new ExampleConfig(bones, bananas, testObject, list);
}

public static record TestObject(BlockPos pos, List<ResourceLocation> ids, boolean bool)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/commoble/databuddy/config/ConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
import java.time.temporal.Temporal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
Expand Down Expand Up @@ -291,6 +292,17 @@ public Object createString(String value)
{
return value;
}

@Override
public DataResult<Object> mergeToList(Object list, List<Object> values)
{
// default mergeToList returns the null object if list is empty;
// toml doesn't support null values so we need to convert to an empty list
return DynamicOps.super.mergeToList(list, values)
.map(obj -> obj == this.empty()
? new ArrayList<>()
: obj);
}

@Override
public DataResult<Object> mergeToList(Object list, Object value)
Expand Down

0 comments on commit 7bb92ae

Please sign in to comment.