Register new enchantment on custom anvil reload.

This commit is contained in:
alexcrea 2024-07-24 15:55:18 +02:00
parent e48eb95aa8
commit 851489068d
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
3 changed files with 37 additions and 3 deletions

View file

@ -51,8 +51,8 @@ class ReloadExecutor : CommandExecutor {
// temporary: handle 1.21 update
Update_1_21.handleUpdate()
// Register enchantment of compatible plugin and load configuration change.
DependencyManager.handleCompatibilityConfig()
// Handle dependency reload
DependencyManager.handleConfigReload()
// Call event
val configReadyEvent = CAConfigReadyEvent()

View file

@ -44,4 +44,13 @@ object DependencyManager {
}
fun handleConfigReload(){
// Register enchantment of compatible plugin and load configuration change.
handleCompatibilityConfig()
// Then handle plugin reload
ecoEnchantCompatibility?.handleConfigReload()
}
}

View file

@ -1,5 +1,6 @@
package xyz.alexcrea.cuanvil.dependency
import com.willfp.ecoenchants.enchant.EcoEnchant
import com.willfp.ecoenchants.enchant.EcoEnchants
import io.delilaheve.CustomAnvil
import org.bukkit.event.inventory.PrepareAnvilEvent
@ -17,15 +18,39 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin)
}
private var ecoEnchantOldEnchantments: MutableSet<EcoEnchant>? = null
fun registerEnchantments() {
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
for (ecoEnchant in EcoEnchants.values()) {
val enchantments = EcoEnchants.values()
for (ecoEnchant in enchantments) {
EnchantmentApi.unregisterEnchantment(ecoEnchant.enchantment) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
}
ecoEnchantOldEnchantments = HashSet(enchantments)
CustomAnvil.instance.logger.info("Eco Enchant should now work as expected !")
}
fun handleConfigReload() {
// Should not happen in known case.
if(this.ecoEnchantOldEnchantments == null) return
val newEnchantments = EcoEnchants.values()
// Add new enchantments
for (ecoEnchant in newEnchantments)
if(!this.ecoEnchantOldEnchantments!!.contains(ecoEnchant))
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
// Remove old enchantments that not now currently used
this.ecoEnchantOldEnchantments!!.removeAll(newEnchantments)
for (oldEnchantment in this.ecoEnchantOldEnchantments!!) {
EnchantmentApi.unregisterEnchantment(oldEnchantment.enchantment)
}
this.ecoEnchantOldEnchantments = HashSet(newEnchantments)
}
}