<?php

namespace LobbyFly;

use pocketmine\plugin\PluginBase;
use pocketmine\Player;
use pocketmine\event\Listener;
use pocketmine\event\entity\EntityLevelChangeEvent;
use pocketmine\event\player\PlayerMoveEvent as move;

class Main extends PluginBase implements Listener{
	
	public function onEnable(){
     $this->getServer()->getPluginManager()->registerEvents($this, $this);
	}


public function onChange(EntityLevelChangeEvent $e){
$p = $e->getEntity();
if($p instanceof Player){
if($p->getGamemode() == 1 or $p->getGamemode() == 3) return;
$o = $e->getOrigin();
$t = $e->getTarget();
if($o == $this->getServer()->getDefaultLevel()){
if($t->getFolderName() !== $this->getServer()->getDefaultLevel()){
$p->setAllowFlight(false);
return;
}
}
}
}

public function onMove(move $e){
$p = $e->getPlayer();
if($p->getGamemode() == 1 or $p->getGamemode() == 3) return;
if($p->getLevel() !== $this->getServer()->getDefaultLevel()){
$p->setAllowFlight(false);
}
if($p->getLevel() === $this->getServer()->getDefaultLevel()){
$p->setGamemode(2);
	if(!$p->hasPermission("use.fly")) return;
$p->setAllowFlight(true);
}
}
}