backport anvil mechanic

This commit is contained in:
alexcrea 2026-07-11 22:51:24 +02:00
parent c38e8025cd
commit 5bd78b4444
Signed by: alexcrea
GPG key ID: E59DF23EE2A2266C

View file

@ -358,7 +358,7 @@ class AnvilResultListener : Listener {
} }
if (event.click != ClickType.MIDDLE) if (event.click != ClickType.MIDDLE)
handleAnvilMechanic(player, view, player.gameMode != GameMode.CREATIVE) handleAnvilMechanic(player, inventory, player.gameMode != GameMode.CREATIVE)
return true return true
} }
@ -388,16 +388,15 @@ class AnvilResultListener : Listener {
} }
// Process both sound & degradation // Process both sound & degradation
private fun handleAnvilMechanic(player: HumanEntity, view: AnvilView, canDegrade: Boolean) { private fun handleAnvilMechanic(player: HumanEntity, inventory: AnvilInventory, canDegrade: Boolean) {
// ok so we do not provide a getLocation on view ? // ok so we do not provide a getLocation on view ?
val inv = view.topInventory as? AnvilInventory val location = inventory.location
val location = inv?.location
val wasDestroyed = canDegrade && tryDegradeAnvil(view, location) val wasDestroyed = canDegrade && tryDegradeAnvil(player, location)
tryPlaySound(location, player, wasDestroyed) tryPlaySound(location, wasDestroyed)
} }
private fun tryDegradeAnvil(view: AnvilView, location: Location?): Boolean { private fun tryDegradeAnvil(player: HumanEntity, location: Location?): Boolean {
val world = location?.world ?: return false val world = location?.world ?: return false
if (Math.random() > AnvilFinishOptions.degradation_chance) return false if (Math.random() > AnvilFinishOptions.degradation_chance) return false
@ -413,7 +412,7 @@ class AnvilResultListener : Listener {
block.type = next block.type = next
if (next == Material.AIR) { if (next == Material.AIR) {
view.close() player.closeInventory()
return true return true
} }
@ -422,7 +421,6 @@ class AnvilResultListener : Listener {
private fun tryPlaySound( private fun tryPlaySound(
location: Location?, location: Location?,
player: HumanEntity,
wasDestroyed: Boolean, wasDestroyed: Boolean,
) { ) {
val world = location?.world val world = location?.world
@ -430,16 +428,7 @@ class AnvilResultListener : Listener {
if (!AnvilFinishOptions.sound_enabled) return if (!AnvilFinishOptions.sound_enabled) return
val sound = AnvilFinishOptions.getAnvilSound(wasDestroyed) val sound = AnvilFinishOptions.getAnvilSound(wasDestroyed)
if(world == null) { world?.playSound(
player.world.playSound(
player,
sound.sound,
sound.category,
sound.volume,
sound.pitch,
)
} else {
world.playSound(
location, location,
sound.sound, sound.sound,
sound.category, sound.category,
@ -447,7 +436,6 @@ class AnvilResultListener : Listener {
sound.pitch, sound.pitch,
) )
} }
}
private fun onUnitRepairExtract( private fun onUnitRepairExtract(
rightItem: ItemStack, rightItem: ItemStack,