From c0af35fa0bb1b951392db5b49bd5438472ab6dfd Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 18 Oct 2024 17:43:33 +0200 Subject: [PATCH] Fix enchantments avoiding custom anvil rules when an ExcellentEnchants enchantment is present. fix #36 Also version up --- build.gradle.kts | 2 +- src/main/kotlin/io/delilaheve/CustomAnvil.kt | 2 +- .../cuanvil/dependency/DependencyManager.kt | 5 ++++ .../dependency/ExcellentEnchantsDependency.kt | 23 +++++++++++-------- .../cuanvil/listener/PrepareAnvilListener.kt | 4 ++++ 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8dafea2..dac932e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -15,7 +15,7 @@ plugins { } group = "xyz.alexcrea" -version = "1.6.5" +version = "1.6.6" repositories { // EcoEnchants diff --git a/src/main/kotlin/io/delilaheve/CustomAnvil.kt b/src/main/kotlin/io/delilaheve/CustomAnvil.kt index 285e1c5..442af0a 100644 --- a/src/main/kotlin/io/delilaheve/CustomAnvil.kt +++ b/src/main/kotlin/io/delilaheve/CustomAnvil.kt @@ -28,7 +28,7 @@ import java.util.logging.Level /** * Bukkit/Spigot/Paper plugin to alter anvil feature */ -class CustomAnvil : JavaPlugin() { +open class CustomAnvil : JavaPlugin() { companion object { // bstats plugin id diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt index 694f314..4aa69db 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt @@ -5,6 +5,7 @@ import org.bukkit.Bukkit import org.bukkit.event.inventory.InventoryClickEvent import org.bukkit.event.inventory.PrepareAnvilEvent import org.bukkit.inventory.AnvilInventory +import org.bukkit.inventory.ItemStack import xyz.alexcrea.cuanvil.config.ConfigHolder import xyz.alexcrea.cuanvil.dependency.gui.ExternGuiTester import xyz.alexcrea.cuanvil.dependency.gui.GuiTesterSelector @@ -105,6 +106,10 @@ object DependencyManager { return bypass } + fun treatAnvilResult(event: PrepareAnvilEvent, result: ItemStack) { + excellentEnchantsCompatibility?.treatAnvilResult(event, result) + } + fun tryClickAnvilResultBypass(event: InventoryClickEvent, inventory: AnvilInventory): Boolean { var bypass = false diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/ExcellentEnchantsDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/ExcellentEnchantsDependency.kt index 437cd75..2613b2a 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/ExcellentEnchantsDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/ExcellentEnchantsDependency.kt @@ -82,16 +82,17 @@ class ExcellentEnchantsDependency { if(event.result == null) return true } - var first: ItemStack? = event.inventory.getItem(0) - var second: ItemStack? = event.inventory.getItem(1) - var result = event.result + val first: ItemStack = treatInput(event.inventory.getItem(0)) + val second: ItemStack = treatInput(event.inventory.getItem(1)) - if (first == null) first = ItemStack(Material.AIR) - if (second == null) second = ItemStack(Material.AIR) - if (result == null) result = ItemStack(Material.AIR) + return handleRechargeMethod.invoke(this.anvilListener, event, first, second) as Boolean + } + + fun treatAnvilResult(event: PrepareAnvilEvent, result: ItemStack) { + val first: ItemStack = treatInput(event.inventory.getItem(0)) + val second: ItemStack = treatInput(event.inventory.getItem(1)) - if(handleRechargeMethod.invoke(this.anvilListener, event, first, second) as Boolean) return true - return handleCombineMethod.invoke(this.anvilListener, event, first, second, result) as Boolean + handleCombineMethod.invoke(this.anvilListener, event, first, second, result) } fun testAnvilResult(event: InventoryClickEvent): Any { @@ -100,8 +101,12 @@ class ExcellentEnchantsDependency { return event.inventory.getItem(2) == null } - return false; } + fun treatInput(item: ItemStack?): ItemStack { + if(item == null) return ItemStack(Material.AIR) + return item + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index 57de82f..b1ec4b9 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -86,6 +86,7 @@ class PrepareAnvilListener : Listener { resultItem.amount *= amount event.result = resultItem + DependencyManager.treatAnvilResult(event, resultItem) AnvilXpUtil.setAnvilInvXp(inventory, event.view, recipe.xpCostPerCraft * amount, true) return true @@ -104,6 +105,7 @@ class PrepareAnvilListener : Listener { } event.result = resultItem + DependencyManager.treatAnvilResult(event, resultItem) anvilCost += AnvilXpUtil.calculatePenalty(first, null, resultItem) @@ -173,6 +175,7 @@ class PrepareAnvilListener : Listener { // Finally, we set result event.result = resultItem + DependencyManager.treatAnvilResult(event, resultItem) AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost) } @@ -198,6 +201,7 @@ class PrepareAnvilListener : Listener { return true } event.result = resultItem + DependencyManager.treatAnvilResult(event, resultItem) AnvilXpUtil.setAnvilInvXp(inventory, event.view, anvilCost) return true