Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to get cause in EntityTeleportEvent (#5767) #5861

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/command/defaults/TeleportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use pocketmine\command\CommandSender;
use pocketmine\command\utils\InvalidCommandSyntaxException;
use pocketmine\entity\Location;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\permission\DefaultPermissionNames;
use pocketmine\player\Player;
Expand Down Expand Up @@ -89,7 +90,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
return true;
}

$subject->teleport($targetPlayer->getLocation());
$subject->teleport($targetPlayer->getLocation(), cause: EntityTeleportEvent::CAUSE_COMMAND);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_tp_success($subject->getName(), $targetPlayer->getName()));

return true;
Expand All @@ -109,7 +110,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
$z = $this->getRelativeDouble($base->z, $sender, $args[2]);
$targetLocation = new Location($x, $y, $z, $base->getWorld(), $yaw, $pitch);

$subject->teleport($targetLocation);
$subject->teleport($targetLocation, cause: EntityTeleportEvent::CAUSE_COMMAND);
Command::broadcastCommandMessage($sender, KnownTranslationFactory::commands_tp_success_coordinates(
$subject->getName(),
(string) round($targetLocation->x, 2),
Expand Down
4 changes: 2 additions & 2 deletions src/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ public function isOnGround() : bool{
/**
* @param Vector3|Position|Location $pos
*/
public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null) : bool{
public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null, int $cause = EntityTeleportEvent::CAUSE_PLUGIN) : bool{
Utils::checkVector3NotInfOrNaN($pos);
if($pos instanceof Location){
$yaw = $yaw ?? $pos->yaw;
Expand All @@ -1433,7 +1433,7 @@ public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null)

$from = $this->location->asPosition();
$to = Position::fromObject($pos, $pos instanceof Position ? $pos->getWorld() : $this->getWorld());
$ev = new EntityTeleportEvent($this, $from, $to);
$ev = new EntityTeleportEvent($this, $from, $to, $cause);
$ev->call();
if($ev->isCancelled()){
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/entity/projectile/EnderPearl.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\entity\projectile;

use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\event\entity\ProjectileHitEvent;
use pocketmine\network\mcpe\protocol\types\entity\EntityIds;
use pocketmine\world\particle\EndermanTeleportParticle;
Expand All @@ -40,7 +41,7 @@ protected function onHit(ProjectileHitEvent $event) : void{

$this->getWorld()->addParticle($origin = $owner->getPosition(), new EndermanTeleportParticle());
$this->getWorld()->addSound($origin, new EndermanTeleportSound());
$owner->teleport($target = $event->getRayTraceResult()->getHitVector());
$owner->teleport($target = $event->getRayTraceResult()->getHitVector(), cause: EntityTeleportEvent::CAUSE_PROJECTILE);
$this->getWorld()->addSound($target, new EndermanTeleportSound());

$owner->attack(new EntityDamageEvent($owner, EntityDamageEvent::CAUSE_FALL, 5));
Expand Down
18 changes: 17 additions & 1 deletion src/event/entity/EntityTeleportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,18 @@
class EntityTeleportEvent extends EntityEvent implements Cancellable{
use CancellableTrait;

public const CAUSE_PLUGIN = 0;
public const CAUSE_PROJECTILE = 1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CAUSE_PROJECTILE doesn't tell us what type of projectile was used. Maybe CAUSE_ENDER_PEARL would be better?

Copy link
Contributor

@JavierLeon9966 JavierLeon9966 Jun 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You know, I think the cause should be a string so plugins can specify their custom causes like "morepearls:snowball". That way we can be specific while also allowing customization

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah agree, with ints its really easy to mess up if someone was to create custom one

public const CAUSE_WORLD = 2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what this is supposed to mean.

public const CAUSE_RESPAWN = 3;
public const CAUSE_CHORUS_FRUIT = 4;
public const CAUSE_COMMAND = 5;

public function __construct(
Entity $entity,
private Position $from,
private Position $to
private Position $to,
private int $cause
){
$this->entity = $entity;
}
Expand All @@ -51,6 +59,14 @@ public function getTo() : Position{
return $this->to;
}

public function getCause() : int{
return $this->cause;
}

public function setCause(int $cause) : void{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it can be used for plugin conflicts 🤷‍♂️

$this->cause = $cause;
}

public function setTo(Position $to) : void{
Utils::checkVector3NotInfOrNaN($to);
$this->to = $to;
Expand Down
3 changes: 2 additions & 1 deletion src/item/ChorusFruit.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

use pocketmine\block\Liquid;
use pocketmine\entity\Living;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\math\Vector3;
use pocketmine\world\sound\EndermanTeleportSound;
use function min;
Expand Down Expand Up @@ -76,7 +77,7 @@ public function onConsume(Living $consumer) : void{

//Sounds are broadcasted at both source and destination
$world->addSound($origin, new EndermanTeleportSound());
$consumer->teleport($target = new Vector3($x + 0.5, $y + 1, $z + 0.5));
$consumer->teleport($target = new Vector3($x + 0.5, $y + 1, $z + 0.5), cause: EntityTeleportEvent::CAUSE_CHORUS_FRUIT);
$world->addSound($target, new EndermanTeleportSound());

break;
Expand Down
7 changes: 4 additions & 3 deletions src/player/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use pocketmine\entity\Skin;
use pocketmine\event\entity\EntityDamageByEntityEvent;
use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\event\inventory\InventoryCloseEvent;
use pocketmine\event\inventory\InventoryOpenEvent;
use pocketmine\event\player\PlayerBedEnterEvent;
Expand Down Expand Up @@ -2387,7 +2388,7 @@ function(Position $safeSpawn) : void{
$ev->call();

$realSpawn = Position::fromObject($ev->getRespawnPosition()->add(0.5, 0, 0.5), $ev->getRespawnPosition()->getWorld());
$this->teleport($realSpawn);
$this->teleport($realSpawn, cause: EntityTeleportEvent::CAUSE_RESPAWN);

$this->setSprinting(false);
$this->setSneaking(false);
Expand Down Expand Up @@ -2487,8 +2488,8 @@ protected function sendPosition(Vector3 $pos, ?float $yaw = null, ?float $pitch
$this->ySize = 0;
}

public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null) : bool{
if(parent::teleport($pos, $yaw, $pitch)){
public function teleport(Vector3 $pos, ?float $yaw = null, ?float $pitch = null, int $cause = EntityTeleportEvent::CAUSE_PLUGIN) : bool{
if(parent::teleport($pos, $yaw, $pitch, $cause)){

$this->removeCurrentWindow();
$this->stopSleep();
Expand Down
3 changes: 2 additions & 1 deletion src/world/WorldManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace pocketmine\world;

use pocketmine\entity\Entity;
use pocketmine\event\entity\EntityTeleportEvent;
use pocketmine\event\world\WorldInitEvent;
use pocketmine\event\world\WorldLoadEvent;
use pocketmine\event\world\WorldUnloadEvent;
Expand Down Expand Up @@ -150,7 +151,7 @@ public function unloadWorld(World $world, bool $forceUnload = false) : bool{
if($safeSpawn === null){
$player->disconnect("Forced default world unload");
}else{
$player->teleport($safeSpawn);
$player->teleport($safeSpawn, cause: EntityTeleportEvent::CAUSE_WORLD);
}
}
}
Expand Down