From bf8b5905e4a02c6267d06ec26d3b1db26f96c680 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Thu, 19 Aug 2021 15:56:40 +0100 Subject: [PATCH] Player: Play ItemBreakSounds when items break due to usage closes #4381 --- src/player/Player.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/player/Player.php b/src/player/Player.php index e5c5046c5a..41b57be804 100644 --- a/src/player/Player.php +++ b/src/player/Player.php @@ -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); }