Armor: only make sound when the item was equipped by player action

this ensures the greatest amount of consistency with vanilla.

in order to prevent the sounds being broadcasted on armor damage with the old method, we'd also have to sacrifice the sound when replacing one leather helmet with another, for example.
this approach minimizes the gameplay impact at the possible expense of plugins.

closes #6325
This commit is contained in:
Dylan K. Taylor 2024-04-08 14:05:24 +01:00
parent 55cf24aa02
commit 37322e0d50
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 4 additions and 18 deletions

View File

@ -149,24 +149,6 @@ abstract class Living extends Entity{
$this->getViewers(),
fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobArmorChange($recipients, $this)
)));
$playArmorSound = function(Item $newItem, Item $oldItem) : void{
if(!$newItem->isNull() && $newItem instanceof Armor && !$newItem->equalsExact($oldItem)){
$equipSound = $newItem->getMaterial()->getEquipSound();
if($equipSound !== null){
$this->broadcastSound($equipSound);
}
}
};
$this->armorInventory->getListeners()->add(new CallbackInventoryListener(
function(Inventory $inventory, int $slot, Item $oldItem) use ($playArmorSound) : void{
$playArmorSound($inventory->getItem($slot), $oldItem);
},
function(Inventory $inventory, array $oldContents) use ($playArmorSound) : void{
foreach($oldContents as $slot => $oldItem){
$playArmorSound($inventory->getItem($slot), $oldItem);
}
}
));
$health = $this->getMaxHealth();

View File

@ -146,6 +146,10 @@ class Armor extends Durable{
$new = $thisCopy->pop();
$player->getArmorInventory()->setItem($this->getArmorSlot(), $new);
$player->getInventory()->setItemInHand($existing);
$sound = $new->getMaterial()->getEquipSound();
if($sound !== null){
$player->broadcastSound($sound);
}
if(!$thisCopy->isNull()){
//if the stack size was bigger than 1 (usually won't happen, but might be caused by plugins)
$returnedItems[] = $thisCopy;