diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java index fff834c7..9df54b97 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/Items.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/Items.java @@ -187,12 +187,7 @@ public final class Items { @NotNull public static TestableItem lookup(@NotNull final String key) { if (key.startsWith("{")) { - ItemStack generated = fromSNBT(key); - if (generated != null) { - return new ExactTestableItem(generated); - } else { - return new EmptyTestableItem(); - } + return Eco.getHandler().getSNBTHandler().createTestable(key); } return ITEMS_LOOKUP_HANDLER.parseKey(key); diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/SNBTHandler.java b/eco-api/src/main/java/com/willfp/eco/core/items/SNBTHandler.java index f0f693ee..1a73ae34 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/SNBTHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/SNBTHandler.java @@ -29,4 +29,13 @@ public interface SNBTHandler { */ @NotNull String toSNBT(@NotNull ItemStack itemStack); + + /** + * Make TestableItem from SNBT. + * + * @param snbt The NBT string. + * @return The TestableItem. + */ + @NotNull + TestableItem createTestable(@NotNull String snbt); } diff --git a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/SNBTConverter.kt b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/SNBTConverter.kt index 9a9e35d7..4f9c4050 100644 --- a/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/SNBTConverter.kt +++ b/eco-core/core-nms/v1_17_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_17_R1/SNBTConverter.kt @@ -1,5 +1,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_17_R1 +import com.willfp.eco.core.items.TestableItem +import com.willfp.eco.core.recipe.parts.EmptyTestableItem import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.SnbtPrinterTagVisitor @@ -18,4 +20,35 @@ class SNBTConverter : SNBTConverterProxy { val nms = CraftItemStack.asNMSCopy(itemStack) return SnbtPrinterTagVisitor().visit(nms.save(CompoundTag())) } + + override fun makeSNBTTestable(snbt: String): TestableItem { + val nbt = runCatching { TagParser.parseTag(snbt) }.getOrNull() ?: return EmptyTestableItem() + val nms = net.minecraft.world.item.ItemStack.of(nbt) + if (nms == net.minecraft.world.item.ItemStack.EMPTY) { + return EmptyTestableItem() + } + + nbt.remove("Count") + + return SNBTTestableItem(nbt) + } + + class SNBTTestableItem( + private val tag: CompoundTag + ) : TestableItem { + override fun matches(itemStack: ItemStack?): Boolean { + if (itemStack == null) { + return false + } + + val nms = CraftItemStack.asNMSCopy(itemStack) + val nmsTag = nms.save(CompoundTag()) + nmsTag.remove("Count") + return tag.copy().merge(nmsTag) == nmsTag + } + + override fun getItem(): ItemStack { + return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(tag)) + } + } } \ No newline at end of file diff --git a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/SNBTConverter.kt b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/SNBTConverter.kt index 0bf5355b..4e4a21e9 100644 --- a/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/SNBTConverter.kt +++ b/eco-core/core-nms/v1_18_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R1/SNBTConverter.kt @@ -1,5 +1,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R1 +import com.willfp.eco.core.items.TestableItem +import com.willfp.eco.core.recipe.parts.EmptyTestableItem import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.SnbtPrinterTagVisitor @@ -18,4 +20,35 @@ class SNBTConverter : SNBTConverterProxy { val nms = CraftItemStack.asNMSCopy(itemStack) return SnbtPrinterTagVisitor().visit(nms.save(CompoundTag())) } + + override fun makeSNBTTestable(snbt: String): TestableItem { + val nbt = runCatching { TagParser.parseTag(snbt) }.getOrNull() ?: return EmptyTestableItem() + val nms = net.minecraft.world.item.ItemStack.of(nbt) + if (nms == net.minecraft.world.item.ItemStack.EMPTY) { + return EmptyTestableItem() + } + + nbt.remove("Count") + + return SNBTTestableItem(nbt) + } + + class SNBTTestableItem( + private val tag: CompoundTag + ) : TestableItem { + override fun matches(itemStack: ItemStack?): Boolean { + if (itemStack == null) { + return false + } + + val nms = CraftItemStack.asNMSCopy(itemStack) + val nmsTag = nms.save(CompoundTag()) + nmsTag.remove("Count") + return tag.copy().merge(nmsTag) == nmsTag + } + + override fun getItem(): ItemStack { + return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(tag)) + } + } } \ No newline at end of file diff --git a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/SNBTConverter.kt b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/SNBTConverter.kt index 0be53af4..fd96cc5e 100644 --- a/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/SNBTConverter.kt +++ b/eco-core/core-nms/v1_18_R2/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_18_R2/SNBTConverter.kt @@ -1,5 +1,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_18_R2 +import com.willfp.eco.core.items.TestableItem +import com.willfp.eco.core.recipe.parts.EmptyTestableItem import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.SnbtPrinterTagVisitor @@ -18,4 +20,35 @@ class SNBTConverter : SNBTConverterProxy { val nms = CraftItemStack.asNMSCopy(itemStack) return SnbtPrinterTagVisitor().visit(nms.save(CompoundTag())) } + + override fun makeSNBTTestable(snbt: String): TestableItem { + val nbt = runCatching { TagParser.parseTag(snbt) }.getOrNull() ?: return EmptyTestableItem() + val nms = net.minecraft.world.item.ItemStack.of(nbt) + if (nms == net.minecraft.world.item.ItemStack.EMPTY) { + return EmptyTestableItem() + } + + nbt.remove("Count") + + return SNBTTestableItem(nbt) + } + + class SNBTTestableItem( + private val tag: CompoundTag + ) : TestableItem { + override fun matches(itemStack: ItemStack?): Boolean { + if (itemStack == null) { + return false + } + + val nms = CraftItemStack.asNMSCopy(itemStack) + val nmsTag = nms.save(CompoundTag()) + nmsTag.remove("Count") + return tag.copy().merge(nmsTag) == nmsTag + } + + override fun getItem(): ItemStack { + return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(tag)) + } + } } \ No newline at end of file diff --git a/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/SNBTConverter.kt b/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/SNBTConverter.kt index fff53a57..13990a84 100644 --- a/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/SNBTConverter.kt +++ b/eco-core/core-nms/v1_19_R1/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_19_R1/SNBTConverter.kt @@ -1,5 +1,7 @@ package com.willfp.eco.internal.spigot.proxy.v1_19_R1 +import com.willfp.eco.core.items.TestableItem +import com.willfp.eco.core.recipe.parts.EmptyTestableItem import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.SnbtPrinterTagVisitor @@ -18,4 +20,35 @@ class SNBTConverter : SNBTConverterProxy { val nms = CraftItemStack.asNMSCopy(itemStack) return SnbtPrinterTagVisitor().visit(nms.save(CompoundTag())) } -} \ No newline at end of file + + override fun makeSNBTTestable(snbt: String): TestableItem { + val nbt = runCatching { TagParser.parseTag(snbt) }.getOrNull() ?: return EmptyTestableItem() + val nms = net.minecraft.world.item.ItemStack.of(nbt) + if (nms == net.minecraft.world.item.ItemStack.EMPTY) { + return EmptyTestableItem() + } + + nbt.remove("Count") + + return SNBTTestableItem(nbt) + } + + class SNBTTestableItem( + private val tag: CompoundTag + ) : TestableItem { + override fun matches(itemStack: ItemStack?): Boolean { + if (itemStack == null) { + return false + } + + val nms = CraftItemStack.asNMSCopy(itemStack) + val nmsTag = nms.save(CompoundTag()) + nmsTag.remove("Count") + return tag.copy().merge(nmsTag) == nmsTag + } + + override fun getItem(): ItemStack { + return CraftItemStack.asBukkitCopy(net.minecraft.world.item.ItemStack.of(tag)) + } + } +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/items/EcoSNBTHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/items/EcoSNBTHandler.kt index 287dc6d6..51d69b88 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/items/EcoSNBTHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/items/EcoSNBTHandler.kt @@ -2,6 +2,7 @@ package com.willfp.eco.internal.spigot.items import com.willfp.eco.core.EcoPlugin import com.willfp.eco.core.items.SNBTHandler +import com.willfp.eco.core.items.TestableItem import com.willfp.eco.internal.spigot.proxy.SNBTConverterProxy import org.bukkit.inventory.ItemStack @@ -13,4 +14,7 @@ class EcoSNBTHandler( override fun toSNBT(itemStack: ItemStack): String = plugin.getProxy(SNBTConverterProxy::class.java).toSNBT(itemStack) + + override fun createTestable(snbt: String): TestableItem = + plugin.getProxy(SNBTConverterProxy::class.java).makeSNBTTestable(snbt) } \ No newline at end of file diff --git a/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/SNBTConverterProxy.kt b/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/SNBTConverterProxy.kt index f899cc7e..e8f3a31d 100644 --- a/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/SNBTConverterProxy.kt +++ b/eco-core/core-proxy/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/SNBTConverterProxy.kt @@ -1,8 +1,10 @@ package com.willfp.eco.internal.spigot.proxy +import com.willfp.eco.core.items.TestableItem import org.bukkit.inventory.ItemStack interface SNBTConverterProxy { fun toSNBT(itemStack: ItemStack): String fun fromSNBT(snbt: String): ItemStack? + fun makeSNBTTestable(snbt: String): TestableItem } \ No newline at end of file