anvil sound & degrade basic feature

# Conflicts:
#	src/main/kotlin/xyz/alexcrea/cuanvil/listener/AnvilResultListener.kt
This commit is contained in:
alexcrea 2026-06-20 04:36:02 +02:00
parent 52a58acd28
commit 015ca99475
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C

View file

@ -4,7 +4,9 @@ import io.delilaheve.CustomAnvil
import io.delilaheve.util.ConfigOptions import io.delilaheve.util.ConfigOptions
import io.delilaheve.util.ItemUtil.canMergeWith import io.delilaheve.util.ItemUtil.canMergeWith
import org.bukkit.GameMode import org.bukkit.GameMode
import org.bukkit.Location
import org.bukkit.Material import org.bukkit.Material
import org.bukkit.Sound
import org.bukkit.entity.Player import org.bukkit.entity.Player
import org.bukkit.event.Event import org.bukkit.event.Event
import org.bukkit.event.EventHandler import org.bukkit.event.EventHandler
@ -354,7 +356,9 @@ class AnvilResultListener : Listener {
player.inventory.setItem(slotDestination.slot, result.item) 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 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( private fun onUnitRepairExtract(
rightItem: ItemStack, rightItem: ItemStack,
event: InventoryClickEvent, event: InventoryClickEvent,