aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin
diff options
context:
space:
mode:
authorLinnea Gräf <nea@nea.moe>2024-05-14 19:35:39 +0200
committerLinnea Gräf <nea@nea.moe>2024-05-14 19:35:39 +0200
commit21e84ba59f253a78fcf0ba6187ba27ba52114ee6 (patch)
tree504d3ffc62b59728675657e15cf689348a994b1a /src/main/kotlin
parent7682534f6fd139b75f24c79b76098fe05f0fa0fe (diff)
downloadFirmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.tar.gz
Firmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.tar.bz2
Firmament-21e84ba59f253a78fcf0ba6187ba27ba52114ee6.zip
Add item predicate
Diffstat (limited to 'src/main/kotlin')
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/texturepack/CustomModelOverrideParser.kt3
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/texturepack/FirmamentModelPredicateParser.kt2
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/texturepack/ItemPredicate.kt37
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/MC.kt2
4 files changed, 42 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
+ }
+ }
+}
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)
}