|
| 1 | +package zone.moddev.mc.orespawn.integration; |
| 2 | + |
| 3 | +import java.util.HashSet; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Set; |
| 6 | + |
| 7 | +import com.google.gson.JsonElement; |
| 8 | +import com.google.gson.JsonObject; |
| 9 | +import net.minecraft.resources.Identifier; |
| 10 | + |
| 11 | +/** Conservative matching at provider merge time, never in chunk generation. */ |
| 12 | +final class RetainedProviderOreDefaults { |
| 13 | + |
| 14 | + private static final Set<String> BASE_METALS_LEGACY_MATERIALS = Set.of( |
| 15 | + "coldiron", "adamantine", "starsteel", "tin", "lead", "zinc", |
| 16 | + "silver", "mercury", "nickel", "platinum"); |
| 17 | + |
| 18 | + private record Placement(String kind, String dimension, String channel) {} |
| 19 | + |
| 20 | + private RetainedProviderOreDefaults() {} |
| 21 | + |
| 22 | + static String predecessor(String owner, String currentId, JsonObject current, |
| 23 | + JsonObject defaults, JsonObject saved, Set<String> known) { |
| 24 | + String output = singletonOutput(currentId, current); |
| 25 | + Set<Placement> scope = placements(current); |
| 26 | + if (output == null || scope == null || scope.isEmpty()) return null; |
| 27 | + String historical = historicalPredecessor(owner, currentId, output); |
| 28 | + if (historical != null && known.contains(historical) && !saved.has(historical)) return historical; |
| 29 | + |
| 30 | + int replacements = 0; |
| 31 | + for (var entry : defaults.entrySet()) { |
| 32 | + if (equivalentOutputAndScope(entry.getKey(), entry.getValue(), output, scope)) replacements++; |
| 33 | + } |
| 34 | + if (replacements != 1) return null; |
| 35 | + |
| 36 | + String retained = null; |
| 37 | + for (var entry : saved.entrySet()) { |
| 38 | + if (!equivalentOutputAndScope(entry.getKey(), entry.getValue(), output, scope)) continue; |
| 39 | + if (retained != null) return null; |
| 40 | + retained = entry.getKey(); |
| 41 | + } |
| 42 | + return retained != null && declaredPredecessor(owner, currentId, output, retained, saved.getAsJsonObject(retained)) |
| 43 | + ? retained : null; |
| 44 | + } |
| 45 | + |
| 46 | + private static boolean declaredPredecessor(String owner, String currentId, String output, String savedId, JsonObject saved) { |
| 47 | + String importedPrefix = "orespawn:legacy/" + owner + "/"; |
| 48 | + boolean imported = savedId.startsWith(importedPrefix) && savedId.length() > importedPrefix.length(); |
| 49 | + if (saved.has("source_provider")) { |
| 50 | + if (!owner.equals(text(saved.get("source_provider")))) return false; |
| 51 | + } else if (saved.has("source_mod")) { |
| 52 | + String fallbackOwner = text(saved.get("source_mod")); |
| 53 | + if (!owner.equals(fallbackOwner) && !(imported && "legacy".equals(fallbackOwner))) return false; |
| 54 | + } |
| 55 | + if (imported) return true; |
| 56 | + return savedId.equals(historicalPredecessor(owner, currentId, output)); |
| 57 | + } |
| 58 | + |
| 59 | + private static String historicalPredecessor(String owner, String currentId, String output) { |
| 60 | + // Base Metals' maintained 1.10/1.12 providers declared these IDs themselves. |
| 61 | + // A generic provider's "legacy/" prefix is not evidence of a replacement. |
| 62 | + if (!owner.equals("basemetals")) return null; |
| 63 | + for (String material : BASE_METALS_LEGACY_MATERIALS) { |
| 64 | + if (currentId.equals("basemetals:ore/" + material) |
| 65 | + && output.equals("basemetals:" + material + "_ore")) return "basemetals:legacy/" + material + "_ore"; |
| 66 | + } |
| 67 | + return null; |
| 68 | + } |
| 69 | + |
| 70 | + private static boolean equivalentOutputAndScope(String id, JsonElement rule, String output, Set<Placement> scope) { |
| 71 | + return rule.isJsonObject() && output.equals(singletonOutput(id, rule.getAsJsonObject())) |
| 72 | + && scope.equals(placements(rule.getAsJsonObject())); |
| 73 | + } |
| 74 | + |
| 75 | + private static String singletonOutput(String id, JsonObject rule) { |
| 76 | + String output = rule.has("block") ? text(rule.get("block")) : id; |
| 77 | + Identifier parsed = output == null ? null : Identifier.tryParse(output); |
| 78 | + if (parsed == null || !defaultMetadata(rule)) return null; |
| 79 | + output = parsed.toString(); |
| 80 | + if (rule.has("deep_output") && !output.equals(text(rule.get("deep_output")))) return null; |
| 81 | + if (rule.has("outputs")) { |
| 82 | + JsonElement values = rule.get("outputs"); |
| 83 | + if (!values.isJsonArray() || values.getAsJsonArray().size() != 1 |
| 84 | + || !values.getAsJsonArray().get(0).isJsonObject()) return null; |
| 85 | + JsonObject only = values.getAsJsonArray().get(0).getAsJsonObject(); |
| 86 | + if (!output.equals(text(only.get("block"))) || !defaultMetadata(only)) return null; |
| 87 | + } |
| 88 | + return output; |
| 89 | + } |
| 90 | + |
| 91 | + private static boolean defaultMetadata(JsonObject rule) { |
| 92 | + JsonElement metadata = rule.get("metadata"); |
| 93 | + return metadata == null || metadata.isJsonPrimitive() && metadata.getAsJsonPrimitive().isNumber() |
| 94 | + && metadata.getAsBigDecimal().signum() == 0; |
| 95 | + } |
| 96 | + |
| 97 | + private static Set<Placement> placements(JsonObject rule) { |
| 98 | + Set<Placement> result = new HashSet<>(); |
| 99 | + for (String kind : List.of("dimensions", "dimension_selectors")) { |
| 100 | + JsonElement table = rule.get(kind); |
| 101 | + if (table == null) continue; |
| 102 | + if (!table.isJsonObject()) return null; |
| 103 | + for (var entry : table.getAsJsonObject().entrySet()) { |
| 104 | + if (Identifier.tryParse(entry.getKey()) == null || !entry.getValue().isJsonObject()) return null; |
| 105 | + // Disabled and zero-rate placements are saved choices too. |
| 106 | + result.add(new Placement(kind, entry.getKey(), channel(entry.getValue().getAsJsonObject()))); |
| 107 | + } |
| 108 | + } |
| 109 | + return result; |
| 110 | + } |
| 111 | + |
| 112 | + private static String channel(JsonObject placement) { |
| 113 | + Identifier explicit = identifier(placement.get("placement_channel")); |
| 114 | + if (explicit != null) return explicit.toString(); |
| 115 | + JsonElement pattern = placement.get("pattern"); |
| 116 | + Identifier type = pattern != null && pattern.isJsonObject() |
| 117 | + ? identifier(pattern.getAsJsonObject().get("type")) : null; |
| 118 | + String legacy = text(pattern); |
| 119 | + if (type == null && legacy != null && legacy.indexOf(':') >= 0) type = Identifier.tryParse(legacy); |
| 120 | + return type == null ? "orespawn:standard" : type.toString(); |
| 121 | + } |
| 122 | + |
| 123 | + private static Identifier identifier(JsonElement value) { |
| 124 | + String name = text(value); |
| 125 | + return name == null ? null : Identifier.tryParse(name); |
| 126 | + } |
| 127 | + |
| 128 | + private static String text(JsonElement value) { |
| 129 | + return value != null && value.isJsonPrimitive() && value.getAsJsonPrimitive().isString() |
| 130 | + ? value.getAsString() : null; |
| 131 | + } |
| 132 | +} |
0 commit comments