diff --git a/Common/src/main/java/com/almostreliable/unified/config/Config.java b/Common/src/main/java/com/almostreliable/unified/config/Config.java index 9abf5c7..05f740d 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/Config.java +++ b/Common/src/main/java/com/almostreliable/unified/config/Config.java @@ -25,19 +25,33 @@ public class Config { JsonObject json = safeLoadJson(name); T config = serializer.deserialize(json); if (serializer.isInvalid()) { - AlmostUnified.LOG.warn("Config {} is invalid or does not exist. Saving new config", name); - save(name, config, serializer); + Path filePath = buildPath(createConfigDir(), name); + if (Files.exists(filePath)) { + backupConfig(name, filePath); + } + AlmostUnified.LOG.warn("Creating config: {}", name); + save(filePath, config, serializer); } return config; } - public static void save(String name, T config, Serializer serializer) { + private static void backupConfig(String name, Path p) { + AlmostUnified.LOG.warn("Config {} is invalid. Backing up and recreating.", name); + Path backupPath = p.resolveSibling(p.getFileName() + ".bak"); + try { + Files.deleteIfExists(backupPath); + Files.move(p, backupPath); + } catch (IOException e) { + AlmostUnified.LOG.error("Could not backup config file", e); + } + } + + public static void save(Path p, T config, Serializer serializer) { JsonObject json = serializer.serialize(config); - Path p = createConfigDir(); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonString = gson.toJson(json); try { - Files.writeString(buildPath(p, name), + Files.writeString(p, jsonString, StandardOpenOption.CREATE, StandardOpenOption.WRITE); diff --git a/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java b/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java index 1e01396..823392f 100644 --- a/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java +++ b/Common/src/main/java/com/almostreliable/unified/config/UnifyConfig.java @@ -20,13 +20,13 @@ public class UnifyConfig extends Config { private final List stoneStrata; private final List unbakedTags; private final List materials; - private final Map priorityOverrides; + private final Map priorityOverrides; private final Set> ignoredTags; private final Set ignoredRecipeTypes; private final Set ignoredRecipes; private final boolean hideJeiRei; - public UnifyConfig(List modPriorities, List stoneStrata, List unbakedTags, List materials, Map priorityOverrides, Set> ignoredTags, Set ignoredRecipeTypes, Set ignoredRecipes, boolean hideJeiRei) { + public UnifyConfig(List modPriorities, List stoneStrata, List unbakedTags, List materials, Map priorityOverrides, Set> ignoredTags, Set ignoredRecipeTypes, Set ignoredRecipes, boolean hideJeiRei) { this.modPriorities = modPriorities; this.stoneStrata = stoneStrata; this.unbakedTags = unbakedTags; @@ -67,7 +67,7 @@ public class UnifyConfig extends Config { return result; } - public Map getPriorityOverrides() { + public Map getPriorityOverrides() { return Collections.unmodifiableMap(priorityOverrides); } @@ -105,13 +105,14 @@ public class UnifyConfig extends Config { Defaults.getTags(AlmostUnifiedPlatform.INSTANCE.getPlatform())); List materials = safeGet(() -> JsonUtils.toList(json.getAsJsonArray(MATERIALS)), Defaults.MATERIALS); - LinkedHashMap priorityOverrides = safeGet(() -> json.getAsJsonObject(PRIORITY_OVERRIDES) + Map priorityOverrides = safeGet(() -> json.getAsJsonObject(PRIORITY_OVERRIDES) .entrySet() .stream() - .collect(Collectors.toMap(Map.Entry::getKey, - e -> e.getValue().getAsString(), + .collect(Collectors.toMap( + entry -> new ResourceLocation(entry.getKey()), + entry -> entry.getValue().getAsString(), (a, b) -> b, - LinkedHashMap::new)), new LinkedHashMap<>()); + HashMap::new)), new HashMap<>()); Set> ignoredTags = safeGet(() -> JsonUtils .toList(json.getAsJsonArray(IGNORED_TAGS)) .stream() @@ -145,7 +146,7 @@ public class UnifyConfig extends Config { json.add(MATERIALS, JsonUtils.toArray(config.materials)); JsonObject priorityOverrides = new JsonObject(); config.priorityOverrides.forEach((tag, mod) -> { - priorityOverrides.add(tag, new JsonPrimitive(mod)); + priorityOverrides.add(tag.toString(), new JsonPrimitive(mod)); }); json.add(PRIORITY_OVERRIDES, priorityOverrides); json.add(IGNORED_TAGS, diff --git a/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java b/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java index f459ca0..6c3f631 100644 --- a/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java +++ b/Common/src/main/java/com/almostreliable/unified/utils/ReplacementMap.java @@ -107,7 +107,7 @@ public class ReplacementMap { @Nullable private ResourceLocation getOverrideForTag(UnifyTag tag, List items) { - String priorityOverride = unifyConfig.getPriorityOverrides().get(tag.location().toString()); + String priorityOverride = unifyConfig.getPriorityOverrides().get(tag.location()); if (priorityOverride != null) { ResourceLocation item = findItemByNamespace(items, priorityOverride); if (item != null) return item;