Fixed enchantment registry issues

This commit is contained in:
Will FP
2025-01-31 11:13:16 +00:00
parent 5e4ae1f932
commit 2d46a6c392
5 changed files with 11 additions and 54 deletions

View File

@@ -17,6 +17,7 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextColor
import net.kyori.adventure.text.format.TextDecoration
import net.minecraft.core.Holder
import net.minecraft.core.component.DataComponentType
import net.minecraft.core.component.DataComponents
import net.minecraft.core.registries.Registries
@@ -47,7 +48,6 @@ private fun Component.unstyled(): Component {
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")
@@ -83,20 +83,17 @@ open class NewEcoFastItemStack(
enchantment: Enchantment,
checkStored: Boolean
): Int {
val minecraft =
CraftRegistry.bukkitToMinecraft<Enchantment, net.minecraft.world.item.enchantment.Enchantment>(
enchantment
val minecraft = CraftRegistry.bukkitToMinecraftHolder(
enchantment,
Registries.ENCHANTMENT
)
val registry = registryAccessor.getRegistry(Registries.ENCHANTMENT)
val holder = registry.wrapAsHolder(minecraft)
val enchantments = handle.get(DataComponents.ENCHANTMENTS) ?: return 0
var level = enchantments.getLevel(holder)
var level = enchantments.getLevel(minecraft)
if (checkStored) {
val storedEnchantments = handle.get(DataComponents.STORED_ENCHANTMENTS) ?: return 0
level = max(level, storedEnchantments.getLevel(holder))
level = max(level, storedEnchantments.getLevel(minecraft))
}
return level

View File

@@ -1,14 +0,0 @@
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,7 +3,6 @@ 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
@@ -19,7 +18,7 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
}
private class LegacyNewEcoFastItemStack(itemStack: ItemStack) :
NewEcoFastItemStack(itemStack, LegacyRegistryAccessor) {
NewEcoFastItemStack(itemStack) {
override fun getCustomModelData(): Int? =
handle.get(DataComponents.CUSTOM_MODEL_DATA)?.value
@@ -34,12 +33,4 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
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,7 +3,6 @@ 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
@@ -12,14 +11,6 @@ import org.bukkit.inventory.ItemStack
class FastItemStackFactory : FastItemStackFactoryProxy {
override fun create(itemStack: ItemStack): FastItemStack {
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()
}
return NewEcoFastItemStack(itemStack)
}
}

View File

@@ -3,8 +3,8 @@ 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.core.registries.Registries
import net.minecraft.resources.ResourceKey
import org.bukkit.Bukkit
import org.bukkit.craftbukkit.CraftServer
@@ -12,14 +12,6 @@ import org.bukkit.inventory.ItemStack
class FastItemStackFactory : FastItemStackFactoryProxy {
override fun create(itemStack: ItemStack): FastItemStack {
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()
}
return NewEcoFastItemStack(itemStack)
}
}