From 37322e0d508c48beabbb56b0c5289b86c7fed227 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Mon, 8 Apr 2024 14:05:24 +0100 Subject: [PATCH] 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 --- src/entity/Living.php | 18 ------------------ src/item/Armor.php | 4 ++++ 2 files changed, 4 insertions(+), 18 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index e695ba425..e7d669fda 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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(); diff --git a/src/item/Armor.php b/src/item/Armor.php index e9667a8a8..417c57f75 100644 --- a/src/item/Armor.php +++ b/src/item/Armor.php @@ -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;