Player: Play ItemBreakSounds when items break due to usage

closes #4381
This commit is contained in:
Dylan K. Taylor 2021-08-19 15:56:40 +01:00
parent a5b85e095a
commit bf8b5905e4
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -75,6 +75,7 @@ use pocketmine\form\FormValidationException;
use pocketmine\inventory\Inventory;
use pocketmine\inventory\PlayerCursorInventory;
use pocketmine\item\ConsumableItem;
use pocketmine\item\Durable;
use pocketmine\item\enchantment\EnchantmentInstance;
use pocketmine\item\enchantment\MeleeWeaponEnchantment;
use pocketmine\item\Item;
@ -110,6 +111,7 @@ use pocketmine\world\Position;
use pocketmine\world\sound\EntityAttackNoDamageSound;
use pocketmine\world\sound\EntityAttackSound;
use pocketmine\world\sound\FireExtinguishSound;
use pocketmine\world\sound\ItemBreakSound;
use pocketmine\world\sound\Sound;
use pocketmine\world\World;
use Ramsey\Uuid\UuidInterface;
@ -1380,6 +1382,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$this->resetItemCooldown($item);
if($this->hasFiniteResources() and !$item->equalsExact($oldItem) and $oldItem->equalsExact($this->inventory->getItemInHand())){
if($item instanceof Durable && $item->isBroken()){
$this->broadcastSound(new ItemBreakSound());
}
$this->inventory->setItemInHand($item);
}
@ -1441,6 +1446,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
if($result->equals(ItemUseResult::SUCCESS())){
$this->resetItemCooldown($item);
if(!$item->equalsExact($oldItem) and $oldItem->equalsExact($this->inventory->getItemInHand())){
if($item instanceof Durable && $item->isBroken()){
$this->broadcastSound(new ItemBreakSound());
}
$this->inventory->setItemInHand($item);
}
return true;
@ -1558,6 +1566,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$oldItem = clone $item;
if($this->getWorld()->useBreakOn($pos, $item, $this, true)){
if($this->hasFiniteResources() and !$item->equalsExact($oldItem) and $oldItem->equalsExact($this->inventory->getItemInHand())){
if($item instanceof Durable && $item->isBroken()){
$this->broadcastSound(new ItemBreakSound());
}
$this->inventory->setItemInHand($item);
}
$this->hungerManager->exhaust(0.025, PlayerExhaustEvent::CAUSE_MINING);
@ -1582,6 +1593,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$oldItem = clone $item;
if($this->getWorld()->useItemOn($pos, $item, $face, $clickOffset, $this, true)){
if($this->hasFiniteResources() and !$item->equalsExact($oldItem) and $oldItem->equalsExact($this->inventory->getItemInHand())){
if($item instanceof Durable && $item->isBroken()){
$this->broadcastSound(new ItemBreakSound());
}
$this->inventory->setItemInHand($item);
}
return true;
@ -1654,6 +1668,9 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
//reactive damage like thorns might cause us to be killed by attacking another mob, which
//would mean we'd already have dropped the inventory by the time we reached here
if($heldItem->onAttackEntity($entity) and $this->hasFiniteResources() and $oldItem->equalsExact($this->inventory->getItemInHand())){ //always fire the hook, even if we are survival
if($heldItem instanceof Durable && $heldItem->isBroken()){
$this->broadcastSound(new ItemBreakSound());
}
$this->inventory->setItemInHand($heldItem);
}