<?php

namespace Lunarelly\Events;

use pocketmine\Player;
use pocketmine\Server;
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\entity\Effect;
use pocketmine\math\Vector3;
use pocketmine\event\block\{BlockPlaceEvent, BlockBreakEvent};
use pocketmine\event\player\{PlayerJoinEvent, PlayerQuitEvent, PlayerDeathEvent, PlayerExhaustEvent, PlayerPickupExpOrbEvent, PlayerDropItemEvent, PlayerRespawnEvent, PlayerMoveEvent, PlayerInteractEvent, PlayerPreLoginEvent};
use pocketmine\event\entity\{EntityDamageEvent, EntityExplodeEvent, EntityDamageByEntityEvent};
use Lunarelly\Main;

class EventListener implements Listener {
    public $pp;
	
	public function __construct(Main $main) {
		$this->main = $main;
		}
		
		public function onDamage(EntityDamageEvent $e) { 	
                  $p = $e->getEntity();
                  if($e->getCause() == EntityDamageEvent::CAUSE_FALL) {
                  $map = $p->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "tnt3", "blobby", "blobby2", "blobby3", "blobby4", "Bricks", "Stones", "Clay", "Nether", "Bridge", "Bridge2", "Bridge3", "Bridge4", "Doom"];
                  if(in_array($map, $servers)) {
                  $e->setCancelled();
                  }
                  }
                  if($e instanceof EntityDamageByEntityEvent) {
              	  $d = $e->getDamager();
                  if(!$p instanceof Player) {
              	  return;
                  }
                  if(!$d instanceof Player) {
                  return;
                  }
                  $map = $d->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "tnt3", "blobby", "blobby2", "blobby3", "blobby4"];
                  if(in_array($map, $servers)) {
                  $e->setCancelled();
                  }
                  if(!in_array($map, $servers)) {
                  $hp = $p->getHealth();
                  $maxhp = $p->getMaxHealth();
                  $d->sendPopup("§8[§b" . $hp . "§8/§b" . $maxhp . "§8]§r");
                  if($p->hasPermission("gapi.distance")) {
                  $name = $d->getName();
                  $distance = $d->distance($p);
                  $p->sendPopup("§f" . $name . ": §b" . $distance . "§r");
                  }
                  }
                  if($p->getGamemode() === 1 and $d->getGamemode() === 0) {
                  $e->setCancelled();
                  return;
                  }
                  if($p->getGamemode() === 0 and $d->getGamemode() === 1) {
                  $e->setCancelled();
                  return;
                  }
                  if($p->getGamemode() === 1 and $d->getGamemode() === 2) {
                  $e->setCancelled();
                  return;
                  }
                  if($p->getGamemode() === 2 and $d->getGamemode() === 1) {
                  $e->setCancelled();
                  return;
                  }
                  if($p->getAllowFlight(true)) {
                  $e->setCancelled();
                  return;
                  }
                  if($d->getAllowFlight(true)) {
                  $e->setCancelled();
                  return;
                  }
                  }
                  }

                  public function onPreLogin(PlayerPreLoginEvent $e) {
                  $p = $e->getPlayer();
                  foreach($this->main->getServer()->getOnlinePlayers() as $players) {
                  if(count($players) >= 200) {
                  $p->kick("§fСервер §bзаполнен§f!\n§fВК: §e@dragonflype");
                  }
                  if(strtolower($players->getName()) == strtolower($p->getName())) {
                  $p->kick("§fЭтот §bигрок §fуже онлайн!\n§fВК: §e@dragonflype", false);
                  }
                  }
                  }

                  public function onJoin(PlayerJoinEvent $e) {
                  $p = $e->getPlayer();
                      
 $p->addEffect(Effect::getEffect(1)->setAmplifier(50)->setDuration(99999999)); 
                       $p->addEffect(Effect::getEffect(9)->setAmplifier(10)->setDuration(99999999)); 
                       $p->addEffect(Effect::getEffect(15)->setAmplifier(50)->setDuration(99999999)); 
                      
 $p->addEffect(Effect::getEffect(14)->setAmplifier(1)->setDuration(99999999)); 
		    	$this->pp = $this->main->getServer()->getPluginManager()->getPlugin("PurePerms");
                      $group = $this->pp->getUserDataMgr()->getGroup($p)->getName();
           
                      if($group === "Owner"){
                          $p->getServer()->broadcastMessage("§8(§bB§aG§8) §fServer Owner §c@{$p->getName()} §fjoined the server.");}
			      $e->setJoinMessage(null);
			
			      $p->removeAllEffects();
			      $p->teleport($this->main->getServer()->getDefaultLevel()->getSafeSpawn());
			      $p->setMaxHealth(20);
                  $p->setHealth(20);
                  $p->setFood(20);
                  $p->setGamemode(2);
                  }

                  public function onQuit(PlayerQuitEvent $e) {
                  $e->setQuitMessage(null);
                  }

                  public function onDeath(PlayerDeathEvent $e) {
                  $e->setDeathMessage(null);
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "blobby", "blobby2", "blobby3", "blobby4"];
                  if(in_array($server, $servers)) {
                  $e->setDrops([]);
                  }
                  }

                  public function onExplode(EntityExplodeEvent $e) {
                  $e->setCancelled();
                  }

                  public function onRespawn(PlayerRespawnEvent $e) {
                  $p = $e->getPlayer();
                  $p->setMaxHealth(20);
                  $p->setHealth(20);
                  $p->setFood(20);
                  $p->setGamemode(2);
                  }

                  public function onDrop(PlayerDropItemEvent $e) {
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
               	  $servers = ["server", "lb", "ml", "leaderboards", "blobby", "blobby2", "blobby3", "blobby4"];
                  if(in_array($server, $servers)) {
                  $e->setCancelled();
                  }
                  }

                  public function onExhaust(PlayerExhaustEvent $e) {
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "blobby", "blobby2", "blobby3", "blobby4", "tnt3"];
                  if(in_array($server, $servers)) {
                  $e->setCancelled();
                  }
                  }

                  public function onExp(PlayerPickupExpOrbEvent $e) {
                  $e->setCancelled();
                  }

                  public function onMove(PlayerMoveEvent $e) {
                  $p = $e->getPlayer();
                  $p->getLevel()->setTime(0);
                  $server = $this->main->getServer()->getDefaultLevel();
                  if($p->getLevel() == $server){
                  if($p->getY() < 0){
                  return $p->teleport($server->getSafeSpawn());
                  }
                  $distance = $p->distance($server->getSafeSpawn());
                  if($distance > 100) {
                  return $p->teleport($server->getSafeSpawn());
                  }
                  }
                  $wrld = $p->getLevel()->getFolderName();
                  $servers = ["lb", "ml", "leaderboards"];
                  if(in_array($wrld, $servers)) {
                  if($p->getY() < 0) {
                  $p->getInventory()->clearAll();
                  return $p->teleport($server->getSafeSpawn());
                  }
                  }
                  }
                  
                  public function onPlace(BlockPlaceEvent $e) {
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "blobby", "blobby2", "blobby3", "blobby4"];
                  if(in_array($server, $servers)) {
                  $e->setCancelled();
                  }
                  }
                  
                  public function onBreak(BlockBreakEvent $e) {
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
                  $servers = ["server", "lb", "ml", "leaderboards", "blobby", "blobby2", "blobby3", "blobby4"];
                  if(in_array($server, $servers)) {
                  $e->setCancelled();
                  }
                  }
                  
                  public function onInteract(PlayerInteractEvent $e) {
                  $p = $e->getPlayer();
                  $server = $p->getLevel()->getFolderName();
                  $servers = ["server"];
                  if(in_array($server, $servers)) {
                  $e->setCancelled();
                  }
                  }
                  }
                  ?>