Skip to content

Commit f799cfa

Browse files
authored
Implemented sound when equipping armor (#6303)
1 parent 337e462 commit f799cfa

10 files changed

+286
-8
lines changed

src/entity/Living.php

+16
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,22 @@ protected function initEntity(CompoundTag $nbt) : void{
149149
$this->getViewers(),
150150
fn(EntityEventBroadcaster $broadcaster, array $recipients) => $broadcaster->onMobArmorChange($recipients, $this)
151151
)));
152+
$playArmorSound = function(Item $newItem, Item $oldItem) : void{
153+
if(!$newItem->isNull() && $newItem instanceof Armor && !$newItem->equalsExact($oldItem)){
154+
$equipSound = $newItem->getMaterial()->getEquipSound();
155+
if($equipSound !== null){
156+
$this->broadcastSound($equipSound);
157+
}
158+
}
159+
};
160+
$this->armorInventory->getListeners()->add(new CallbackInventoryListener(
161+
fn(Inventory $inventory, int $slot, Item $oldItem) => $playArmorSound($inventory->getItem($slot), $oldItem),
162+
function(Inventory $inventory, array $oldContents) use ($playArmorSound) : void{
163+
foreach($oldContents as $slot => $oldItem){
164+
$playArmorSound($inventory->getItem($slot), $oldItem);
165+
}
166+
}
167+
));
152168

153169
$health = $this->getMaxHealth();
154170

src/item/ArmorMaterial.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323

2424
namespace pocketmine\item;
2525

26+
use pocketmine\world\sound\Sound;
27+
2628
class ArmorMaterial{
2729

2830
public function __construct(
29-
private readonly int $enchantability
31+
private readonly int $enchantability,
32+
private readonly ?Sound $equipSound = null
3033
){
3134
}
3235

@@ -39,4 +42,11 @@ public function __construct(
3942
public function getEnchantability() : int{
4043
return $this->enchantability;
4144
}
45+
46+
/**
47+
* Returns the sound that plays when equipping the armor
48+
*/
49+
public function getEquipSound() : ?Sound{
50+
return $this->equipSound;
51+
}
4252
}

src/item/VanillaArmorMaterials.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@
2424
namespace pocketmine\item;
2525

2626
use pocketmine\utils\RegistryTrait;
27+
use pocketmine\world\sound\ArmorEquipChainSound;
28+
use pocketmine\world\sound\ArmorEquipDiamondSound;
29+
use pocketmine\world\sound\ArmorEquipGenericSound;
30+
use pocketmine\world\sound\ArmorEquipGoldSound;
31+
use pocketmine\world\sound\ArmorEquipIronSound;
32+
use pocketmine\world\sound\ArmorEquipLeatherSound;
33+
use pocketmine\world\sound\ArmorEquipNetheriteSound;
2734

2835
/**
2936
* This doc-block is generated automatically, do not modify it manually.
@@ -62,12 +69,12 @@ public static function getAll() : array{
6269
}
6370

6471
protected static function setup() : void{
65-
self::register("leather", new ArmorMaterial(15));
66-
self::register("chainmail", new ArmorMaterial(12));
67-
self::register("iron", new ArmorMaterial(9));
68-
self::register("turtle", new ArmorMaterial(9));
69-
self::register("gold", new ArmorMaterial(25));
70-
self::register("diamond", new ArmorMaterial(10));
71-
self::register("netherite", new ArmorMaterial(15));
72+
self::register("leather", new ArmorMaterial(15, new ArmorEquipLeatherSound()));
73+
self::register("chainmail", new ArmorMaterial(12, new ArmorEquipChainSound()));
74+
self::register("iron", new ArmorMaterial(9, new ArmorEquipIronSound()));
75+
self::register("turtle", new ArmorMaterial(9, new ArmorEquipGenericSound()));
76+
self::register("gold", new ArmorMaterial(25, new ArmorEquipGoldSound()));
77+
self::register("diamond", new ArmorMaterial(10, new ArmorEquipDiamondSound()));
78+
self::register("netherite", new ArmorMaterial(15, new ArmorEquipNetheriteSound()));
7279
}
7380
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipChainSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_CHAIN, $pos, false)];
34+
}
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipDiamondSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_DIAMOND, $pos, false)];
34+
}
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipGenericSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_GENERIC, $pos, false)];
34+
}
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipGoldSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_GOLD, $pos, false)];
34+
}
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipIronSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_IRON, $pos, false)];
34+
}
35+
}
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipLeatherSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_LEATHER, $pos, false)];
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
/*
4+
*
5+
* ____ _ _ __ __ _ __ __ ____
6+
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7+
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8+
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9+
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10+
*
11+
* This program is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Lesser General Public License as published by
13+
* the Free Software Foundation, either version 3 of the License, or
14+
* (at your option) any later version.
15+
*
16+
* @author PocketMine Team
17+
* @link http://www.pocketmine.net/
18+
*
19+
*
20+
*/
21+
22+
declare(strict_types=1);
23+
24+
namespace pocketmine\world\sound;
25+
26+
use pocketmine\math\Vector3;
27+
use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
28+
use pocketmine\network\mcpe\protocol\types\LevelSoundEvent;
29+
30+
class ArmorEquipNetheriteSound implements Sound{
31+
32+
public function encode(Vector3 $pos) : array{
33+
return [LevelSoundEventPacket::nonActorSound(LevelSoundEvent::ARMOR_EQUIP_NETHERITE, $pos, false)];
34+
}
35+
}

0 commit comments

Comments
 (0)