aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt
blob: ad1f436a89822d4a579e776583960e88d48f421f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
 * SPDX-FileCopyrightText: 2024 Linnea Gräf <nea@nea.moe>
 *
 * 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<Identifier, FirmamentModelPredicateParser>()


    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)
        registerPredicateParser("not", NotPredicate.Parser)
    }

    fun parsePredicates(predicates: JsonObject): List<FirmamentModelPredicate> {
        val parsedPredicates = mutableListOf<FirmamentModelPredicate>()
        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<FirmamentModelPredicate>? {
        val predicates = (jsonObject["predicate"] as? JsonObject) ?: return null
        val parsedPredicates = parsePredicates(predicates)
        if (parsedPredicates.isEmpty())
            return null
        return parsedPredicates.toTypedArray()
    }
}