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

Conversation

Nyrok
Copy link

@Nyrok Nyrok commented Jun 27, 2023

Introduction

Relevant issues

#5767

Changes

API changes

The following method now accepts int $cause parameter. Defaults is 0 for EntityTeleportEvent::CAUSE_PLUGIN.

Entity->teleport(Vector3 $pos, ?float $yaw, ?float $pitch, int $cause) : bool;

Backwards compatibility

No BC-breaking changes, because there is a default parameter to this method signature change.

Nyrok

This comment was marked as outdated.

public const CAUSE_ITEM = 1;
public const CAUSE_WORLD = 2;
public const CAUSE_RESPAWN = 3;
public const CAUSE_PATCH = 4;
Copy link
Member

Choose a reason for hiding this comment

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

This naming is very unintuitive, I have no idea what it's supposed to mean

Copy link
Author

Choose a reason for hiding this comment

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

  • Item: this is when an item cause the teleportation, like chorus fruit or enderpearl
  • World: this is when the world cause teleportation like changing world or being teleported in safe spawn due to world unload
  • Respawn: this is when the player respawn cause teleportation
  • Patch: I think we can merge it with "Respawn" as "Player"
    So, I can suggest this changes:
	public const CAUSE_ITEM = 1;
	public const CAUSE_WORLD = 2;
-	public const CAUSE_RESPAWN = 3;
-	public const CAUSE_PATCH = 4;
+	public const CAUSE_PLAYER = 3;

Copy link
Member

Choose a reason for hiding this comment

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

This is even less clear than the initial, and additionally provides less information.

@@ -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(), null, null, EntityTeleportEvent::CAUSE_ITEM);
Copy link
Contributor

Choose a reason for hiding this comment

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

CAUSE_PROJECTILE seems better

@@ -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), null, null, EntityTeleportEvent::CAUSE_ITEM);
Copy link
Contributor

Choose a reason for hiding this comment

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

CAUSE_CHORUS_FRUIT seems better

@@ -1339,7 +1340,7 @@ protected function processMostRecentMovements() : void{
}

if($to->distanceSquared($ev->getTo()) > 0.01){ //If plugins modify the destination
$this->teleport($ev->getTo());
$this->teleport($ev->getTo(), null, null, EntityTeleportEvent::CAUSE_PATCH);
Copy link
Contributor

Choose a reason for hiding this comment

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

CAUSE_SYNCHRONIZE seems better

Copy link
Member

Choose a reason for hiding this comment

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

CAUSE_PLUGIN would be sufficient.

@Nyrok
Copy link
Author

Nyrok commented Jun 28, 2023

This is specific, but let's see what @dktapps think about it

+ EntityTeleportEvent: Code style improve
@Nyrok Nyrok requested review from dktapps and JavierLeon9966 June 28, 2023 10:46
@@ -89,7 +90,7 @@ public function execute(CommandSender $sender, string $commandLabel, array $args
return true;
}

$subject->teleport($targetPlayer->getLocation());
$subject->teleport($targetPlayer->getLocation(), null, null, EntityTeleportEvent::CAUSE_COMMAND);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
$subject->teleport($targetPlayer->getLocation(), null, null, EntityTeleportEvent::CAUSE_COMMAND);
$subject->teleport($targetPlayer->getLocation(), cause: EntityTeleportEvent::CAUSE_COMMAND);

Seems better

Refering to pmmp#5861 (comment)

Co-Authored-By: ShockedPlot7560  <no-reply@tchallon.fr>
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 🤷‍♂️

@@ -36,10 +36,10 @@ class EntityTeleportEvent extends EntityEvent implements Cancellable{
use CancellableTrait;

public const CAUSE_PLUGIN = 0;
public const CAUSE_ITEM = 1;
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

@jasonw4331 jasonw4331 added Category: API Related to the plugin API Type: Enhancement Contributes features or other improvements to PocketMine-MP labels Jun 29, 2023
@Nyrok
Copy link
Author

Nyrok commented Jul 1, 2023

What's now ? Should be the reason a string or a int (like seeing PlayerPreLoginEvent) ?

@ShockedPlot7560
Copy link
Member

What's now ? Should be the reason a string or a int (like seeing PlayerPreLoginEvent) ?

I think a string would allow greater flexibility. An integer is very restrictive and doesn't give direct information for plugins that want to get the values.

@dktapps
Copy link
Member

dktapps commented Jul 13, 2023

What's now ? Should be the reason a string or a int (like seeing PlayerPreLoginEvent) ?

I think a string would allow greater flexibility. An integer is very restrictive and doesn't give direct information for plugins that want to get the values.

Plugins won't be able to interpret values they don't know about anyway. I think an integer is fine.

Really this should probably use an enum, but I don't want to introduce API inconsistencies.

@@ -35,10 +35,18 @@
class EntityTeleportEvent extends EntityEvent implements Cancellable{
use CancellableTrait;

public const CAUSE_PLUGIN = 0;
public const CAUSE_PROJECTILE = 1;
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.

Joshy3282 added a commit to Joshy3282/PocketMine-MP that referenced this pull request Oct 2, 2024
@dktapps dktapps closed this Nov 14, 2024
@dktapps dktapps added Resolution: Abandoned PR has been abandoned by its author and removed Status: Waiting on Author labels Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: API Related to the plugin API Resolution: Abandoned PR has been abandoned by its author Type: Enhancement Contributes features or other improvements to PocketMine-MP
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants