diff --git a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/ModernEnchantmentRegisterer.kt b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/ModernEnchantmentRegisterer.kt index 77ffa526..628390c0 100644 --- a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/ModernEnchantmentRegisterer.kt +++ b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/ModernEnchantmentRegisterer.kt @@ -7,9 +7,15 @@ import com.willfp.ecoenchants.enchant.registration.modern.ModernEnchantmentRegis import com.willfp.ecoenchants.proxy.v1_21.registration.EcoEnchantsCraftEnchantment import com.willfp.ecoenchants.proxy.v1_21.registration.ModifiedVanillaCraftEnchantment import com.willfp.ecoenchants.proxy.v1_21.registration.vanillaEcoEnchantsEnchantment +import com.willfp.ecoenchants.setStaticFinal +import io.papermc.paper.registry.PaperRegistryAccess +import io.papermc.paper.registry.RegistryAccess +import io.papermc.paper.registry.RegistryKey +import io.papermc.paper.registry.WritableCraftRegistry import net.minecraft.core.Holder import net.minecraft.core.MappedRegistry import net.minecraft.core.Registry +import net.minecraft.core.RegistrySynchronization import net.minecraft.core.registries.Registries import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceLocation @@ -20,12 +26,17 @@ import org.bukkit.craftbukkit.CraftRegistry import org.bukkit.craftbukkit.CraftServer import org.bukkit.craftbukkit.util.CraftNamespacedKey import org.bukkit.enchantments.Enchantment +import java.util.HashMap import java.util.IdentityHashMap import java.util.function.BiFunction -val enchantmentRegistry = +private val enchantmentRegistry = (Bukkit.getServer() as CraftServer).server.registryAccess().registryOrThrow(Registries.ENCHANTMENT) +@Suppress("DEPRECATION") +private val bukkitRegistry = + org.bukkit.Registry.ENCHANTMENT + class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { private val frozenField = MappedRegistry::class.java .declaredFields @@ -37,12 +48,23 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { .last { it.type == Map::class.java } .apply { isAccessible = true } - private val minecraftToBukkit = CraftRegistry::class.java + private val minecraftToBukkit = bukkitRegistry::class.java .getDeclaredField("minecraftToBukkit") .apply { isAccessible = true } + // Paper has two minecraftToBukkit fields, one in CraftRegistry and one in WritableCraftRegistry + private val minecraftToBukkitAlt = CraftRegistry::class.java + .getDeclaredField("minecraftToBukkit") + .apply { isAccessible = true } + + private val cache = CraftRegistry::class.java + .getDeclaredField("cache") + .apply { isAccessible = true } + + @Suppress("UNCHECKED_CAST") private val vanillaEnchantments = Enchantments::class.java .declaredFields + .asSequence() .filter { it.type == ResourceKey::class.java } .map { it.get(null) as ResourceKey } .map { it.location() } @@ -65,8 +87,11 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { } // Update bukkit registry - @Suppress("DEPRECATION") - minecraftToBukkit.set(org.bukkit.Registry.ENCHANTMENT, newRegistryMTB) + minecraftToBukkit.set(bukkitRegistry, newRegistryMTB) + minecraftToBukkitAlt.set(bukkitRegistry, newRegistryMTB) + + // Clear the enchantment cache + cache.set(bukkitRegistry, mutableMapOf()) // Unfreeze NMS registry frozenField.set(enchantmentRegistry, false) @@ -78,6 +103,9 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { } override fun register(enchant: EcoEnchantBase): Enchantment { + // Clear the enchantment cache + cache.set(bukkitRegistry, mutableMapOf()) + if (enchantmentRegistry.containsKey(CraftNamespacedKey.toMinecraft(enchant.enchantmentKey))) { val nms = enchantmentRegistry[CraftNamespacedKey.toMinecraft(enchant.enchantmentKey)] @@ -88,10 +116,14 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { } } + val vanillaEnchantment = vanillaEcoEnchantsEnchantment(enchant) + + enchantmentRegistry.createIntrusiveHolder(vanillaEnchantment) + Registry.register( enchantmentRegistry, ResourceLocation.withDefaultNamespace(enchant.id), - vanillaEcoEnchantsEnchantment(enchant) + vanillaEnchantment ) return register(enchant) diff --git a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/EcoEnchantsCraftEnchantment.kt b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/EcoEnchantsCraftEnchantment.kt index 5363aabb..5db774db 100644 --- a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/EcoEnchantsCraftEnchantment.kt +++ b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/EcoEnchantsCraftEnchantment.kt @@ -37,6 +37,10 @@ class EcoEnchantsCraftEnchantment( return enchant.conflictsWith(other) } + @Deprecated( + message = "getName is a paper Spigot API", + replaceWith = ReplaceWith("this.displayName(level)") + ) override fun translationKey(): String { return "ecoenchants:enchantment.$id" } diff --git a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/VanillaEcoEnchantsEnchantment.kt b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/VanillaEcoEnchantsEnchantment.kt index cbed1129..a3bcb530 100644 --- a/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/VanillaEcoEnchantsEnchantment.kt +++ b/eco-core/core-nms/v1_21/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21/registration/VanillaEcoEnchantsEnchantment.kt @@ -9,7 +9,7 @@ fun vanillaEcoEnchantsEnchantment(enchant: EcoEnchant): Enchantment { val enchantment = Enchantment.enchantment( Enchantment.definition( HolderSet.empty(), - 0, // Weight is 0, so we can use our own rarity system + 1, // 1.21 hardcodes a minimum weight of 1 enchant.maximumLevel, Enchantment.constantCost(1), Enchantment.constantCost(1), diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt index 1761ac72..1d19b64b 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/EcoEnchantsPlugin.kt @@ -96,6 +96,10 @@ class EcoEnchantsPlugin : LibreforgePlugin() { override fun handleAfterLoad() { isLoaded = true + if (Prerequisite.HAS_1_21.isMet) { + plugin.getProxy(ModernEnchantmentRegistererProxy::class.java).replaceRegistry() + } + // Run in afterLoad to prevent items from having their enchantments deleted if (Prerequisite.HAS_1_20_5.isMet && !Prerequisite.HAS_1_21.isMet) { if (!this.configYml.getBool("enable-1-20-6")) { diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt index ee49ccac..d1e57fac 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/ecoenchants/enchant/EcoEnchants.kt @@ -19,6 +19,8 @@ import com.willfp.ecoenchants.type.EnchantmentTypes import com.willfp.libreforge.loader.LibreforgePlugin import com.willfp.libreforge.loader.configs.RegistrableCategory import org.bukkit.ChatColor +import org.bukkit.NamespacedKey +import org.bukkit.enchantments.Enchantment @Suppress("UNUSED") object EcoEnchants : RegistrableCategory("enchant", "enchants") { diff --git a/gradle.properties b/gradle.properties index 17449fb5..db9e4de6 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ #libreforge-updater #Fri May 31 20:38:06 BST 2024 kotlin.code.style=official -libreforge-version=4.60.0 -version=12.6.1 +libreforge-version=4.59.1 +version=12.7.0