diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt index df2fbce..523f99d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt @@ -5,10 +5,12 @@ import io.delilaheve.CustomAnvil import net.kyori.adventure.text.Component import org.bukkit.Bukkit import org.bukkit.ChatColor +import org.bukkit.command.CommandSender 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.inventory.Inventory import org.bukkit.inventory.ItemStack import xyz.alexcrea.cuanvil.api.event.listener.CAClickResultBypassEvent import xyz.alexcrea.cuanvil.api.event.listener.CAEarlyPreAnvilBypassEvent @@ -46,6 +48,8 @@ object DependencyManager { var axPlayerWarpsCompatibility: AxPlayerWarpsDependency? = null + var itemsAdderCompatibility: ItemsAdderDependency? = null + val genericDependencies = ArrayList() fun loadDependency() { @@ -98,6 +102,12 @@ object DependencyManager { axPlayerWarpsCompatibility = AxPlayerWarpsDependency() } + if (pluginManager.isPluginEnabled("ItemsAdder")){ + val dependency = ItemsAdderDependency(pluginManager.getPlugin("ItemsAdder")!!) + itemsAdderCompatibility = dependency + genericDependencies.add(dependency) + } + // "Generic" dependencies if (pluginManager.isPluginEnabled("ToolStats")) genericDependencies.add(ToolStatsDependency(pluginManager.getPlugin("ToolStats")!!)) @@ -138,27 +148,35 @@ object DependencyManager { ecoEnchantCompatibility?.handleConfigReload() } + private fun logException(target: CommandSender, e: Exception) { + CustomAnvil.instance.logger.log( + Level.SEVERE, + "Error while trying to handle custom anvil supported plugin: ", + e + ) + trackError(e) + + // Finally, warn the player + target.sendMessage( + "[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " + + ChatColor.RED.toString() + "Error while handling the anvil." + ) + } + + private fun logExceptionAndClear(target: CommandSender, inventory: Inventory, e: Exception) { + // Just in case to avoid illegal items + inventory.setItem(ANVIL_OUTPUT_SLOT, null) + + logException(target, e) + } + // Return true if should bypass (either by a dependency or error) // called before immutability test fun earlyTryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean { try { return earlyUnsafeTryEventPreAnvilBypass(event, player) } catch (e: Exception) { - CustomAnvil.instance.logger.log( - Level.SEVERE, - "Error while trying to handle custom anvil supported plugin: ", - e - ) - trackError(e) - - // Just in case to avoid illegal items - event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) - - // Finally, warn the player, maybe a lot of time but better warn than do nothing - event.view.player.sendMessage( - "[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " + - ChatColor.RED.toString() + "Error while handling the anvil." - ) + logExceptionAndClear(event.view.player, event.inventory, e) return true } } @@ -184,21 +202,7 @@ object DependencyManager { try { return unsafeTryEventPreAnvilBypass(event, player) } catch (e: Exception) { - CustomAnvil.instance.logger.log( - Level.SEVERE, - "Error while trying to handle custom anvil supported plugin: ", - e - ) - trackError(e) - - // Just in case to avoid illegal items - event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) - - // Finally, warn the player, maybe a lot of time but better warn than do nothing - event.view.player.sendMessage( - "[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " + - ChatColor.RED.toString() + "Error while handling the anvil." - ) + logExceptionAndClear(event.view.player, event.inventory, e) return true } } @@ -238,21 +242,7 @@ object DependencyManager { unsafeTryTreatAnvilResult(treatEvent) return treatEvent; } catch (e: Exception) { - CustomAnvil.instance.logger.log( - Level.SEVERE, - "Error while trying to handle custom anvil supported plugin: ", - e - ) - trackError(e) - - // Just in case to avoid illegal items - event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) - - // Finally, warn the player, maybe a lot of time but better warn than do nothing - event.view.player.sendMessage( - "[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " + - ChatColor.RED.toString() + "Error while handling the anvil." - ) + logExceptionAndClear(event.view.player, event.inventory, e) return null } } @@ -268,21 +258,7 @@ object DependencyManager { try { return unsafeTryClickAnvilResultBypass(event, inventory) } catch (e: Exception) { - CustomAnvil.instance.logger.log( - Level.SEVERE, - "Error while trying to handle custom anvil supported plugin: ", - e - ) - trackError(e) - - // Just in case to avoid illegal items - event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) - - // Finally, warn the player, maybe a lot of time but better warn than do nothing - event.whoClicked.sendMessage( - "[" + ChatColor.YELLOW.toString() + "CustomAnvil" + ChatColor.WHITE.toString() + "] " + - ChatColor.RED.toString() + "Error while handling the anvil." - ) + logExceptionAndClear(event.view.player, event.inventory, e) return true } } @@ -316,6 +292,23 @@ object DependencyManager { return bypass } + // Clone item and use plugin specific clone if needed + fun cloneItem(event: PrepareAnvilEvent, item: ItemStack): ItemStack { + try { + return unsafeCloneItem(item) + } catch (e: Exception) { + logException(event.view.player, e) + return item.clone() + } + } + + private fun unsafeCloneItem(item: ItemStack): ItemStack { + var cloned: ItemStack? = null + + if(cloned == null) cloned = item.clone() + return cloned + } + fun stripLore(item: ItemStack): MutableList { val dummy = item.clone() diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/ItemsAdderDependency.java b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/ItemsAdderDependency.java new file mode 100644 index 0000000..7034b9a --- /dev/null +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/plugins/ItemsAdderDependency.java @@ -0,0 +1,12 @@ +package xyz.alexcrea.cuanvil.dependency.plugins; + +import org.bukkit.plugin.Plugin; +import org.jetbrains.annotations.NotNull; + +public class ItemsAdderDependency extends GenericPluginDependency{ + + public ItemsAdderDependency(@NotNull Plugin plugin) { + super(plugin); + } + +} diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index 7126dd9..03865f0 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -155,7 +155,7 @@ class PrepareAnvilListener : Listener { val amount = CustomRecipeUtil.getCustomRecipeAmount(recipe, first, second) - val resultItem: ItemStack = recipe.resultItem!!.clone() + val resultItem: ItemStack = DependencyManager.cloneItem(event, recipe.resultItem!!) resultItem.amount *= amount // Maybe add an option on custom craft to ignore/not ignore penalty ?? @@ -179,7 +179,7 @@ class PrepareAnvilListener : Listener { event: PrepareAnvilEvent, inventory: AnvilInventory, player: HumanEntity, first: ItemStack ) { - val resultItem = first.clone() + val resultItem = DependencyManager.cloneItem(event, first) var anvilCost = handleRename(resultItem, inventory, player) // Test/stop if nothing changed. @@ -251,7 +251,7 @@ class PrepareAnvilListener : Listener { .combineWith(second.findEnchantments(), first, player) var hasChanged = !isIdentical(first.findEnchantments(), newEnchants) - val resultItem = first.clone() + val resultItem = DependencyManager.cloneItem(event, first) var anvilCost = 0 if(hasChanged){ resultItem.setEnchantmentsUnsafe(newEnchants) @@ -307,7 +307,7 @@ class PrepareAnvilListener : Listener { ): Boolean { val unitRepairAmount = first.getRepair(second) ?: return false - val resultItem = first.clone() + val resultItem = DependencyManager.cloneItem(event, first) var anvilCost = handleRename(resultItem, inventory, player) val repairAmount = resultItem.unitRepair(second.amount, unitRepairAmount)