- Author: Zenqi
- API: 5.0.0
- Version: 1.0.0
- Time spent: 2 hours
- on
PlayerLogin
the player would be added in jump array
public function onLogin(PlayerLoginEvent $event) {
$player = $event->getPlayer();
$this->jump[$player->getName()] = 0;
}
- on
PlayerQuit
the player would be removed from the jump array
public function onQuit(PlayerQuitEvent $event) {
$player = $event->getPlayer();
if (isset($this->jump[$player->getName()])) {
unset($this->jump[$player->getName()]);
}
}
- on
PlayerJump
the jump array will temporary enable the player permission to fly, and keep track of number of jumps that has maximum of2
so if player stops jumping it would go back to0
, by temporary enabling the permission to fly, the player now can trigger thePlayerToggleFlightEvent
which can make the player jump mid-air without actually toggling the fly function
public function onJump(PlayerJumpEvent $event) {
$player = $event->getPlayer();
$this->jump[$player->getName()]++;
if ($this->jump[$player->getName()] == 1) {
$player->setAllowFlight(true);
}
if ($this->jump[$player->getName()] == 1) $this->getScheduler()->scheduleDelayedTask(new ClosureTask(function () use ($player) : void {
$this->jump[$player->getName()] = 0;
}), 30);
}
- on
PlayerToggleFlightEvent
the direction where the player is will be the direction where the player will jump mid-air, this is a necessary function due to otherplugins
jumping to random sides, usingcosine
andsine
to get the direction of player.
public function onToggle(PlayerToggleFlightEvent $event) {
$player = $event->getPlayer();
# Configurable, recommended: 0.4 - 0.6
$jumpHeight = 0.4;
$jumpDistance = 0.4;
$motionX = -sin(deg2rad($player->getLocation()->getYaw())) * $jumpDistance;
$motionY = $jumpHeight;
$motionZ = cos(deg2rad($player->getLocation()->getYaw())) * $jumpDistance;
$player->setMotion(new Vector3($motionX, $motionY, $motionZ));
$this->jump[$player->getName()] = 0;
$event->cancel();
$player->setAllowFlight(false);
}