From 5df9becb19bce19604ce7e50daa896fa8965ab7f Mon Sep 17 00:00:00 2001 From: alexcrea Date: Sat, 20 Jun 2026 04:36:02 +0200 Subject: [PATCH] anvil sound & degrade basic feature --- .../cuanvil/listener/AnvilResultListener.kt | 59 ++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt index 2bfc97d0..68a44ff7 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -4,13 +4,16 @@ import io.delilaheve.CustomAnvil import io.delilaheve.util.ConfigOptions import io.delilaheve.util.ItemUtil.canMergeWith import org.bukkit.GameMode +import org.bukkit.Location import org.bukkit.Material +import org.bukkit.Sound import org.bukkit.entity.Player import org.bukkit.event.Event import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.inventory.ClickType import org.bukkit.event.inventory.InventoryClickEvent +import org.bukkit.inventory.AnvilInventory import org.bukkit.inventory.ItemStack import org.bukkit.inventory.meta.BookMeta import org.bukkit.inventory.view.AnvilView @@ -349,7 +352,9 @@ class AnvilResultListener : Listener { player.inventory.setItem(slotDestination.slot, result.item) } - // TODO probably anvil damage & sound here ?? + if (event.click != ClickType.MIDDLE) + handleAnvilMechanic(view, player.gameMode != GameMode.CREATIVE) + return true } @@ -377,6 +382,58 @@ class AnvilResultListener : Listener { } } + // Process both sound & degradation + private fun handleAnvilMechanic(view: AnvilView, canDegrade: Boolean) { + // ok so we do not provide a getLocation on view ? + val inv = view.topInventory as? AnvilInventory + val location = inv?.location + + val wasDestroyed = canDegrade && tryDegradeAnvil(view, location) + tryPlaySound(location, wasDestroyed) + } + + private fun tryDegradeAnvil(view: AnvilView, location: Location?): Boolean { + val world = location?.world ?: return false + if (Math.random() > 0.12) return false //TODO config value instead of hardcoded + + val block = world.getBlockAt(location) + + val next = when (block.type) { + Material.ANVIL -> Material.CHIPPED_ANVIL + Material.CHIPPED_ANVIL -> Material.DAMAGED_ANVIL + Material.DAMAGED_ANVIL -> Material.AIR + else -> return false + } + + block.type = next + + if (next == Material.AIR) { + view.close() + return true + } + + return false + } + + private fun tryPlaySound( + location: Location?, + wasDestroyed: Boolean, + ) { + val world = location?.world ?: return + + if (false) return //TODO enabled config sound + + // TODO config values + world.playSound( + location, + if (wasDestroyed) + Sound.BLOCK_ANVIL_DESTROY + else + Sound.BLOCK_ANVIL_USE, + 1f, 1f + ) + } + private fun onUnitRepairExtract( rightItem: ItemStack, event: InventoryClickEvent,