From fd7e7684d9680580e4b009c38f64c2fbc2c4c857 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 21 Mar 2025 17:04:50 +0100 Subject: [PATCH] made lore edit compatible with enchantment squared --- .../cuanvil/dependency/DependencyManager.kt | 41 +++++++++++++++---- .../EnchantmentSquaredDependency.kt | 8 ++++ .../cuanvil/util/AnvilLoreEditUtil.kt | 8 +++- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt index 437f3d5..212dc6d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/DependencyManager.kt @@ -105,8 +105,12 @@ object DependencyManager { fun tryEventPreAnvilBypass(event: PrepareAnvilEvent, player: HumanEntity): Boolean { try { return unsafeTryEventPreAnvilBypass(event, player) - } catch (e: Exception){ - CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", e) + } catch (e: Exception) { + CustomAnvil.instance.logger.log( + Level.SEVERE, + "Error while trying to handle custom anvil supported plugin: ", + e + ) // Just in case to avoid illegal items event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) @@ -141,8 +145,12 @@ object DependencyManager { try { unsafeTryTreatAnvilResult(event, result) return false - } catch (e: Exception){ - CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", e) + } catch (e: Exception) { + CustomAnvil.instance.logger.log( + Level.SEVERE, + "Error while trying to handle custom anvil supported plugin: ", + e + ) // Just in case to avoid illegal items event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) @@ -160,9 +168,13 @@ object DependencyManager { // Return true if should bypass (either by a dependency or error) fun tryClickAnvilResultBypass(event: InventoryClickEvent, inventory: AnvilInventory): Boolean { try { - return unsafeTryClickAnvilResultBypass(event, inventory) - } catch (e: Exception){ - CustomAnvil.instance.logger.log(Level.SEVERE, "Error while trying to handle custom anvil supported plugin: ", e) + return unsafeTryClickAnvilResultBypass(event, inventory) + } catch (e: Exception) { + CustomAnvil.instance.logger.log( + Level.SEVERE, + "Error while trying to handle custom anvil supported plugin: ", + e + ) // Just in case to avoid illegal items event.inventory.setItem(ANVIL_OUTPUT_SLOT, null) @@ -191,6 +203,21 @@ object DependencyManager { return bypass } + fun stripLore(item: ItemStack): ArrayList { + val lore = ArrayList() + val dummy = item.clone() + + enchantmentSquaredCompatibility?.stripLore(dummy) + + val itemLore = item.itemMeta!!.lore + if (itemLore != null) lore.addAll(itemLore) + + return lore + } + + fun updateLore(item: ItemStack) { + enchantmentSquaredCompatibility?.updateLore(item) + } private fun testIsFolia(): Boolean { try { diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt index 1b16c43..e630e8c 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/dependency/EnchantmentSquaredDependency.kt @@ -203,4 +203,12 @@ class EnchantmentSquaredDependency(private val enchantmentSquaredPlugin: Plugin) } } + fun stripLore(item: ItemStack) { + CustomEnchantManager.getInstance().removeAllEnchants(item) + } + + fun updateLore(item: ItemStack) { + CustomEnchantManager.getInstance().updateLore(item) + } + } diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt index e3b6d41..5d93a05 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/util/AnvilLoreEditUtil.kt @@ -1,9 +1,11 @@ package xyz.alexcrea.cuanvil.util +import com.willfp.eco.util.toNiceString import org.bukkit.entity.HumanEntity import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.BookMeta import org.bukkit.permissions.Permissible +import xyz.alexcrea.cuanvil.dependency.DependencyManager import xyz.alexcrea.cuanvil.util.config.LoreEditConfigUtil import xyz.alexcrea.cuanvil.util.config.LoreEditType import java.util.concurrent.atomic.AtomicInteger @@ -60,13 +62,14 @@ object AnvilLoreEditUtil { // remove lore val result = first.clone() val leftMeta = result.itemMeta ?: return null - val currentLore = ArrayList(leftMeta.lore ?: return null) + val currentLore: ArrayList = DependencyManager.stripLore(result) val uncolorCost = uncolorLines(player, currentLore, LoreEditType.REMOVE_BOOK) leftMeta.lore = null result.itemMeta = leftMeta + DependencyManager.updateLore(result) if (result == first) return null // Handle xp @@ -182,7 +185,7 @@ object AnvilLoreEditUtil { val meta = result.itemMeta!! val removeEnd = LoreEditConfigUtil.paperLoreOrderIsEnd - val lore: ArrayList = ArrayList(meta.lore!!) + val lore: ArrayList = DependencyManager.stripLore(result) val line = if (removeEnd) lore.removeAt(lore.size - 1) else lore.removeAt(0) @@ -195,6 +198,7 @@ object AnvilLoreEditUtil { tempList.add(line) val uncolorCost = uncolorLines(player, tempList, LoreEditType.REMOVE_PAPER) + DependencyManager.updateLore(result) if (result == first) return null // Handle other xp