Fixed enchantment registry issues
This commit is contained in:
@@ -17,6 +17,7 @@ import net.kyori.adventure.text.Component
|
|||||||
import net.kyori.adventure.text.format.Style
|
import net.kyori.adventure.text.format.Style
|
||||||
import net.kyori.adventure.text.format.TextColor
|
import net.kyori.adventure.text.format.TextColor
|
||||||
import net.kyori.adventure.text.format.TextDecoration
|
import net.kyori.adventure.text.format.TextDecoration
|
||||||
|
import net.minecraft.core.Holder
|
||||||
import net.minecraft.core.component.DataComponentType
|
import net.minecraft.core.component.DataComponentType
|
||||||
import net.minecraft.core.component.DataComponents
|
import net.minecraft.core.component.DataComponents
|
||||||
import net.minecraft.core.registries.Registries
|
import net.minecraft.core.registries.Registries
|
||||||
@@ -47,7 +48,6 @@ private fun Component.unstyled(): Component {
|
|||||||
|
|
||||||
open class NewEcoFastItemStack(
|
open class NewEcoFastItemStack(
|
||||||
private val bukkit: ItemStack,
|
private val bukkit: ItemStack,
|
||||||
private val registryAccessor: RegistryAccessor
|
|
||||||
) : ImplementedFIS {
|
) : ImplementedFIS {
|
||||||
// Cast is there because, try as I might, I can't get IntellIJ to recognise half the classes in the dev bundle
|
// 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")
|
@Suppress("USELESS_CAST")
|
||||||
@@ -83,20 +83,17 @@ open class NewEcoFastItemStack(
|
|||||||
enchantment: Enchantment,
|
enchantment: Enchantment,
|
||||||
checkStored: Boolean
|
checkStored: Boolean
|
||||||
): Int {
|
): Int {
|
||||||
val minecraft =
|
val minecraft = CraftRegistry.bukkitToMinecraftHolder(
|
||||||
CraftRegistry.bukkitToMinecraft<Enchantment, net.minecraft.world.item.enchantment.Enchantment>(
|
enchantment,
|
||||||
enchantment
|
Registries.ENCHANTMENT
|
||||||
)
|
)
|
||||||
|
|
||||||
val registry = registryAccessor.getRegistry(Registries.ENCHANTMENT)
|
|
||||||
val holder = registry.wrapAsHolder(minecraft)
|
|
||||||
|
|
||||||
val enchantments = handle.get(DataComponents.ENCHANTMENTS) ?: return 0
|
val enchantments = handle.get(DataComponents.ENCHANTMENTS) ?: return 0
|
||||||
var level = enchantments.getLevel(holder)
|
var level = enchantments.getLevel(minecraft)
|
||||||
|
|
||||||
if (checkStored) {
|
if (checkStored) {
|
||||||
val storedEnchantments = handle.get(DataComponents.STORED_ENCHANTMENTS) ?: return 0
|
val storedEnchantments = handle.get(DataComponents.STORED_ENCHANTMENTS) ?: return 0
|
||||||
level = max(level, storedEnchantments.getLevel(holder))
|
level = max(level, storedEnchantments.getLevel(minecraft))
|
||||||
}
|
}
|
||||||
|
|
||||||
return level
|
return level
|
||||||
|
|||||||
@@ -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>
|
|
||||||
}
|
|
||||||
@@ -3,7 +3,6 @@ package com.willfp.eco.internal.spigot.proxy.v1_21
|
|||||||
import com.willfp.eco.core.fast.FastItemStack
|
import com.willfp.eco.core.fast.FastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
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.NewEcoFastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor
|
|
||||||
import net.minecraft.core.Registry
|
import net.minecraft.core.Registry
|
||||||
import net.minecraft.core.component.DataComponents
|
import net.minecraft.core.component.DataComponents
|
||||||
import net.minecraft.core.registries.Registries
|
import net.minecraft.core.registries.Registries
|
||||||
@@ -19,7 +18,7 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private class LegacyNewEcoFastItemStack(itemStack: ItemStack) :
|
private class LegacyNewEcoFastItemStack(itemStack: ItemStack) :
|
||||||
NewEcoFastItemStack(itemStack, LegacyRegistryAccessor) {
|
NewEcoFastItemStack(itemStack) {
|
||||||
|
|
||||||
override fun getCustomModelData(): Int? =
|
override fun getCustomModelData(): Int? =
|
||||||
handle.get(DataComponents.CUSTOM_MODEL_DATA)?.value
|
handle.get(DataComponents.CUSTOM_MODEL_DATA)?.value
|
||||||
@@ -34,12 +33,4 @@ class FastItemStackFactory : FastItemStackFactoryProxy {
|
|||||||
apply()
|
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.core.fast.FastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
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.NewEcoFastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor
|
|
||||||
import net.minecraft.core.Registry
|
import net.minecraft.core.Registry
|
||||||
import net.minecraft.resources.ResourceKey
|
import net.minecraft.resources.ResourceKey
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
@@ -12,14 +11,6 @@ import org.bukkit.inventory.ItemStack
|
|||||||
|
|
||||||
class FastItemStackFactory : FastItemStackFactoryProxy {
|
class FastItemStackFactory : FastItemStackFactoryProxy {
|
||||||
override fun create(itemStack: ItemStack): FastItemStack {
|
override fun create(itemStack: ItemStack): FastItemStack {
|
||||||
return NewEcoFastItemStack(itemStack, ModernRegistryAccessor)
|
return NewEcoFastItemStack(itemStack)
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.core.fast.FastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.FastItemStackFactoryProxy
|
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.NewEcoFastItemStack
|
||||||
import com.willfp.eco.internal.spigot.proxy.common.modern.RegistryAccessor
|
|
||||||
import net.minecraft.core.Registry
|
import net.minecraft.core.Registry
|
||||||
|
import net.minecraft.core.registries.Registries
|
||||||
import net.minecraft.resources.ResourceKey
|
import net.minecraft.resources.ResourceKey
|
||||||
import org.bukkit.Bukkit
|
import org.bukkit.Bukkit
|
||||||
import org.bukkit.craftbukkit.CraftServer
|
import org.bukkit.craftbukkit.CraftServer
|
||||||
@@ -12,14 +12,6 @@ import org.bukkit.inventory.ItemStack
|
|||||||
|
|
||||||
class FastItemStackFactory : FastItemStackFactoryProxy {
|
class FastItemStackFactory : FastItemStackFactoryProxy {
|
||||||
override fun create(itemStack: ItemStack): FastItemStack {
|
override fun create(itemStack: ItemStack): FastItemStack {
|
||||||
return NewEcoFastItemStack(itemStack, ModernRegistryAccessor)
|
return NewEcoFastItemStack(itemStack)
|
||||||
}
|
|
||||||
|
|
||||||
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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user