diff options
author | Linnea Gräf <nea@nea.moe> | 2024-05-14 19:35:39 +0200 |
---|---|---|
committer | Linnea Gräf <nea@nea.moe> | 2024-05-14 19:35:39 +0200 |
commit | 21e84ba59f253a78fcf0ba6187ba27ba52114ee6 (patch) | |
tree | 504d3ffc62b59728675657e15cf689348a994b1a /src/main/kotlin/moe/nea/firmament/features/texturepack | |
parent | 7682534f6fd139b75f24c79b76098fe05f0fa0fe (diff) | |
download | Firmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.tar.gz Firmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.tar.bz2 Firmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.zip |
Add item predicate
Diffstat (limited to 'src/main/kotlin/moe/nea/firmament/features/texturepack')
3 files changed, 40 insertions, 2 deletions
diff --git a/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt index d512dec..538c63f 100644 --- a/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt +++ b/src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt @@ -42,6 +42,7 @@ object CustomModelOverrideParser { registerPredicateParser("all", AndPredicate.Parser) registerPredicateParser("any", OrPredicate.Parser) registerPredicateParser("not", NotPredicate.Parser) + registerPredicateParser("item", ItemPredicate.Parser) } fun parsePredicates(predicates: JsonObject): List<FirmamentModelPredicate> { @@ -50,7 +51,7 @@ object CustomModelOverrideParser { if (!predicateName.startsWith("firmament:")) continue val identifier = Identifier(predicateName) val parser = predicateParsers[identifier] ?: continue - val parsedPredicate = parser.parse(predicates[predicateName]) + val parsedPredicate = parser.parse(predicates[predicateName]) ?: continue parsedPredicates.add(parsedPredicate) } return parsedPredicates diff --git a/src/main/kotlin/moe/nea/firmament/features/texturepack/FirmamentModelPredicateParser.kt b/src/main/kotlin/moe/nea/firmament/features/texturepack/FirmamentModelPredicateParser.kt index de7557a..e999679 100644 --- a/src/main/kotlin/moe/nea/firmament/features/texturepack/FirmamentModelPredicateParser.kt +++ b/src/main/kotlin/moe/nea/firmament/features/texturepack/FirmamentModelPredicateParser.kt @@ -9,5 +9,5 @@ package moe.nea.firmament.features.texturepack import com.google.gson.JsonElement interface FirmamentModelPredicateParser { - fun parse(jsonElement: JsonElement): FirmamentModelPredicate + fun parse(jsonElement: JsonElement): FirmamentModelPredicate? } diff --git a/src/main/kotlin/moe/nea/firmament/features/texturepack/ItemPredicate.kt b/src/main/kotlin/moe/nea/firmament/features/texturepack/ItemPredicate.kt new file mode 100644 index 0000000..1464037 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/texturepack/ItemPredicate.kt @@ -0,0 +1,37 @@ +/* + * 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.JsonElement +import com.google.gson.JsonPrimitive +import kotlin.jvm.optionals.getOrNull +import net.minecraft.item.Item +import net.minecraft.item.ItemStack +import net.minecraft.registry.RegistryKey +import net.minecraft.registry.RegistryKeys +import net.minecraft.util.Identifier +import moe.nea.firmament.util.MC + +class ItemPredicate( + val item: Item +) : FirmamentModelPredicate { + override fun test(stack: ItemStack): Boolean { + return stack.item == item + } + + object Parser : FirmamentModelPredicateParser { + override fun parse(jsonElement: JsonElement): ItemPredicate? { + if (jsonElement is JsonPrimitive && jsonElement.isString) { + val itemKey = RegistryKey.of(RegistryKeys.ITEM, + Identifier.tryParse(jsonElement.asString) + ?: return null) + return ItemPredicate(MC.defaultItems.getOptional(itemKey).getOrNull()?.value() ?: return null) + } + return null + } + } +} |