Skip to content

Commit

Permalink
feat: update NPC action handling and queue management
Browse files Browse the repository at this point in the history
  • Loading branch information
AIPTU committed Jul 2, 2024
1 parent 959c5ab commit 7515d1b
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 36 deletions.
96 changes: 60 additions & 36 deletions src/aiptu/smaccer/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use aiptu\smaccer\entity\utils\EntityVisibility;
use aiptu\smaccer\event\NPCAttackEvent;
use aiptu\smaccer\event\NPCInteractEvent;
use aiptu\smaccer\utils\FormManager;
use aiptu\smaccer\utils\Permissions;
use aiptu\smaccer\utils\Queue;
use pocketmine\event\entity\EntityDamageByEntityEvent;
Expand Down Expand Up @@ -110,49 +111,72 @@ public function onEffectAdd(EntityEffectAddEvent $event) : void {
}

public function onAttack(EntityDamageEvent $event) : void {
if ($event instanceof EntityDamageByEntityEvent) {
$damager = $event->getDamager();
$entity = $event->getEntity();
if (!$event instanceof EntityDamageByEntityEvent) {
return;
}

if (($entity instanceof HumanSmaccer) || ($entity instanceof EntitySmaccer)) {
if ($entity->getVisibility() === EntityVisibility::INVISIBLE_TO_EVERYONE) {
return;
}
$damager = $event->getDamager();
$entity = $event->getEntity();

if ($damager instanceof Player) {
$npcAttackEvent = new NPCAttackEvent($damager, $entity);
$npcAttackEvent->call();
if ($npcAttackEvent->isCancelled()) {
$event->cancel();
return;
}
if (!($entity instanceof HumanSmaccer || $entity instanceof EntitySmaccer)) {
return;
}

$npcId = $entity->getId();
$playerName = $damager->getName();
if (Queue::isInQueue($playerName, Queue::ACTION_RETRIEVE)) {
$damager->sendMessage(TextFormat::GREEN . 'NPC Entity ID: ' . $npcId);
Queue::removeFromQueue($playerName, Queue::ACTION_RETRIEVE);
} elseif (Queue::isInQueue($playerName, Queue::ACTION_DELETE)) {
if (!$entity->isOwnedBy($damager) && !$damager->hasPermission(Permissions::COMMAND_DELETE_OTHERS)) {
$damager->sendMessage(TextFormat::RED . "You don't have permission to delete this entity!");
return;
}
if ($entity->getVisibility() === EntityVisibility::INVISIBLE_TO_EVERYONE) {
return;
}

SmaccerHandler::getInstance()->despawnNPC($entity->getCreatorId(), $entity)->onCompletion(
function (bool $success) use ($damager, $npcId, $entity) : void {
$damager->sendMessage(TextFormat::GREEN . 'NPC ' . $entity->getName() . ' with ID ' . $npcId . ' despawned successfully.');
},
function (\Throwable $e) use ($damager) : void {
$damager->sendMessage(TextFormat::RED . 'Failed to despawn npc: ' . $e->getMessage());
}
);
Queue::removeFromQueue($playerName, Queue::ACTION_DELETE);
}
if (!$damager instanceof Player) {
return;
}

$npcAttackEvent = new NPCAttackEvent($damager, $entity);
$npcAttackEvent->call();
if ($npcAttackEvent->isCancelled()) {
$event->cancel();
return;
}

$npcId = $entity->getId();
$playerName = $damager->getName();
$action = Queue::getCurrentAction($playerName);

if ($action === null) {
$event->cancel();
return;
}

switch ($action) {
case Queue::ACTION_EDIT:
if (!$entity->isOwnedBy($damager) && !$damager->hasPermission(Permissions::COMMAND_EDIT_OTHERS)) {
$damager->sendMessage(TextFormat::RED . "You don't have permission to edit this entity!");
break;
}

$event->cancel();
}
FormManager::sendEditMenuForm($damager, $entity);
break;
case Queue::ACTION_DELETE:
if (!$entity->isOwnedBy($damager) && !$damager->hasPermission(Permissions::COMMAND_DELETE_OTHERS)) {
$damager->sendMessage(TextFormat::RED . "You don't have permission to delete this entity!");
break;
}

SmaccerHandler::getInstance()->despawnNPC($entity->getCreatorId(), $entity)->onCompletion(
function (bool $success) use ($damager, $npcId, $entity) : void {
$damager->sendMessage(TextFormat::GREEN . 'NPC ' . $entity->getName() . ' with ID ' . $npcId . ' despawned successfully.');
},
function (\Throwable $e) use ($damager) : void {
$damager->sendMessage(TextFormat::RED . 'Failed to despawn NPC: ' . $e->getMessage());
}
);
break;
case Queue::ACTION_RETRIEVE:
$damager->sendMessage(TextFormat::GREEN . 'NPC Entity ID: ' . $npcId);
break;
}

Queue::removeFromQueue($playerName, $action);
$event->cancel();
}

public function onInteract(PlayerEntityInteractEvent $event) : void {
Expand Down
15 changes: 15 additions & 0 deletions src/aiptu/smaccer/utils/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,19 @@ public static function removeFromAllQueues(string $playerName) : bool {

return false;
}

public static function getCurrentAction(string $playerName) : ?string {
$playerName = strtolower($playerName);
if (!self::isInAnyQueue($playerName)) {
return null;
}

foreach (self::$validActions as $action) {
if (self::isInQueue($playerName, $action)) {
return $action;
}
}

return null;
}
}

0 comments on commit 7515d1b

Please sign in to comment.