diff --git a/src/entity/Living.php b/src/entity/Living.php index e7d669fda..9a95cae07 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -149,6 +149,22 @@ 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( + fn(Inventory $inventory, int $slot, Item $oldItem) => $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/ArmorMaterial.php b/src/item/ArmorMaterial.php index c7915bacc..d0ea33feb 100644 --- a/src/item/ArmorMaterial.php +++ b/src/item/ArmorMaterial.php @@ -23,10 +23,13 @@ declare(strict_types=1); namespace pocketmine\item; +use pocketmine\world\sound\Sound; + class ArmorMaterial{ public function __construct( - private readonly int $enchantability + private readonly int $enchantability, + private readonly ?Sound $equipSound = null ){ } @@ -39,4 +42,11 @@ class ArmorMaterial{ public function getEnchantability() : int{ return $this->enchantability; } + + /** + * Returns the sound that plays when equipping the armor + */ + public function getEquipSound() : ?Sound{ + return $this->equipSound; + } } diff --git a/src/item/VanillaArmorMaterials.php b/src/item/VanillaArmorMaterials.php index ab2909bce..818273d20 100644 --- a/src/item/VanillaArmorMaterials.php +++ b/src/item/VanillaArmorMaterials.php @@ -24,6 +24,13 @@ declare(strict_types=1); namespace pocketmine\item; use pocketmine\utils\RegistryTrait; +use pocketmine\world\sound\ArmorEquipChainSound; +use pocketmine\world\sound\ArmorEquipDiamondSound; +use pocketmine\world\sound\ArmorEquipGenericSound; +use pocketmine\world\sound\ArmorEquipGoldSound; +use pocketmine\world\sound\ArmorEquipIronSound; +use pocketmine\world\sound\ArmorEquipLeatherSound; +use pocketmine\world\sound\ArmorEquipNetheriteSound; /** * This doc-block is generated automatically, do not modify it manually. @@ -62,12 +69,12 @@ final class VanillaArmorMaterials{ } protected static function setup() : void{ - self::register("leather", new ArmorMaterial(15)); - self::register("chainmail", new ArmorMaterial(12)); - self::register("iron", new ArmorMaterial(9)); - self::register("turtle", new ArmorMaterial(9)); - self::register("gold", new ArmorMaterial(25)); - self::register("diamond", new ArmorMaterial(10)); - self::register("netherite", new ArmorMaterial(15)); + self::register("leather", new ArmorMaterial(15, new ArmorEquipLeatherSound())); + self::register("chainmail", new ArmorMaterial(12, new ArmorEquipChainSound())); + self::register("iron", new ArmorMaterial(9, new ArmorEquipIronSound())); + self::register("turtle", new ArmorMaterial(9, new ArmorEquipGenericSound())); + self::register("gold", new ArmorMaterial(25, new ArmorEquipGoldSound())); + self::register("diamond", new ArmorMaterial(10, new ArmorEquipDiamondSound())); + self::register("netherite", new ArmorMaterial(15, new ArmorEquipNetheriteSound())); } } diff --git a/src/world/sound/ArmorEquipChainSound.php b/src/world/sound/ArmorEquipChainSound.php new file mode 100644 index 000000000..efcb4f982 --- /dev/null +++ b/src/world/sound/ArmorEquipChainSound.php @@ -0,0 +1,35 @@ +