Skip to content

Commit

Permalink
add pmmp#5861
Browse files Browse the repository at this point in the history
  • Loading branch information
Joshy3282 committed Oct 2, 2024
1 parent 1ca54db commit a01bc4d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Additions
> https://github.com/pmmp/PocketMine-MP/pull/5800
> https://github.com/pmmp/PocketMine-MP/pull/5809
> https://github.com/pmmp/PocketMine-MP/pull/5838
> https://github.com/pmmp/PocketMine-MP/pull/5861

## What is this?
Expand Down
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 @@ -1462,7 +1462,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 @@ -1477,7 +1477,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_ENDER_PEARL);
$this->getWorld()->addSound($target, new EndermanTeleportSound());

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

public const CAUSE_PLUGIN = 0;
public const CAUSE_ENDER_PEARL = 1; // projectile is vague and not needed. ender pearl is the only teleporting projectile
public const CAUSE_CHORUS_FRUIT = 2;
public const CAUSE_COMMAND = 3;
public const CAUSE_DIMENSION_CHANGE = 4;
public const CAUSE_ENTERING_BED = 5; // according to wiki this is teleporting. will not implement for now
public const CAUSE_MOUNT_ENTITY = 6;
public const CAUSE_ENTER_ENTITY = 7;
public const CAUSE_RESPAWN = 8;

// todo possibly adding entities? such as tamed wolves/cats and endermen

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

public function getCause() : int{
return $this->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 @@ -78,7 +79,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 @@ -2509,7 +2510,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 @@ -2615,8 +2616,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 @@ -146,7 +147,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, EntityTeleportEvent::CAUSE_DIMENSION_CHANGE);
}
}
}
Expand Down

0 comments on commit a01bc4d

Please sign in to comment.