aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea/firmament/features/texturepack/ExtraAttributesPredicate.kt
blob: 4114f45550c910037fa900e87bb6007c144d336f (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package moe.nea.firmament.features.texturepack

import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import net.minecraft.item.ItemStack
import net.minecraft.nbt.NbtByte
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtDouble
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtFloat
import net.minecraft.nbt.NbtInt
import net.minecraft.nbt.NbtList
import net.minecraft.nbt.NbtLong
import net.minecraft.nbt.NbtShort
import net.minecraft.nbt.NbtString
import moe.nea.firmament.util.extraAttributes

fun interface NbtMatcher {
    fun matches(nbt: NbtElement): Boolean

    object Parser {
        fun parse(jsonElement: JsonElement): NbtMatcher? {
            if (jsonElement is JsonPrimitive) {
                if (jsonElement.isString) {
                    val string = jsonElement.asString
                    return MatchStringExact(string)
                }
                if (jsonElement.isNumber) {
                    return MatchNumberExact(jsonElement.asLong) //TODO: parse generic number
                }
            }
            if (jsonElement is JsonObject) {
                var encounteredParser: NbtMatcher? = null
                for (entry in ExclusiveParserType.entries) {
                    val data = jsonElement[entry.key] ?: continue
                    if (encounteredParser != null) {
                        // TODO: warn
                        return null
                    }
                    encounteredParser = entry.parse(data) ?: return null
                }
                return encounteredParser
            }
            return null
        }

        enum class ExclusiveParserType(val key: String) {
            STRING("string") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return MatchString(StringMatcher.parse(element))
                }
            },
            INT("int") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asInt },
                                              { (it as? NbtInt)?.intValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            FLOAT("float") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asFloat },
                                              { (it as? NbtFloat)?.floatValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            DOUBLE("double") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asDouble },
                                              { (it as? NbtDouble)?.doubleValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            LONG("long") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asLong },
                                              { (it as? NbtLong)?.longValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            SHORT("short") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asShort },
                                              { (it as? NbtShort)?.shortValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            BYTE("byte") {
                override fun parse(element: JsonElement): NbtMatcher? {
                    return parseGenericNumber(element,
                                              { it.asByte },
                                              { (it as? NbtByte)?.byteValue() },
                                              { a, b ->
                                                  if (a == b) Comparison.EQUAL
                                                  else if (a < b) Comparison.LESS_THAN
                                                  else Comparison.GREATER
                                              })
                }
            },
            ;

            abstract fun parse(element: JsonElement): NbtMatcher?
        }

        enum class Comparison {
            LESS_THAN, EQUAL, GREATER
        }

        inline fun <T : Any> parseGenericNumber(
            jsonElement: JsonElement,
            primitiveExtractor: (JsonPrimitive) -> T?,
            crossinline nbtExtractor: (NbtElement) -> T?,
            crossinline compare: (T, T) -> Comparison
        ): NbtMatcher? {
            if (jsonElement is JsonPrimitive) {
                val expected = primitiveExtractor(jsonElement) ?: return null
                return NbtMatcher {
                    val actual = nbtExtractor(it) ?: return@NbtMatcher false
                    compare(actual, expected) == Comparison.EQUAL
                }
            }
            if (jsonElement is JsonObject) {
                val minElement = jsonElement.getAsJsonPrimitive("min")
                val min = if (minElement != null) primitiveExtractor(minElement) ?: return null else null
                val minExclusive = jsonElement.get("minExclusive")?.asBoolean ?: false
                val maxElement = jsonElement.getAsJsonPrimitive("max")
                val max = if (maxElement != null) primitiveExtractor(maxElement) ?: return null else null
                val maxExclusive = jsonElement.get("maxExclusive")?.asBoolean ?: true
                if (min == null && max == null) return null
                return NbtMatcher {
                    val actual = nbtExtractor(it) ?: return@NbtMatcher false
                    if (max != null) {
                        val comp = compare(actual, max)
                        if (comp == Comparison.GREATER) return@NbtMatcher false
                        if (comp == Comparison.EQUAL && maxExclusive) return@NbtMatcher false
                    }
                    if (min != null) {
                        val comp = compare(actual, min)
                        if (comp == Comparison.LESS_THAN) return@NbtMatcher false
                        if (comp == Comparison.EQUAL && minExclusive) return@NbtMatcher false
                    }
                    return@NbtMatcher true
                }
            }
            return null

        }
    }

    class MatchNumberExact(val number: Long) : NbtMatcher {
        override fun matches(nbt: NbtElement): Boolean {
            return when (nbt) {
                is NbtByte -> nbt.byteValue().toLong() == number
                is NbtInt -> nbt.intValue().toLong() == number
                is NbtShort -> nbt.shortValue().toLong() == number
                is NbtLong -> nbt.longValue().toLong() == number
                else -> false
            }
        }

    }

    class MatchStringExact(val string: String) : NbtMatcher {
        override fun matches(nbt: NbtElement): Boolean {
            return nbt is NbtString && nbt.asString() == string
        }

        override fun toString(): String {
            return "MatchNbtStringExactly($string)"
        }
    }

    class MatchString(val string: StringMatcher) : NbtMatcher {
        override fun matches(nbt: NbtElement): Boolean {
            return nbt is NbtString && string.matches(nbt.asString())
        }

        override fun toString(): String {
            return "MatchNbtString($string)"
        }
    }
}

data class ExtraAttributesPredicate(
    val path: NbtPrism,
    val matcher: NbtMatcher,
) : FirmamentModelPredicate {

    object Parser : FirmamentModelPredicateParser {
        override fun parse(jsonElement: JsonElement): FirmamentModelPredicate? {
            if (jsonElement !is JsonObject) return null
            val path = jsonElement.get("path") ?: return null
            val pathSegments = if (path is JsonArray) {
                path.map { (it as JsonPrimitive).asString }
            } else if (path is JsonPrimitive && path.isString) {
                path.asString.split(".")
            } else return null
            val matcher = NbtMatcher.Parser.parse(jsonElement.get("match") ?: jsonElement)
                ?: return null
            return ExtraAttributesPredicate(NbtPrism(pathSegments), matcher)
        }
    }

    override fun test(stack: ItemStack): Boolean {
        return path.access(stack.extraAttributes)
            .any { matcher.matches(it) }
    }
}

class NbtPrism(val path: List<String>) {
    override fun toString(): String {
        return "Prism($path)"
    }
    fun access(root: NbtElement): Collection<NbtElement> {
        var rootSet = mutableListOf(root)
        var switch = mutableListOf<NbtElement>()
        for (pathSegment in path) {
            if (pathSegment == ".") continue
            for (element in rootSet) {
                if (element is NbtList) {
                    if (pathSegment == "*")
                        switch.addAll(element)
                    val index = pathSegment.toIntOrNull() ?: continue
                    if (index !in element.indices) continue
                    switch.add(element[index])
                }
                if (element is NbtCompound) {
                    if (pathSegment == "*")
                        element.keys.mapTo(switch) { element.get(it)!! }
                    switch.add(element.get(pathSegment) ?: continue)
                }
            }
            val temp = switch
            switch = rootSet
            rootSet = temp
            switch.clear()
        }
        return rootSet
    }
}