Some changes for this version:
- Tried to make compatible with some legacy version of eco enchant (not assumed to work)
- Fix some enchantment test from other plugin not being taken into account. 
Should work as expected now, but that mean it may not work as currently.
- Made Heaven bag works
- Updated Disenchantment, should work with disenchantment version >= 6.1.0. But break support for previous versions
This commit is contained in:
alexcrea 2025-01-20 18:20:46 +01:00 committed by GitHub
parent 4147f018a9
commit 5f557e3d49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 503 additions and 102 deletions

View file

@ -28,13 +28,14 @@ object DependencyManager {
var excellentEnchantsCompatibility: ExcellentEnchantsDependency? = null
var disenchantmentCompatibility: DisenchantmentDependency? = null
var havenBagsCompatibility: HavenBagsDependency? = null
fun loadDependency(){
fun loadDependency() {
val pluginManager = Bukkit.getPluginManager()
// Bukkit or Paper scheduler ?
isFolia = testIsFolia()
scheduler = if(isFolia) {
scheduler = if (isFolia) {
CustomAnvil.instance.logger.info("Folia detected... Custom Anvil Folia support is experimental. issues are more likely to happens.")
FoliaScheduler()
@ -46,29 +47,35 @@ object DependencyManager {
externGuiTester = GuiTesterSelector.selectGuiTester
// Enchantment Squared dependency
if(pluginManager.isPluginEnabled("EnchantsSquared")){
if (pluginManager.isPluginEnabled("EnchantsSquared")) {
enchantmentSquaredCompatibility = EnchantmentSquaredDependency(pluginManager.getPlugin("EnchantsSquared")!!)
enchantmentSquaredCompatibility!!.disableAnvilListener()
}
// EcoEnchants dependency
if(pluginManager.isPluginEnabled("EcoEnchants")){
if (pluginManager.isPluginEnabled("EcoEnchants")) {
ecoEnchantCompatibility = EcoEnchantDependency(pluginManager.getPlugin("EcoEnchants")!!)
ecoEnchantCompatibility!!.disableAnvilListener()
}
// Excellent Enchants dependency
if(pluginManager.isPluginEnabled("ExcellentEnchants")){
if (pluginManager.isPluginEnabled("ExcellentEnchants")) {
excellentEnchantsCompatibility = ExcellentEnchantsDependency()
excellentEnchantsCompatibility!!.redirectListeners()
}
// Disenchantment dependency
if(pluginManager.isPluginEnabled("Disenchantment")){
if (pluginManager.isPluginEnabled("Disenchantment")) {
disenchantmentCompatibility = DisenchantmentDependency()
disenchantmentCompatibility!!.redirectListeners()
}
// HavenBags dependency
if (pluginManager.isPluginEnabled("HavenBags")) {
havenBagsCompatibility = HavenBagsDependency()
havenBagsCompatibility!!.redirectListeners()
}
}
fun handleCompatibilityConfig() {
@ -83,7 +90,7 @@ object DependencyManager {
}
fun handleConfigReload(){
fun handleConfigReload() {
// Register enchantment of compatible plugin and load configuration change.
handleCompatibilityConfig()
@ -95,14 +102,18 @@ object DependencyManager {
fun tryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
var bypass = false
// Test if disenchantment used special prepare anvil
if(disenchantmentCompatibility?.testPrepareAnvil(event, player) == true) bypass = true
// Test if disenchantment used prepare anvil
if (disenchantmentCompatibility?.testPrepareAnvil(event, player) == true) bypass = true
// Test excellent enchantments used special prepare anvil
if(!bypass && (excellentEnchantsCompatibility?.testPrepareAnvil(event) == true)) bypass = true
// Test heaven bags used prepare anvil
if (!bypass && (havenBagsCompatibility?.testPrepareAnvil(event, player) == true)) bypass = true
// Test excellent enchantments used prepare anvil
if (!bypass && (excellentEnchantsCompatibility?.testPrepareAnvil(event) == true)) bypass = true
// Test if the inventory is a gui(version specific)
if(!bypass && (externGuiTester?.testIfGui(event.view) == true)) bypass = true
if (!bypass && (externGuiTester?.testIfGui(event.view) == true)) bypass = true
return bypass
}
@ -114,14 +125,17 @@ object DependencyManager {
fun tryClickAnvilResultBypass(event: InventoryClickEvent, inventory: AnvilInventory): Boolean {
var bypass = false
// Test if disenchantment used special event click
if(disenchantmentCompatibility?.testAnvilResult(event, inventory) == true) bypass = true
// Test if disenchantment used event click
if (disenchantmentCompatibility?.testAnvilResult(event, inventory) == true) bypass = true
// Test if disenchantment used special event click
if(!bypass && (excellentEnchantsCompatibility?.testAnvilResult(event) == true)) bypass = true
// Test if haven bag used event click
if (!bypass && (havenBagsCompatibility?.testAnvilResult(event, inventory) == true)) bypass = true
// Test if disenchantment used event click
if (!bypass && (excellentEnchantsCompatibility?.testAnvilResult(event) == true)) bypass = true
// Test if the inventory is a gui(version specific)
if(!bypass && (externGuiTester?.testIfGui(event.view) == true)) bypass = true
if (!bypass && (externGuiTester?.testIfGui(event.view) == true)) bypass = true
return bypass
}

View file

@ -1,9 +1,9 @@
package xyz.alexcrea.cuanvil.dependency
import cz.kominekjan.disenchantment.events.DisenchantClickEvent
import cz.kominekjan.disenchantment.events.DisenchantEvent
import cz.kominekjan.disenchantment.events.ShatterClickEvent
import cz.kominekjan.disenchantment.events.ShatterEvent
import com.jankominek.disenchantment.events.DisenchantClickEvent
import com.jankominek.disenchantment.events.DisenchantEvent
import com.jankominek.disenchantment.events.ShatterClickEvent
import com.jankominek.disenchantment.events.ShatterEvent
import io.delilaheve.CustomAnvil
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent

View file

@ -10,11 +10,29 @@ import xyz.alexcrea.cuanvil.enchant.wrapped.CAEcoEnchant
class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
private val isLegacy: Boolean
private val legacyDependency: LegacyEcoEnchantDependency?
init {
CustomAnvil.instance.logger.info("Eco Enchant Detected !")
var isLegacy = true
try {
Class.forName("com.willfp.ecoenchants.enchant.EcoEnchants")
isLegacy = false
} catch (_: ClassNotFoundException) {
}
this.isLegacy = isLegacy;
if (isLegacy) {
this.legacyDependency = LegacyEcoEnchantDependency()
} else {
this.legacyDependency = null
}
}
fun disableAnvilListener(){
fun disableAnvilListener() {
PrepareAnvilEvent.getHandlerList().unregister(this.ecoEnchantPlugin)
}
@ -22,6 +40,11 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
fun registerEnchantments() {
CustomAnvil.instance.logger.info("Preparing Eco Enchant compatibility...")
if (isLegacy) {
legacyDependency!!.registerEnchantments();
return
}
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.
@ -34,14 +57,19 @@ class EcoEnchantDependency(private val ecoEnchantPlugin: Plugin) {
}
fun handleConfigReload() {
if (isLegacy) {
legacyDependency!!.handleConfigReload()
return
}
// Should not happen in known case.
if(this.ecoEnchantOldEnchantments == null) return
if (this.ecoEnchantOldEnchantments == null) return
val newEnchantments = EcoEnchants.values()
// Add new enchantments
for (ecoEnchant in newEnchantments)
if(!this.ecoEnchantOldEnchantments!!.contains(ecoEnchant))
if (!this.ecoEnchantOldEnchantments!!.contains(ecoEnchant))
EnchantmentApi.registerEnchantment(CAEcoEnchant(ecoEnchant))
// Remove old enchantments that not now currently used

View file

@ -0,0 +1,84 @@
package xyz.alexcrea.cuanvil.dependency
import io.delilaheve.CustomAnvil
import org.bukkit.entity.HumanEntity
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.event.inventory.PrepareAnvilEvent
import org.bukkit.inventory.AnvilInventory
import org.bukkit.plugin.RegisteredListener
import valorless.havenbags.BagSkin
import valorless.havenbags.BagUpgrade
import valorless.havenbags.HavenBags
import xyz.alexcrea.cuanvil.listener.PrepareAnvilListener
import xyz.alexcrea.cuanvil.util.AnvilXpUtil
class HavenBagsDependency {
init {
CustomAnvil.instance.logger.info("Heaven Bags Detected !")
}
private lateinit var bagUpgrade: BagUpgrade
private lateinit var bagSkin: BagSkin
fun redirectListeners() {
val toUnregister = ArrayList<RegisteredListener>()
// get required PrepareAnvilEvent listener
for (registeredListener in PrepareAnvilEvent.getHandlerList().registeredListeners) {
val listener = registeredListener.listener
if (listener is BagUpgrade) {
bagUpgrade = listener
toUnregister.add(registeredListener)
}
if (listener is BagSkin) {
bagSkin = listener
toUnregister.add(registeredListener)
}
}
for (listener in toUnregister) {
PrepareAnvilEvent.getHandlerList().unregister(listener)
InventoryClickEvent.getHandlerList().unregister(listener)
}
}
fun testPrepareAnvil(event: PrepareAnvilEvent, player: HumanEntity): Boolean {
val previousResult = event.result
event.result = null
// Test if event change the result
bagSkin.onPrepareAnvil(event)
if (event.result != null) {
CustomAnvil.log("Detected pre anvil heaven bag anvil skin.")
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
return true
}
bagUpgrade.onPrepareAnvil(event)
if (event.result != null) {
CustomAnvil.log("Detected pre anvil heaven bag anvil upgrade.")
AnvilXpUtil.setAnvilInvXp(event.inventory, event.view, player, event.inventory.repairCost)
return true
}
event.result = previousResult
return false
}
fun testAnvilResult(event: InventoryClickEvent, inventory: AnvilInventory): Boolean {
val result = inventory.getItem(PrepareAnvilListener.ANVIL_OUTPUT_SLOT)?.clone()
if (HavenBags.IsBag(result)) {
CustomAnvil.log("Detected anvil click haven bag bypass.")
bagUpgrade.onInventoryClick(event)
bagSkin.onInventoryClick(event)
return true
}
return false
}
}

View file

@ -0,0 +1,46 @@
package xyz.alexcrea.cuanvil.dependency
import com.willfp.ecoenchants.enchantments.EcoEnchant
import com.willfp.ecoenchants.enchantments.EcoEnchants
import org.bukkit.enchantments.Enchantment
import xyz.alexcrea.cuanvil.api.EnchantmentApi
import xyz.alexcrea.cuanvil.enchant.wrapped.CALegacyEcoEnchant
class LegacyEcoEnchantDependency {
private var ecoEnchantOldEnchantments: MutableSet<EcoEnchant>? = null
fun registerEnchantments() {
val enchantments = EcoEnchants.values()
for (ecoEnchant in enchantments) {
ecoEnchant as Enchantment
EnchantmentApi.unregisterEnchantment(ecoEnchant) // As eco enchants is loaded before custom anvil and register enchantment to registry, we need to unregister old "vanilla" enchant.
EnchantmentApi.registerEnchantment(CALegacyEcoEnchant(ecoEnchant, ecoEnchant))
}
ecoEnchantOldEnchantments = HashSet(enchantments)
}
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(CALegacyEcoEnchant(ecoEnchant, ecoEnchant as Enchantment))
// Remove old enchantments that not now currently used
this.ecoEnchantOldEnchantments!!.removeAll(newEnchantments)
for (oldEnchantment in this.ecoEnchantOldEnchantments!!) {
EnchantmentApi.unregisterEnchantment(oldEnchantment as Enchantment)
}
this.ecoEnchantOldEnchantments = HashSet(newEnchantments)
}
}

View file

@ -191,7 +191,7 @@ class EnchantConflictManager {
}
val immutableEnchants = Collections.unmodifiableMap(appliedEnchants)
for (appliedEnchant in appliedEnchants) {
for (appliedEnchant in appliedEnchants.keys) {
if(appliedEnchant is AdditionalTestEnchantment){
val doConflict = appliedEnchant.isEnchantConflict(immutableEnchants, mat)
if(doConflict){