Skip to content

Commit

Permalink
feat: implements npc events
Browse files Browse the repository at this point in the history
  • Loading branch information
AIPTU committed Jun 30, 2024
1 parent 3927736 commit adad9f8
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 9 deletions.
17 changes: 17 additions & 0 deletions src/aiptu/smaccer/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -155,6 +170,8 @@ public function onInteract(PlayerEntityInteractEvent $event) : void {
}
}
}

$event->cancel();
}
}
}
32 changes: 23 additions & 9 deletions src/aiptu/smaccer/entity/SmaccerHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -408,6 +411,8 @@ public function spawnNPC(
$entityId = $entity->getId();
$this->playerNPCs[$playerId][$entityId] = $entity;

(new NPCSpawnEvent($entity))->call();

$resolver->resolve($entity);
return $promise;
}
Expand All @@ -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]);

Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions src/aiptu/smaccer/event/NPCAttackEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\event;

use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\event\player\PlayerEvent;
use pocketmine\player\Player;

class NPCAttackEvent extends PlayerEvent implements Cancellable {
use CancellableTrait;

public function __construct(
protected Player $player,
private Entity $entity,
) {}

public function getEntity() : Entity {
return $this->entity;
}
}
24 changes: 24 additions & 0 deletions src/aiptu/smaccer/event/NPCDespawnEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\event;

use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityEvent;

/**
* @phpstan-extends EntityEvent<Entity>
*/
class NPCDespawnEvent extends EntityEvent {
public function __construct(protected Entity $entity) {}
}
33 changes: 33 additions & 0 deletions src/aiptu/smaccer/event/NPCInteractEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\event;

use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\event\player\PlayerEvent;
use pocketmine\player\Player;

class NPCInteractEvent extends PlayerEvent implements Cancellable {
use CancellableTrait;

public function __construct(
protected Player $player,
private Entity $entity,
) {}

public function getEntity() : Entity {
return $this->entity;
}
}
24 changes: 24 additions & 0 deletions src/aiptu/smaccer/event/NPCSpawnEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\event;

use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityEvent;

/**
* @phpstan-extends EntityEvent<Entity>
*/
class NPCSpawnEvent extends EntityEvent {
public function __construct(protected Entity $entity) {}
}
40 changes: 40 additions & 0 deletions src/aiptu/smaccer/event/NPCUpdateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/*
* Copyright (c) 2024 AIPTU
*
* For the full copyright and license information, please view
* the LICENSE.md file that was distributed with this source code.
*
* @see https://github.com/AIPTU/Smaccer
*/

declare(strict_types=1);

namespace aiptu\smaccer\event;

use aiptu\smaccer\entity\NPCData;
use pocketmine\entity\Entity;
use pocketmine\event\Cancellable;
use pocketmine\event\CancellableTrait;
use pocketmine\event\entity\EntityEvent;

/**
* @phpstan-extends EntityEvent<Entity>
*/
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;
}
}

0 comments on commit adad9f8

Please sign in to comment.