Fixed version compatibility issues on FastItemStack

This commit is contained in:
Will FP
2025-01-20 13:38:03 +00:00
parent 91eff08c25
commit afd794e443
5 changed files with 90 additions and 13 deletions

View File

@@ -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

View File

@@ -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 <T> getRegistry(key: ResourceKey<Registry<T>>): Registry<T>
}

View File

@@ -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 <T> getRegistry(key: ResourceKey<Registry<T>>): Registry<T> {
val server = Bukkit.getServer() as CraftServer
val access = server.server.registryAccess()
return access.registryOrThrow(key)
}
}
}

View File

@@ -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 <T> getRegistry(key: ResourceKey<Registry<T>>): Registry<T> {
val server = Bukkit.getServer() as CraftServer
val access = server.server.registryAccess()
return access.get(key).get().value()
}
}
}

View File

@@ -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 <T> getRegistry(key: ResourceKey<Registry<T>>): Registry<T> {
val server = Bukkit.getServer() as CraftServer
val access = server.server.registryAccess()
return access.get(key).get().value()
}
}
}