Skip to content

Commit

Permalink
fix: Fix Set and ResourceLocation configs in Forge and NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 28, 2023
1 parent d029eaa commit 0dc9230
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.blay09.mods.balm.api.event.ConfigReloadedEvent;
import net.blay09.mods.balm.api.network.ConfigReflection;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModLoadingContext;
Expand Down Expand Up @@ -56,7 +57,9 @@ private void buildConfigSpec(String parentPath, ForgeConfigSpec.Builder builder,

if (String.class.isAssignableFrom(type)) {
builder.define(path, (String) defaultValue);
} else if (Collection.class.isAssignableFrom(type)) {
} else if (ResourceLocation.class.isAssignableFrom(type)) {
builder.define(path, ((ResourceLocation) defaultValue).toString());
} else if (List.class.isAssignableFrom(type)) {
ExpectedType expectedType = field.getAnnotation(ExpectedType.class);
if (expectedType == null) {
logger.warn("Config field without expected type, will not validate list content ({} in {})", field.getName(), clazz.getName());
Expand All @@ -66,6 +69,16 @@ private void buildConfigSpec(String parentPath, ForgeConfigSpec.Builder builder,
() -> ((List<?>) defaultValue),
it -> expectedType == null || expectedType.value().isAssignableFrom(it.getClass()) || (expectedType.value().isEnum() && Arrays.stream(
expectedType.value().getEnumConstants()).anyMatch(constant -> constant.toString().equals(it))));
} else if (Set.class.isAssignableFrom(type)) {
ExpectedType expectedType = field.getAnnotation(ExpectedType.class);
if (expectedType == null) {
logger.warn("Config field without expected type, will not validate list content ({} in {})", field.getName(), clazz.getName());
}

builder.defineListAllowEmpty(Arrays.asList(path.split("\\.")),
() -> (((Set<?>) defaultValue).stream().toList()),
it -> expectedType == null || expectedType.value().isAssignableFrom(it.getClass()) || (expectedType.value().isEnum() && Arrays.stream(
expectedType.value().getEnumConstants()).anyMatch(constant -> constant.toString().equals(it))));
} else if (Enum.class.isAssignableFrom(type)) {
builder.defineEnum(path, (Enum) defaultValue);
} else if (int.class.isAssignableFrom(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import net.blay09.mods.balm.api.config.ExpectedType;
import net.blay09.mods.balm.api.event.ConfigReloadedEvent;
import net.blay09.mods.balm.api.network.ConfigReflection;
import net.minecraft.resources.ResourceLocation;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.fml.config.IConfigSpec;
import net.neoforged.fml.config.ModConfig;
Expand Down Expand Up @@ -53,7 +54,9 @@ private void buildConfigSpec(String parentPath, ModConfigSpec.Builder builder, C

if (String.class.isAssignableFrom(type)) {
builder.define(path, (String) defaultValue);
} else if (Collection.class.isAssignableFrom(type)) {
} else if (ResourceLocation.class.isAssignableFrom(type)) {
builder.define(path, ((ResourceLocation) defaultValue).toString());
} else if (List.class.isAssignableFrom(type)) {
ExpectedType expectedType = field.getAnnotation(ExpectedType.class);
if (expectedType == null) {
logger.warn("Config field without expected type, will not validate list content ({} in {})", field.getName(), clazz.getName());
Expand All @@ -63,6 +66,16 @@ private void buildConfigSpec(String parentPath, ModConfigSpec.Builder builder, C
() -> ((List<?>) defaultValue),
it -> expectedType == null || expectedType.value().isAssignableFrom(it.getClass()) || (expectedType.value().isEnum() && Arrays.stream(
expectedType.value().getEnumConstants()).anyMatch(constant -> constant.toString().equals(it))));
} else if (Set.class.isAssignableFrom(type)) {
ExpectedType expectedType = field.getAnnotation(ExpectedType.class);
if (expectedType == null) {
logger.warn("Config field without expected type, will not validate list content ({} in {})", field.getName(), clazz.getName());
}

builder.defineListAllowEmpty(Arrays.asList(path.split("\\.")),
() -> (((Set<?>) defaultValue).stream().toList()),
it -> expectedType == null || expectedType.value().isAssignableFrom(it.getClass()) || (expectedType.value().isEnum() && Arrays.stream(
expectedType.value().getEnumConstants()).anyMatch(constant -> constant.toString().equals(it))));
} else if (Enum.class.isAssignableFrom(type)) {
builder.defineEnum(path, (Enum) defaultValue);
} else if (int.class.isAssignableFrom(type)) {
Expand Down

0 comments on commit 0dc9230

Please sign in to comment.