Registration improvements

This commit is contained in:
Will FP 2023-12-09 14:14:59 +00:00
parent 80792b8ae1
commit 250607ecb2
20 changed files with 58 additions and 199 deletions

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_17_R1
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import org.bukkit.enchantments.Enchantment
class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
@ -16,7 +17,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_18_R1
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import org.bukkit.enchantments.Enchantment
class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
@ -16,7 +17,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_18_R2
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import org.bukkit.enchantments.Enchantment
class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
@ -16,7 +17,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_19_R1
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import org.bukkit.enchantments.Enchantment
class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
@ -16,7 +17,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_19_R2
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import net.minecraft.core.registries.BuiltInRegistries
import org.bukkit.enchantments.Enchantment
@ -17,7 +18,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_19_R3
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import net.minecraft.core.registries.BuiltInRegistries
import org.bukkit.enchantments.Enchantment
@ -17,7 +18,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_20_R1
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import net.minecraft.core.registries.BuiltInRegistries
import org.bukkit.enchantments.Enchantment
@ -17,7 +18,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -20,27 +20,4 @@ class EcoCraftEnchantment(
return data.conflicts?.contains(other.key) ?: super.conflictsWith(other)
}
@Suppress("UNCHECKED_CAST")
fun register() {
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, org.bukkit.enchantments.Enchantment>).apply { remove(key) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
(get(null) as MutableMap<String, org.bukkit.enchantments.Enchantment>).apply { remove(name) }
}
org.bukkit.enchantments.Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
org.bukkit.enchantments.Enchantment.registerEnchantment(this)
}
}

View File

@ -2,6 +2,7 @@ package com.willfp.ecoenchants.proxy.v1_20_R2
import com.willfp.ecoenchants.enchant.EcoCraftEnchantmentManagerProxy
import com.willfp.ecoenchants.enchant.VanillaEnchantmentData
import com.willfp.ecoenchants.enchant.registration.legacy.LegacyEnchantmentRegisterer
import net.minecraft.core.registries.BuiltInRegistries
import org.bukkit.enchantments.Enchantment
@ -17,7 +18,8 @@ class EcoCraftEnchantmentManager : EcoCraftEnchantmentManagerProxy {
if (key.key != enchantment.key.key) {
continue
}
EcoCraftEnchantment(enchant, data).register()
LegacyEnchantmentRegisterer.registerToBukkit(EcoCraftEnchantment(enchant, data))
}
}
}

View File

@ -3,7 +3,7 @@ package com.willfp.ecoenchants.proxy.v1_20_R3
import com.willfp.ecoenchants.enchant.EcoEnchant
import com.willfp.ecoenchants.enchant.EcoEnchants
import com.willfp.ecoenchants.enchant.registration.modern.ModernEnchantmentRegistererProxy
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.DelegatedCraftEnchantment
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.EcoEnchantsCraftEnchantment
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.ModifiedVanillaCraftEnchantment
import com.willfp.ecoenchants.proxy.v1_20_R3.registration.VanillaEcoEnchantsEnchantment
import net.minecraft.core.MappedRegistry
@ -71,7 +71,7 @@ class ModernEnchantmentRegisterer : ModernEnchantmentRegistererProxy {
Registry.register(BuiltInRegistries.ENCHANTMENT, enchant.id, nms)
return DelegatedCraftEnchantment(enchant, nms)
return EcoEnchantsCraftEnchantment(enchant, nms)
}
override fun unregister(enchant: EcoEnchant) {

View File

@ -13,7 +13,7 @@ import org.bukkit.inventory.EquipmentSlot
import org.bukkit.inventory.ItemStack
import java.util.Objects
class DelegatedCraftEnchantment(
class EcoEnchantsCraftEnchantment(
private val enchant: EcoEnchant,
nmsEnchantment: Enchantment
) : CraftEnchantment(enchant.enchantmentKey, nmsEnchantment), EcoEnchant by enchant {
@ -104,7 +104,7 @@ class DelegatedCraftEnchantment(
override fun getActiveSlots() = emptySet<EquipmentSlot>()
override fun equals(other: Any?): Boolean {
return other is DelegatedCraftEnchantment &&
return other is EcoEnchantsCraftEnchantment &&
other.key == this.key
}
@ -113,6 +113,6 @@ class DelegatedCraftEnchantment(
}
override fun toString(): String {
return "DelegatedCraftEnchantment(key=$key)"
return "EcoEnchantsCraftEnchantment(key=$key)"
}
}

View File

@ -10,4 +10,9 @@ class ModifiedVanillaCraftEnchantment(
target: Enchantment
) : CraftEnchantment(key, target) {
override fun getMaxLevel(): Int = this.vanillaEnchantmentData?.maxLevel ?: super.getMaxLevel()
override fun conflictsWith(other: org.bukkit.enchantments.Enchantment): Boolean {
return this.vanillaEnchantmentData?.conflicts?.contains(other.key) ?: super.conflictsWith(other)
|| other.conflictsWith(this)
}
}

View File

@ -1,20 +1,42 @@
package com.willfp.ecoenchants.enchant.registration.legacy
import com.willfp.ecoenchants.enchant.EcoEnchant
import com.willfp.ecoenchants.enchant.impl.EcoEnchantBase
import com.willfp.ecoenchants.enchant.registration.EnchantmentRegisterer
import org.bukkit.NamespacedKey
import org.bukkit.enchantments.Enchantment
@Suppress("UNCHECKED_CAST")
object LegacyEnchantmentRegisterer : EnchantmentRegisterer {
override fun register(enchant: EcoEnchant): Enchantment {
init {
// Allow registering new enchantments
Enchantment::class.java.getDeclaredField("acceptingNew")
.apply {
isAccessible = true
set(null, true)
}
}
/**
* Register an enchantment to bukkit (for replacing vanilla CraftEnchantments)
*/
fun registerToBukkit(enchantment: Enchantment) {
Enchantment::class.java.getDeclaredField("byKey")
.apply {
isAccessible = true
(get(null) as MutableMap<NamespacedKey, Enchantment>).apply { remove(enchantment.key) }
}
Enchantment::class.java.getDeclaredField("byName")
.apply {
isAccessible = true
@Suppress("DEPRECATION")
(get(null) as MutableMap<String, Enchantment>).apply { remove(enchantment.name) }
}
Enchantment.registerEnchantment(enchantment)
}
override fun register(enchant: EcoEnchant): Enchantment {
val enchantment = LegacyDelegatedEnchantment(enchant)
Enchantment.registerEnchantment(enchantment)