From 21e84ba59f253a78fcf0ba6187ba27ba52114ee6 Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Tue, 14 May 2024 19:35:39 +0200 Subject: Add item predicate --- .../texturepack/CustomModelOverrideParser.kt | 3 +- .../texturepack/FirmamentModelPredicateParser.kt | 2 +- .../features/texturepack/ItemPredicate.kt | 37 ++++++++++++++++++++++ src/main/kotlin/moe/nea/firmament/util/MC.kt | 2 ++ 4 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/features/texturepack/ItemPredicate.kt (limited to 'src/main/kotlin/moe/nea/firmament') 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 { @@ -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 + * + * 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 + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index 2fb1c75..830934f 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -13,6 +13,7 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.gui.screen.ingame.HandledScreen import net.minecraft.network.packet.c2s.play.CommandExecutionC2SPacket import net.minecraft.registry.BuiltinRegistries +import net.minecraft.registry.RegistryKeys import net.minecraft.registry.RegistryWrapper import net.minecraft.resource.ReloadableResourceManagerImpl import net.minecraft.text.Text @@ -74,6 +75,7 @@ object MC { inline val window get() = MinecraftClient.getInstance().window inline val currentRegistries: RegistryWrapper.WrapperLookup? get() = world?.registryManager val defaultRegistries: RegistryWrapper.WrapperLookup = BuiltinRegistries.createWrapperLookup() + val defaultItems = defaultRegistries.getWrapperOrThrow(RegistryKeys.ITEM) } -- cgit