From 602112724d8236c1ec6671e1893128862c9f5815 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Thu, 22 Feb 2024 00:30:05 +0100 Subject: Add custom model predicates Add regex support Add and and or predicates --- .../texturepack/CustomModelOverrideParser.kt | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt (limited to 'src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt') diff --git a/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt new file mode 100644 index 0000000..ac62eaa --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt @@ -0,0 +1,48 @@ +/* + * SPDX-FileCopyrightText: 2024 Linnea Gräf + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +package moe.nea.firmament.features.texturepack + +import com.google.gson.JsonObject +import net.minecraft.util.Identifier + +object CustomModelOverrideParser { + + val predicateParsers = mutableMapOf() + + + fun registerPredicateParser(name: String, parser: FirmamentModelPredicateParser) { + predicateParsers[Identifier("firmament", name)] = parser + } + + init { + registerPredicateParser("display_name", DisplayNamePredicate.Parser) + registerPredicateParser("lore", LorePredicate.Parser) + registerPredicateParser("all", AndPredicate.Parser) + registerPredicateParser("any", OrPredicate.Parser) + } + + fun parsePredicates(predicates: JsonObject): List { + val parsedPredicates = mutableListOf() + for (predicateName in predicates.keySet()) { + if (!predicateName.startsWith("firmament:")) continue + val identifier = Identifier(predicateName) + val parser = predicateParsers[identifier] ?: continue + val parsedPredicate = parser.parse(predicates[predicateName]) + parsedPredicates.add(parsedPredicate) + } + return parsedPredicates + } + + @JvmStatic + fun parseCustomModelOverrides(jsonObject: JsonObject): Array? { + val predicates = (jsonObject["predicate"] as? JsonObject) ?: return null + val parsedPredicates = parsePredicates(predicates) + if (parsedPredicates.isEmpty()) + return null + return parsedPredicates.toTypedArray() + } +} -- cgit