From 1db6385082aa1500f3f6afa4492b4c087cc2ff2e Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 24 Jan 2025 12:13:44 +0100 Subject: [PATCH 1/3] Make sure rename work when null --- .../cuanvil/listener/PrepareAnvilListener.kt | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index 68370d4..87afba6 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -116,26 +116,27 @@ class PrepareAnvilListener : Listener { } private fun handleRename(resultItem: ItemStack, inventory: AnvilInventory, player: HumanEntity): Int { + // Can be null + var inventoryName = ChatColor.stripColor(inventory.renameText) + + var sumCost = 0 + var useColor = false + if(ConfigOptions.renameColorPossible && inventoryName != null){ + val resultString = StringBuilder(inventoryName) + + useColor = AnvilColorUtil.handleRenamingColor(resultString, player) + + if(useColor) { + inventoryName = resultString.toString() + + sumCost+= ConfigOptions.useOfColorCost + } + } + // Rename item and add renaming cost resultItem.itemMeta?.let { + val displayName = ChatColor.stripColor(it.displayName) - var inventoryName = ChatColor.stripColor(inventory.renameText) - - var sumCost = 0 - - var useColor = false - if(ConfigOptions.renameColorPossible){ - val resultString = StringBuilder(inventoryName) - - useColor = AnvilColorUtil.handleRenamingColor(resultString, player) - - if(useColor) { - inventoryName = resultString.toString() - - sumCost+= ConfigOptions.useOfColorCost - } - } - if ((!useColor && (!displayName.contentEquals(inventoryName))) || (useColor && !(it.displayName).contentEquals(inventoryName))) { it.setDisplayName(inventoryName) resultItem.itemMeta = it From 0d034c890e9429115534700adef2a9b66455caa5 Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 24 Jan 2025 12:33:50 +0100 Subject: [PATCH 2/3] Add dependency error safety. --- .../cuanvil/listener/AnvilResultListener.kt | 18 +++++++++++++++++- .../cuanvil/listener/PrepareAnvilListener.kt | 16 +++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt index 6622767..a9d3cd5 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -4,6 +4,7 @@ import io.delilaheve.CustomAnvil import io.delilaheve.util.ConfigOptions import io.delilaheve.util.ItemUtil.canMergeWith import io.delilaheve.util.ItemUtil.unitRepair +import org.bukkit.ChatColor import org.bukkit.GameMode import org.bukkit.Material import org.bukkit.entity.Player @@ -23,6 +24,7 @@ import xyz.alexcrea.cuanvil.recipe.AnvilCustomRecipe import xyz.alexcrea.cuanvil.util.AnvilXpUtil import xyz.alexcrea.cuanvil.util.CustomRecipeUtil import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair +import java.util.logging.Level import kotlin.math.min class AnvilResultListener: Listener { @@ -45,8 +47,22 @@ class AnvilResultListener: Listener { if (event.rawSlot != ANVIL_OUTPUT_SLOT) { return } + // Test if the event should bypass custom anvil. - if(DependencyManager.tryClickAnvilResultBypass(event, inventory)) return + var shouldBypass: Boolean + try { + shouldBypass = DependencyManager.tryClickAnvilResultBypass(event, inventory) + } catch (e: Exception){ + shouldBypass = true + 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) + + // Finally, warn the player, maybe a lot of time but better warn than do nothing + player.sendMessage(ChatColor.RED.toString() + "Error while handling the anvil.") + } + if(shouldBypass) return val output = inventory.getItem(ANVIL_OUTPUT_SLOT) ?: return val leftItem = inventory.getItem(ANVIL_INPUT_LEFT) ?: return diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt index 87afba6..ab50036 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/PrepareAnvilListener.kt @@ -22,6 +22,7 @@ import xyz.alexcrea.cuanvil.util.AnvilColorUtil import xyz.alexcrea.cuanvil.util.AnvilXpUtil import xyz.alexcrea.cuanvil.util.CustomRecipeUtil import xyz.alexcrea.cuanvil.util.UnitRepairUtil.getRepair +import java.util.logging.Level /** * Listener for anvil events @@ -45,7 +46,20 @@ class PrepareAnvilListener : Listener { val player: HumanEntity = event.viewers.first() // Test if the event should bypass custom anvil. - if(DependencyManager.tryEventPreAnvilBypass(event, player)) return + var shouldBypass: Boolean + try { + shouldBypass = DependencyManager.tryEventPreAnvilBypass(event, player) + } catch (e: Exception){ + shouldBypass = true + 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) + + // Finally, warn the player, maybe a lot of time but better warn than do nothing + player.sendMessage(ChatColor.RED.toString() + "Error while handling the anvil.") + } + if(shouldBypass) return val inventory = event.inventory val first = inventory.getItem(ANVIL_INPUT_LEFT) ?: return From 4e46206541df034020258ed8eac1c569d578ae1e Mon Sep 17 00:00:00 2001 From: alexcrea <42614139+alexcrea@users.noreply.github.com> Date: Fri, 24 Jan 2025 12:34:19 +0100 Subject: [PATCH 3/3] version up --- build.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle.kts b/build.gradle.kts index e10c058..45ea74c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -16,7 +16,7 @@ plugins { } group = "xyz.alexcrea" -version = "1.7.0" +version = "1.7.1" repositories { // EcoEnchants