Advertisement
Guest User

Untitled

a guest
Aug 5th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. <?php
  2.  
  3. namespace AlperCore\Nerzox\Drawer;
  4.  
  5. use pocketmine\inventory\ContainerInventory;
  6. use pocketmine\network\mcpe\protocol\BlockEventPacket;
  7. use pocketmine\network\mcpe\protocol\LevelSoundEventPacket;
  8. use pocketmine\network\mcpe\protocol\types\WindowTypes;
  9. use pocketmine\Player;
  10.  
  11. class DrawerInventory extends ContainerInventory {
  12.  
  13. protected $holder;
  14.  
  15. public function __construct(DrawerTile $tile){
  16. parent::__construct($tile);
  17. }
  18.  
  19. public function getNetworkType() : int{
  20. return WindowTypes::CONTAINER;
  21. }
  22.  
  23. public function getName() : string{
  24. return "Drawer";
  25. }
  26. public function getSize(): int
  27. {
  28. return 1;
  29. }
  30. public function getDefaultSize() : int{
  31. return 1;
  32. }
  33.  
  34. public function getHolder(){
  35. return $this->holder;
  36. }
  37.  
  38. protected function getOpenSound() : int{
  39. return LevelSoundEventPacket::SOUND_CHEST_OPEN;
  40. }
  41.  
  42. protected function getCloseSound() : int{
  43. return LevelSoundEventPacket::SOUND_CHEST_CLOSED;
  44. }
  45.  
  46. public function onOpen(Player $who) : void{
  47. parent::onOpen($who);
  48.  
  49. if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
  50. $this->broadcastBlockEventPacket(true);
  51. $this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getOpenSound());
  52. }
  53. }
  54.  
  55. public function onClose(Player $who) : void{
  56. if(count($this->getViewers()) === 1 and $this->getHolder()->isValid()){
  57. $this->broadcastBlockEventPacket(false);
  58. $this->getHolder()->getLevelNonNull()->broadcastLevelSoundEvent($this->getHolder()->add(0.5, 0.5, 0.5), $this->getCloseSound());
  59. }
  60. parent::onClose($who);
  61. }
  62.  
  63. protected function broadcastBlockEventPacket(bool $isOpen) : void{
  64. $holder = $this->getHolder();
  65.  
  66. $pk = new BlockEventPacket();
  67. $pk->x = (int) $holder->x;
  68. $pk->y = (int) $holder->y;
  69. $pk->z = (int) $holder->z;
  70. $pk->eventType = 1; //it's always 1 for a chest
  71. $pk->eventData = $isOpen ? 1 : 0;
  72. $holder->getLevelNonNull()->broadcastPacketToViewers($holder, $pk);
  73. }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement