From afd794e4438ccc6bcc4f62af346f44e71d35a84b Mon Sep 17 00:00:00 2001 From: Will FP Date: Mon, 20 Jan 2025 13:38:03 +0000 Subject: [PATCH] Fixed version compatibility issues on FastItemStack --- .../common/modern/NewEcoFastItemStack.kt | 24 +++++++------ .../proxy/common/modern/RegistryAccessor.kt | 14 ++++++++ .../proxy/v1_21/FastItemStackFactory.kt | 35 ++++++++++++++++++- .../proxy/v1_21_3/FastItemStackFactory.kt | 15 +++++++- .../proxy/v1_21_4/FastItemStackFactory.kt | 15 +++++++- 5 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/RegistryAccessor.kt diff --git a/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/NewEcoFastItemStack.kt b/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/NewEcoFastItemStack.kt index 8cc652b6..2a6a9eeb 100644 --- a/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/NewEcoFastItemStack.kt +++ b/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/NewEcoFastItemStack.kt @@ -35,6 +35,7 @@ import org.bukkit.inventory.ItemFlag import org.bukkit.inventory.ItemStack import org.bukkit.persistence.PersistentDataContainer import kotlin.math.max +import kotlin.math.min private val unstyledComponent = Component.empty().style { it.color(null).decoration(TextDecoration.ITALIC, false) @@ -44,12 +45,13 @@ private fun Component.unstyled(): Component { return unstyledComponent.append(this) } -class NewEcoFastItemStack( - private val bukkit: ItemStack +open class NewEcoFastItemStack( + private val bukkit: ItemStack, + private val registryAccessor: RegistryAccessor ) : ImplementedFIS { // Cast is there because, try as I might, I can't get IntellIJ to recognise half the classes in the dev bundle @Suppress("USELESS_CAST") - private val handle = bukkit.asNMSStack() as net.minecraft.world.item.ItemStack + protected val handle = bukkit.asNMSStack() as net.minecraft.world.item.ItemStack private val pdc = (handle.get(DataComponents.CUSTOM_DATA)?.copyTag() ?: CompoundTag()).makePdc() @@ -86,10 +88,8 @@ class NewEcoFastItemStack( enchantment ) - val server = Bukkit.getServer() as CraftServer - val access = server.server.registryAccess() - - val holder = access.registryOrThrow(Registries.ENCHANTMENT).wrapAsHolder(minecraft) + val registry = registryAccessor.getRegistry(Registries.ENCHANTMENT) + val holder = registry.wrapAsHolder(minecraft) val enchantments = handle.get(DataComponents.ENCHANTMENTS) ?: return 0 var level = enchantments.getLevel(holder) @@ -369,19 +369,23 @@ class NewEcoFastItemStack( override fun getType(): org.bukkit.Material = handle.getItem().toMaterial() + /* + Custom model data doesn't work based on an integer since 1.21.3, so these methods do nothing + */ + override fun getCustomModelData(): Int? = - handle.get(DataComponents.CUSTOM_MODEL_DATA)?.value + null override fun setCustomModelData(data: Int?) { if (data == null) { handle.remove(DataComponents.CUSTOM_MODEL_DATA) - } else { - handle.set(DataComponents.CUSTOM_MODEL_DATA, CustomModelData(data)) } apply() } + // END + override fun equals(other: Any?): Boolean { if (other !is NewEcoFastItemStack) { return false diff --git a/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/RegistryAccessor.kt b/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/RegistryAccessor.kt new file mode 100644 index 00000000..2dd35116 --- /dev/null +++ b/eco-core/core-nms/modern/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/common/modern/RegistryAccessor.kt @@ -0,0 +1,14 @@ +package com.willfp.eco.internal.spigot.proxy.common.modern + +import net.minecraft.core.Registry +import net.minecraft.resources.ResourceKey + +/** + * Cross-version compat method for accessing registries. + */ +interface RegistryAccessor { + /** + * Get registry by [key] or throw. + */ + fun getRegistry(key: ResourceKey>): Registry +} diff --git a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21/FastItemStackFactory.kt b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21/FastItemStackFactory.kt index cd6fc800..9a71b2bc 100644 --- a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21/FastItemStackFactory.kt @@ -3,10 +3,43 @@ package com.willfp.eco.internal.spigot.proxy.v1_21 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import com.willfp.eco.internal.spigot.proxy.common.modern.NewEcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor +import net.minecraft.core.Registry +import net.minecraft.core.component.DataComponents +import net.minecraft.core.registries.Registries +import net.minecraft.resources.ResourceKey +import net.minecraft.world.item.component.CustomModelData +import org.bukkit.Bukkit +import org.bukkit.craftbukkit.CraftServer import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy { override fun create(itemStack: ItemStack): FastItemStack { - return NewEcoFastItemStack(itemStack) + return LegacyNewEcoFastItemStack(itemStack) + } + + private class LegacyNewEcoFastItemStack(itemStack: ItemStack) : + NewEcoFastItemStack(itemStack, LegacyRegistryAccessor) { + + override fun getCustomModelData(): Int? = + handle.get(DataComponents.CUSTOM_MODEL_DATA)?.value + + override fun setCustomModelData(data: Int?) { + if (data == null) { + handle.remove(DataComponents.CUSTOM_MODEL_DATA) + } else { + handle.set(DataComponents.CUSTOM_MODEL_DATA, CustomModelData(data)) + } + + apply() + } + } + + private object LegacyRegistryAccessor : RegistryAccessor { + override fun getRegistry(key: ResourceKey>): Registry { + val server = Bukkit.getServer() as CraftServer + val access = server.server.registryAccess() + return access.registryOrThrow(key) + } } } diff --git a/eco-core/core-nms/v1_21_3/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_3/FastItemStackFactory.kt b/eco-core/core-nms/v1_21_3/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_3/FastItemStackFactory.kt index dce32a47..3071a0d2 100644 --- a/eco-core/core-nms/v1_21_3/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_3/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_21_3/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_3/FastItemStackFactory.kt @@ -3,10 +3,23 @@ package com.willfp.eco.internal.spigot.proxy.v1_21_3 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import com.willfp.eco.internal.spigot.proxy.common.modern.NewEcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor +import net.minecraft.core.Registry +import net.minecraft.resources.ResourceKey +import org.bukkit.Bukkit +import org.bukkit.craftbukkit.CraftServer import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy { override fun create(itemStack: ItemStack): FastItemStack { - return NewEcoFastItemStack(itemStack) + return NewEcoFastItemStack(itemStack, ModernRegistryAccessor) + } + + private object ModernRegistryAccessor : RegistryAccessor { + override fun getRegistry(key: ResourceKey>): Registry { + val server = Bukkit.getServer() as CraftServer + val access = server.server.registryAccess() + return access.get(key).get().value() + } } } diff --git a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_4/FastItemStackFactory.kt b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_4/FastItemStackFactory.kt index 8b2d0541..d3192342 100644 --- a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_4/FastItemStackFactory.kt +++ b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/eco/internal/spigot/proxy/v1_21_4/FastItemStackFactory.kt @@ -3,10 +3,23 @@ package com.willfp.eco.internal.spigot.proxy.v1_21_4 import com.willfp.eco.core.fast.FastItemStack import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy import com.willfp.eco.internal.spigot.proxy.common.modern.NewEcoFastItemStack +import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor +import net.minecraft.core.Registry +import net.minecraft.resources.ResourceKey +import org.bukkit.Bukkit +import org.bukkit.craftbukkit.CraftServer import org.bukkit.inventory.ItemStack class FastItemStackFactory : FastItemStackFactoryProxy { override fun create(itemStack: ItemStack): FastItemStack { - return NewEcoFastItemStack(itemStack) + return NewEcoFastItemStack(itemStack, ModernRegistryAccessor) + } + + private object ModernRegistryAccessor : RegistryAccessor { + override fun getRegistry(key: ResourceKey>): Registry { + val server = Bukkit.getServer() as CraftServer + val access = server.server.registryAccess() + return access.get(key).get().value() + } } }