anvil sound & degrade basic feature

This commit is contained in:
alexcrea 2026-06-20 04:36:02 +02:00
parent 11e10b5850
commit 5df9becb19
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C

View file

@ -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,