Use hasHandlers() for events in player movement processing pathway

this should offer a minor performance improvement.
This commit is contained in:
Dylan K. Taylor 2023-08-23 14:26:17 +01:00
parent cd6abbe0bb
commit d03e4d17ec
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 21 additions and 15 deletions

View File

@ -133,14 +133,18 @@ class HungerManager{
if(!$this->enabled){ if(!$this->enabled){
return 0; return 0;
} }
$ev = new PlayerExhaustEvent($this->entity, $amount, $cause); $evAmount = $amount;
$ev->call(); if(PlayerExhaustEvent::hasHandlers()){
if($ev->isCancelled()){ $ev = new PlayerExhaustEvent($this->entity, $amount, $cause);
return 0.0; $ev->call();
if($ev->isCancelled()){
return 0.0;
}
$evAmount = $ev->getAmount();
} }
$exhaustion = $this->getExhaustion(); $exhaustion = $this->getExhaustion();
$exhaustion += $ev->getAmount(); $exhaustion += $evAmount;
while($exhaustion >= 4.0){ while($exhaustion >= 4.0){
$exhaustion -= 4.0; $exhaustion -= 4.0;
@ -159,7 +163,7 @@ class HungerManager{
} }
$this->setExhaustion($exhaustion); $this->setExhaustion($exhaustion);
return $ev->getAmount(); return $evAmount;
} }
public function getFoodTickTimer() : int{ public function getFoodTickTimer() : int{

View File

@ -1330,18 +1330,20 @@ class Player extends Human implements CommandSender, ChunkListener, IPlayer{
$deltaAngle = abs($this->lastLocation->yaw - $to->yaw) + abs($this->lastLocation->pitch - $to->pitch); $deltaAngle = abs($this->lastLocation->yaw - $to->yaw) + abs($this->lastLocation->pitch - $to->pitch);
if($delta > 0.0001 || $deltaAngle > 1.0){ if($delta > 0.0001 || $deltaAngle > 1.0){
$ev = new PlayerMoveEvent($this, $from, $to); if(PlayerMoveEvent::hasHandlers()){
$ev = new PlayerMoveEvent($this, $from, $to);
$ev->call(); $ev->call();
if($ev->isCancelled()){ if($ev->isCancelled()){
$this->revertMovement($from); $this->revertMovement($from);
return; return;
} }
if($to->distanceSquared($ev->getTo()) > 0.01){ //If plugins modify the destination if($to->distanceSquared($ev->getTo()) > 0.01){ //If plugins modify the destination
$this->teleport($ev->getTo()); $this->teleport($ev->getTo());
return; return;
}
} }
$this->lastLocation = $to; $this->lastLocation = $to;