From adad9f880e29aadb93465e19b90af7c8853d2214 Mon Sep 17 00:00:00 2001 From: AIPTU Date: Sun, 30 Jun 2024 13:32:12 +0700 Subject: [PATCH] feat: implements npc events --- src/aiptu/smaccer/EventHandler.php | 17 +++++++++ src/aiptu/smaccer/entity/SmaccerHandler.php | 32 +++++++++++----- src/aiptu/smaccer/event/NPCAttackEvent.php | 33 ++++++++++++++++ src/aiptu/smaccer/event/NPCDespawnEvent.php | 24 ++++++++++++ src/aiptu/smaccer/event/NPCInteractEvent.php | 33 ++++++++++++++++ src/aiptu/smaccer/event/NPCSpawnEvent.php | 24 ++++++++++++ src/aiptu/smaccer/event/NPCUpdateEvent.php | 40 ++++++++++++++++++++ 7 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 src/aiptu/smaccer/event/NPCAttackEvent.php create mode 100644 src/aiptu/smaccer/event/NPCDespawnEvent.php create mode 100644 src/aiptu/smaccer/event/NPCInteractEvent.php create mode 100644 src/aiptu/smaccer/event/NPCSpawnEvent.php create mode 100644 src/aiptu/smaccer/event/NPCUpdateEvent.php diff --git a/src/aiptu/smaccer/EventHandler.php b/src/aiptu/smaccer/EventHandler.php index c1b8836..5ec0ddd 100644 --- a/src/aiptu/smaccer/EventHandler.php +++ b/src/aiptu/smaccer/EventHandler.php @@ -17,6 +17,8 @@ use aiptu\smaccer\entity\HumanSmaccer; use aiptu\smaccer\entity\SmaccerHandler; use aiptu\smaccer\entity\utils\EntityVisibility; +use aiptu\smaccer\event\NPCAttackEvent; +use aiptu\smaccer\event\NPCInteractEvent; use aiptu\smaccer\utils\Permissions; use aiptu\smaccer\utils\Queue; use pocketmine\entity\animation\ArmSwingAnimation; @@ -99,6 +101,13 @@ public function onAttack(EntityDamageEvent $event) : void { } if ($damager instanceof Player) { + $npcAttackEvent = new NPCAttackEvent($damager, $entity); + $npcAttackEvent->call(); + if ($npcAttackEvent->isCancelled()) { + $event->cancel(); + return; + } + $npcId = $entity->getId(); $playerName = $damager->getName(); if (Queue::isInQueue($playerName, Queue::ACTION_RETRIEVE)) { @@ -133,6 +142,12 @@ public function onInteract(PlayerEntityInteractEvent $event) : void { if (($entity instanceof HumanSmaccer || $entity instanceof EntitySmaccer) && $entity->getVisibility() !== EntityVisibility::INVISIBLE_TO_EVERYONE) { + $npcInteractEvent = new NPCInteractEvent($player, $entity); + $npcInteractEvent->call(); + if ($npcInteractEvent->isCancelled()) { + return; + } + if ($entity->canExecuteCommands($player)) { $entity->executeCommands($player); } @@ -155,6 +170,8 @@ public function onInteract(PlayerEntityInteractEvent $event) : void { } } } + + $event->cancel(); } } } diff --git a/src/aiptu/smaccer/entity/SmaccerHandler.php b/src/aiptu/smaccer/entity/SmaccerHandler.php index e70b931..12516f6 100644 --- a/src/aiptu/smaccer/entity/SmaccerHandler.php +++ b/src/aiptu/smaccer/entity/SmaccerHandler.php @@ -95,6 +95,9 @@ use aiptu\smaccer\entity\npc\ZombieVillagerSmaccer; use aiptu\smaccer\entity\npc\ZombieVillagerV2Smaccer; use aiptu\smaccer\entity\utils\EntityTag; +use aiptu\smaccer\event\NPCDespawnEvent; +use aiptu\smaccer\event\NPCSpawnEvent; +use aiptu\smaccer\event\NPCUpdateEvent; use aiptu\smaccer\Smaccer; use aiptu\smaccer\utils\promise\Promise; use aiptu\smaccer\utils\promise\PromiseResolver; @@ -408,6 +411,8 @@ public function spawnNPC( $entityId = $entity->getId(); $this->playerNPCs[$playerId][$entityId] = $entity; + (new NPCSpawnEvent($entity))->call(); + $resolver->resolve($entity); return $promise; } @@ -426,6 +431,8 @@ public function despawnNPC(string $creatorId, Entity $entity) : Promise { return $promise; } + (new NPCDespawnEvent($entity))->call(); + $entity->flagForDespawn(); unset($this->playerNPCs[$creatorId][$entityId]); @@ -445,15 +452,22 @@ public function editNPC(Player $player, Entity $entity, NPCData $npcData) : Prom return $promise; } - $nameTag = $npcData->getNameTag(); - $scale = $npcData->getScale(); - $rotationEnabled = $npcData->isRotationEnabled(); - $nametagVisible = $npcData->isNametagVisible(); - $visibility = $npcData->getVisibility(); - $isBaby = $npcData->isBaby(); - $slapBack = $npcData->getSlapBack(); - $actionEmote = $npcData->getActionEmote(); - $emote = $npcData->getEmote(); + $ev = new NPCUpdateEvent($entity, $npcData); + $ev->call(); + if ($ev->isCancelled()) { + $resolver->reject(new \RuntimeException('NPC update event was cancelled')); + return $promise; + } + + $nameTag = $ev->getNPCData()->getNameTag(); + $scale = $ev->getNPCData()->getScale(); + $rotationEnabled = $ev->getNPCData()->isRotationEnabled(); + $nametagVisible = $ev->getNPCData()->isNametagVisible(); + $visibility = $ev->getNPCData()->getVisibility(); + $isBaby = $ev->getNPCData()->isBaby(); + $slapBack = $ev->getNPCData()->getSlapBack(); + $actionEmote = $ev->getNPCData()->getActionEmote(); + $emote = $ev->getNPCData()->getEmote(); if ($nameTag !== null) { $nameTag = $this->applyNametag($nameTag, $player); diff --git a/src/aiptu/smaccer/event/NPCAttackEvent.php b/src/aiptu/smaccer/event/NPCAttackEvent.php new file mode 100644 index 0000000..e4889e1 --- /dev/null +++ b/src/aiptu/smaccer/event/NPCAttackEvent.php @@ -0,0 +1,33 @@ +entity; + } +} diff --git a/src/aiptu/smaccer/event/NPCDespawnEvent.php b/src/aiptu/smaccer/event/NPCDespawnEvent.php new file mode 100644 index 0000000..3bcfbba --- /dev/null +++ b/src/aiptu/smaccer/event/NPCDespawnEvent.php @@ -0,0 +1,24 @@ + + */ +class NPCDespawnEvent extends EntityEvent { + public function __construct(protected Entity $entity) {} +} diff --git a/src/aiptu/smaccer/event/NPCInteractEvent.php b/src/aiptu/smaccer/event/NPCInteractEvent.php new file mode 100644 index 0000000..fe8bf14 --- /dev/null +++ b/src/aiptu/smaccer/event/NPCInteractEvent.php @@ -0,0 +1,33 @@ +entity; + } +} diff --git a/src/aiptu/smaccer/event/NPCSpawnEvent.php b/src/aiptu/smaccer/event/NPCSpawnEvent.php new file mode 100644 index 0000000..3dd9dce --- /dev/null +++ b/src/aiptu/smaccer/event/NPCSpawnEvent.php @@ -0,0 +1,24 @@ + + */ +class NPCSpawnEvent extends EntityEvent { + public function __construct(protected Entity $entity) {} +} diff --git a/src/aiptu/smaccer/event/NPCUpdateEvent.php b/src/aiptu/smaccer/event/NPCUpdateEvent.php new file mode 100644 index 0000000..8b53f88 --- /dev/null +++ b/src/aiptu/smaccer/event/NPCUpdateEvent.php @@ -0,0 +1,40 @@ + + */ +class NPCUpdateEvent extends EntityEvent implements Cancellable { + use CancellableTrait; + + public function __construct( + protected Entity $entity, + protected NPCData $npcData + ) {} + + public function getNPCData() : NPCData { + return $this->npcData; + } + + public function setNPCData(NPCData $npcData) : void { + $this->npcData = $npcData; + } +}