22declare(strict_types=1);
24namespace pocketmine\block\utils;
31trait AnimatedContainerLikeTrait{
33 protected function getViewerCount() : int{
35 $tile = $position->getWorld()->getTile($position);
36 if($tile instanceof InventoryHolder){
37 return count($tile->getInventory()->getViewers());
42 abstract protected function getOpenSound() : Sound;
44 abstract protected function getCloseSound() : Sound;
46 abstract protected function playAnimationVisual(Position $position,
bool $isOpen) : void;
48 protected function playAnimationSound(Position $position,
bool $isOpen) : void{
49 $position->getWorld()->addSound($position->add(0.5, 0.5, 0.5), $isOpen ? $this->getOpenSound() : $this->getCloseSound());
52 abstract protected function getPosition() : Position;
54 protected function doAnimationEffects(
bool $isOpen) : void{
56 $this->playAnimationVisual($position, $isOpen);
57 $this->playAnimationSound($position, $isOpen);
60 public function onViewerAdded() : void{
61 if($this->getViewerCount() === 1){
62 $this->doAnimationEffects(
true);
66 public function onViewerRemoved() : void{
67 if($this->getViewerCount() === 1){
68 $this->doAnimationEffects(
false);