port 1.18 changes

- use resource location for tag keys in priority overrides
- implement config backup on invalid entries
This commit is contained in:
Relentless 2022-10-20 23:35:32 +02:00
parent ee3f80d572
commit 85f89d6394
No known key found for this signature in database
GPG key ID: 759D97B8C6F25265
3 changed files with 29 additions and 14 deletions

View file

@ -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 <T extends Config> void save(String name, T config, Serializer<T> 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 <T extends Config> void save(Path p, T config, Serializer<T> 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);

View file

@ -20,13 +20,13 @@ public class UnifyConfig extends Config {
private final List<String> stoneStrata;
private final List<String> unbakedTags;
private final List<String> materials;
private final Map<String, String> priorityOverrides;
private final Map<ResourceLocation, String> priorityOverrides;
private final Set<UnifyTag<Item>> ignoredTags;
private final Set<Pattern> ignoredRecipeTypes;
private final Set<Pattern> ignoredRecipes;
private final boolean hideJeiRei;
public UnifyConfig(List<String> modPriorities, List<String> stoneStrata, List<String> unbakedTags, List<String> materials, Map<String, String> priorityOverrides, Set<UnifyTag<Item>> ignoredTags, Set<Pattern> ignoredRecipeTypes, Set<Pattern> ignoredRecipes, boolean hideJeiRei) {
public UnifyConfig(List<String> modPriorities, List<String> stoneStrata, List<String> unbakedTags, List<String> materials, Map<ResourceLocation, String> priorityOverrides, Set<UnifyTag<Item>> ignoredTags, Set<Pattern> ignoredRecipeTypes, Set<Pattern> 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<String, String> getPriorityOverrides() {
public Map<ResourceLocation, String> getPriorityOverrides() {
return Collections.unmodifiableMap(priorityOverrides);
}
@ -105,13 +105,14 @@ public class UnifyConfig extends Config {
Defaults.getTags(AlmostUnifiedPlatform.INSTANCE.getPlatform()));
List<String> materials = safeGet(() -> JsonUtils.toList(json.getAsJsonArray(MATERIALS)),
Defaults.MATERIALS);
LinkedHashMap<String, String> priorityOverrides = safeGet(() -> json.getAsJsonObject(PRIORITY_OVERRIDES)
Map<ResourceLocation, String> 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<UnifyTag<Item>> 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,

View file

@ -107,7 +107,7 @@ public class ReplacementMap {
@Nullable
private ResourceLocation getOverrideForTag(UnifyTag<Item> tag, List<ResourceLocation> 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;