From 015ca99475c8ca392d80c073153512cc53fde15e 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 # Conflicts: # src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt --- .../cuanvil/listener/AnvilResultListener.kt | 58 ++++++++++++++++++- 1 file changed, 57 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 e9290ac9..75fe607d 100644 --- a/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt +++ b/src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt @@ -4,7 +4,9 @@ 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 @@ -354,7 +356,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 } @@ -382,6 +386,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,