Skip to content

Commit

Permalink
fix: Fix fabric config saving of ResourceLocation and Set types
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 27, 2023
1 parent 0109f14 commit d029eaa
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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("\"");
Expand All @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -24,6 +26,8 @@ public enum ExampleEnum {
public ExampleEnum exampleEnum = ExampleEnum.Hello;
@ExpectedType(String.class)
public List<String> exampleStringList = Arrays.asList("Hello", "World");
@ExpectedType(ResourceLocation.class)
public Set<ResourceLocation> exampleResourceLocationSet = Set.of(new ResourceLocation("dirt"), new ResourceLocation("diamond"));
@ExpectedType(Integer.class)
public List<Integer> exampleIntList = Arrays.asList(12, 24);
@ExpectedType(ExampleEnum.class)
Expand Down

0 comments on commit d029eaa

Please sign in to comment.