From 49e3cc02b5e90992069b2a1c191c3a165e34a7f1 Mon Sep 17 00:00:00 2001 From: Will FP Date: Mon, 20 Jan 2025 15:05:04 +0000 Subject: [PATCH] Fixed enchantment registration on 1.21.4 --- build.gradle.kts | 2 +- eco-core/core-nms/build.gradle.kts | 2 +- .../v1_21_4/ModernEnchantmentRegisterer.kt | 26 ++++++++++++------- .../EcoEnchantsCraftEnchantment.kt | 6 +++-- .../ModifiedVanillaCraftEnchantment.kt | 8 +++--- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index cc617d7d..0025c284 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -55,7 +55,7 @@ allprojects { } dependencies { - compileOnly("com.willfp:eco:6.74.3") + compileOnly("com.willfp:eco:6.75.0") compileOnly("org.jetbrains:annotations:23.0.0") compileOnly("org.jetbrains.kotlin:kotlin-stdlib:2.1.0") compileOnly("com.github.ben-manes.caffeine:caffeine:3.1.5") diff --git a/eco-core/core-nms/build.gradle.kts b/eco-core/core-nms/build.gradle.kts index efd95c86..58030033 100644 --- a/eco-core/core-nms/build.gradle.kts +++ b/eco-core/core-nms/build.gradle.kts @@ -1,5 +1,5 @@ plugins { - id("io.papermc.paperweight.userdev") version "2.0.0-beta.8" apply false + id("io.papermc.paperweight.userdev") version "2.0.0-beta.14" apply false } group = "com.willfp" diff --git a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/ModernEnchantmentRegisterer.kt b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/ModernEnchantmentRegisterer.kt index 19a7c565..8dfdf04f 100644 --- a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/ModernEnchantmentRegisterer.kt +++ b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/ModernEnchantmentRegisterer.kt @@ -7,8 +7,8 @@ import com.willfp.ecoenchants.enchant.registration.modern.ModernEnchantmentRegis import com.willfp.ecoenchants.proxy.v1_21_4.registration.EcoEnchantsCraftEnchantment import com.willfp.ecoenchants.proxy.v1_21_4.registration.ModifiedVanillaCraftEnchantment import com.willfp.ecoenchants.proxy.v1_21_4.registration.vanillaEcoEnchantsEnchantment -import io.papermc.paper.registry.WritableCraftRegistry import io.papermc.paper.registry.entry.RegistryTypeMapper +import io.papermc.paper.registry.legacy.DelayedRegistry import net.minecraft.core.Holder import net.minecraft.core.MappedRegistry import net.minecraft.core.Registry @@ -23,13 +23,15 @@ import org.bukkit.enchantments.Enchantment import java.lang.reflect.Modifier import java.util.IdentityHashMap import java.util.function.BiFunction +import javax.annotation.Nullable private val enchantmentRegistry = (Bukkit.getServer() as CraftServer).server.registryAccess().lookupOrThrow(Registries.ENCHANTMENT) @Suppress("DEPRECATION") -private val bukkitRegistry = - org.bukkit.Registry.ENCHANTMENT +private val bukkitRegistry: org.bukkit.Registry + get() = + (org.bukkit.Registry.ENCHANTMENT as DelayedRegistry).delegate() class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { private val frozenField = MappedRegistry::class.java @@ -44,7 +46,8 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { private val unregisteredIntrusiveHoldersField = MappedRegistry::class.java .declaredFields - .last { it.type == Map::class.java } + .filter { it.type == Map::class.java } + .filter { it.isAnnotationPresent(Nullable::class.java) }[0] .apply { isAccessible = true } // 1.21.4+ only has minecraftToBukkit in CraftRegistry, removing the duplicate in WritableCraftRegistry @@ -60,12 +63,14 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { val newRegistryMTB = BiFunction { key, registry -> val eco = EcoEnchants.getByID(key.key) - val registered = enchantmentRegistry.containsKey(CraftNamespacedKey.toMinecraft(key)) + val isRegistered = enchantmentRegistry.containsKey(CraftNamespacedKey.toMinecraft(key)) if (eco != null) { eco as Enchantment - } else if (registered) { - ModifiedVanillaCraftEnchantment(key, registry) + } else if (isRegistered) { + val holder = enchantmentRegistry.get(CraftNamespacedKey.toMinecraft(key)).get() + + ModifiedVanillaCraftEnchantment(key, registry, holder) } else { null } @@ -75,7 +80,10 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { // The nasty casting hack is because of some weird nullability changes, if I set the BiFunction to have a // non-nullable bukkit enchantment type then it refuses to build, some sort of K2 compiler change. @Suppress("UNCHECKED_CAST") - minecraftToBukkit.set(bukkitRegistry, RegistryTypeMapper(newRegistryMTB as BiFunction)) + minecraftToBukkit.set( + bukkitRegistry, + RegistryTypeMapper(newRegistryMTB as BiFunction) + ) // Clear the enchantment cache cache.set(bukkitRegistry, mutableMapOf()) @@ -112,7 +120,7 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy { val nms = enchantmentRegistry[CraftNamespacedKey.toMinecraft(enchant.enchantmentKey)] if (nms.isPresent) { - return EcoEnchantsCraftEnchantment(enchant, nms.get().value()) + return EcoEnchantsCraftEnchantment(enchant, nms.get()) } else { throw IllegalStateException("Enchantment ${enchant.id} wasn't registered") } diff --git a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/EcoEnchantsCraftEnchantment.kt b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/EcoEnchantsCraftEnchantment.kt index 55362a55..f5456214 100644 --- a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/EcoEnchantsCraftEnchantment.kt +++ b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/EcoEnchantsCraftEnchantment.kt @@ -5,15 +5,17 @@ import com.willfp.ecoenchants.display.getFormattedName import com.willfp.ecoenchants.enchant.EcoEnchant import com.willfp.ecoenchants.enchant.impl.EcoEnchantBase import net.kyori.adventure.text.Component +import net.minecraft.core.Holder import net.minecraft.world.item.enchantment.Enchantment +import org.bukkit.NamespacedKey import org.bukkit.craftbukkit.enchantments.CraftEnchantment import org.bukkit.enchantments.EnchantmentTarget import org.bukkit.inventory.ItemStack class EcoEnchantsCraftEnchantment( private val enchant: EcoEnchantBase, - nmsEnchantment: Enchantment -) : CraftEnchantment(enchant.enchantmentKey, nmsEnchantment), EcoEnchant by enchant { + holder: Holder +) : CraftEnchantment(holder), EcoEnchant by enchant { init { enchant.enchantment = this } diff --git a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/ModifiedVanillaCraftEnchantment.kt b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/ModifiedVanillaCraftEnchantment.kt index 77c86a15..27df1e45 100644 --- a/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/ModifiedVanillaCraftEnchantment.kt +++ b/eco-core/core-nms/v1_21_4/src/main/kotlin/com/willfp/ecoenchants/proxy/v1_21_4/registration/ModifiedVanillaCraftEnchantment.kt @@ -1,14 +1,16 @@ package com.willfp.ecoenchants.proxy.v1_21_4.registration import com.willfp.ecoenchants.enchant.vanillaEnchantmentData +import net.minecraft.core.Holder import net.minecraft.world.item.enchantment.Enchantment import org.bukkit.NamespacedKey import org.bukkit.craftbukkit.enchantments.CraftEnchantment class ModifiedVanillaCraftEnchantment( - key: NamespacedKey, - target: Enchantment -) : CraftEnchantment(key, target) { + private val key: NamespacedKey, + target: Enchantment, + holder: Holder +) : CraftEnchantment(holder) { override fun getMaxLevel(): Int = this.vanillaEnchantmentData?.maxLevel ?: super.getMaxLevel() override fun conflictsWith(other: org.bukkit.enchantments.Enchantment): Boolean {