builder model: deserialize AND condition

They are pretty useless, but appear in 1.19 in the chiseled bookshelves.
This commit is contained in:
Bixilon 2023-03-30 14:08:15 +02:00
parent 34857d0878
commit 57c30d062a
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
2 changed files with 21 additions and 4 deletions

View File

@ -29,15 +29,31 @@ class AndCondition(
}
companion object {
const val KEY = "AND"
fun deserialize(data: List<JsonObject>): BuilderCondition? {
val conditions: MutableSet<PropertyCondition> = mutableSetOf()
for (entry in data) {
conditions += PropertyCondition.deserialize(entry) ?: continue
}
if (conditions.isEmpty()) return null
return AndCondition(conditions) // TODO: They can be compacted into one Property condition, could speed up memory usage and performance a bit
}
fun deserialize(data: JsonObject): BuilderCondition? {
if (data.isEmpty()) return null
val property = PropertyCondition.deserialize(data)
val or = data[OrCondition.KEY]?.let { OrCondition.deserialize(it.unsafeCast()) } ?: return property
if (property == null) return or
val conditions: MutableSet<BuilderCondition> = mutableSetOf()
return AndCondition(mutableSetOf(property, or))
PropertyCondition.deserialize(data)?.let { conditions += it }
data[KEY]?.let { deserialize(it.unsafeCast<List<JsonObject>>()) }?.let { conditions += it }
data[OrCondition.KEY]?.let { OrCondition.deserialize(it.unsafeCast()) }?.let { conditions += it }
if (conditions.isEmpty()) return null
return AndCondition(conditions)
}
}
}

View File

@ -63,6 +63,7 @@ class PropertyCondition(
for ((key, value) in data) {
if (key == OrCondition.KEY) continue
if (key == AndCondition.KEY) continue
if (value is List<*>) {
val (property, values) = deserializeOr(key, value.unsafeCast())