From d029eaa94f28ff2cec97a78e4ce8e88bfe892a5c Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Wed, 27 Dec 2023 14:26:20 +0100 Subject: [PATCH] fix: Fix fabric config saving of ResourceLocation and Set types --- .../config/notoml/NotomlSerializer.java | 48 +++++++++++-------- .../mods/balm/config/ExampleConfigData.java | 4 ++ 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/config/notoml/NotomlSerializer.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/config/notoml/NotomlSerializer.java index 7db2c30c..768bcd8a 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/config/notoml/NotomlSerializer.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/config/notoml/NotomlSerializer.java @@ -1,8 +1,10 @@ package net.blay09.mods.balm.fabric.config.notoml; +import net.minecraft.resources.ResourceLocation; + import java.io.IOException; import java.io.Writer; -import java.util.List; +import java.util.Collection; import java.util.Map; public class NotomlSerializer { @@ -44,7 +46,9 @@ private static String serializeToString(Notoml notoml) { } else { sb.append("\"").append(stringValue.replace("\"", "\\\"")).append("\""); } - } else if (value instanceof List listValue) { + } else if (value instanceof ResourceLocation resourceLocationValue) { + sb.append("\"").append(resourceLocationValue).append("\""); + } else if (value instanceof Collection listValue) { serializeList(listValue, sb); } else if (value instanceof Enum enumValue) { sb.append("\"").append(enumValue.name()).append("\""); @@ -59,31 +63,37 @@ private static String serializeToString(Notoml notoml) { return sb.toString(); } - private static String serializeList(List list, StringBuilder sb) { + private static String serializeList(Collection list, StringBuilder sb) { sb.append("[ "); boolean newLines = list.size() > 3; - if (newLines) { - sb.append("\n"); - } - for (int i = 0; i < list.size(); i++) { - if (newLines) { - sb.append(" "); - } - if (list.get(i) instanceof String) { - sb.append("\"").append(((String) list.get(i)).replace("\"", "\\\"")).append("\""); - } else if (list.get(i) instanceof Enum) { - sb.append("\"").append(((Enum) list.get(i)).name()).append("\""); - } else { - sb.append(list.get(i)); - } - if (i != list.size() - 1) { + var first = true; + for (final var value : list) { + if (!first) { sb.append(", "); } + first = false; + if (newLines) { sb.append("\n"); } + if (newLines) { + sb.append(" "); + } + if (value instanceof String stringValue) { + sb.append("\"").append(stringValue.replace("\"", "\\\"")).append("\""); + } else if (value instanceof ResourceLocation resourceLocationValue) { + sb.append("\"").append(resourceLocationValue).append("\""); + } else if (value instanceof Enum enumValue) { + sb.append("\"").append(enumValue.name()).append("\""); + } else { + sb.append(value); + } + } + if (newLines) { + sb.append("\n]"); + } else { + sb.append(" ]"); } - sb.append(" ]"); return sb.toString(); } diff --git a/shared/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java b/shared/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java index 8b128029..9fa4df7e 100644 --- a/shared/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java +++ b/shared/src/main/java/net/blay09/mods/balm/config/ExampleConfigData.java @@ -4,9 +4,11 @@ import net.blay09.mods.balm.api.config.Comment; import net.blay09.mods.balm.api.config.Config; import net.blay09.mods.balm.api.config.ExpectedType; +import net.minecraft.resources.ResourceLocation; import java.util.Arrays; import java.util.List; +import java.util.Set; @Config("balm") public class ExampleConfigData implements BalmConfigData { @@ -24,6 +26,8 @@ public enum ExampleEnum { public ExampleEnum exampleEnum = ExampleEnum.Hello; @ExpectedType(String.class) public List exampleStringList = Arrays.asList("Hello", "World"); + @ExpectedType(ResourceLocation.class) + public Set exampleResourceLocationSet = Set.of(new ResourceLocation("dirt"), new ResourceLocation("diamond")); @ExpectedType(Integer.class) public List exampleIntList = Arrays.asList(12, 24); @ExpectedType(ExampleEnum.class)