<?php

namespace Lunarelly;

use pocketmine\plugin\PluginBase;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\Server;
use pocketmine\event\Listener;
use pocketmine\scheduler\PluginTask;
use pocketmine\math\Vector3;
use pocketmine\level\Level;
use Lunarelly\Events\Chat;
use pocketmine\level\Position;
use pocketmine\utils\Config;
use Lunarelly\Events\AntiBot;
use Lunarelly\Events\EventListener;
use Lunarelly\Events\Query;
use Lunarelly\Tasks\AutoRestart;
use Lunarelly\Tasks\Broadcaster;
use Lunarelly\Tasks\HotBar;

class Main extends PluginBase implements Listener {
	
	public $author = "Lunarelly";
	public $version = "2.0.0";
	
	public function onEnable() {
		$this->getServer()->getPluginManager()->registerEvents(new EventListener($this), $this);
		$this->getServer()->getPluginManager()->registerEvents(new Query($this), $this);$this->getServer()->getPluginManager()->registerEvents(new Chat($this), $this);
		
        $this->getServer()->getPluginManager()->registerEvents(new AntiBot($this), $this);
		$this->getServer()->getScheduler()->scheduleRepeatingTask(new HotBar($this), 20);
		$this->getServer()->getScheduler()->scheduleRepeatingTask(new Broadcaster($this), 1500);
        $this->getServer()->getScheduler()->scheduleRepeatingTask(new AutoRestart($this), 160000);
		$this->chat = new Chat($this);
		$this->config = new Config($this->getDataFolder() . "positions.yml", Config::YAML);
	    }
	
	public function onCommand(CommandSender $p, Command $cmd, $label, array $args) {
        $name = $p->getName();
		switch($cmd->getName()) {
			case "info":
			$p->sendMessage("§a» §fInformation:");
			$p->sendMessage("§7- §fDiscord: §edsc.gg/bluegames");
            break;
            
            case "cc":
            $this->getServer()->broadcastMessage("\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n");
            $this->getServer()->broadcastMessage("§a» §e" . $name ." §fcleared the chat!");
            break;
            
            case "ccme":
            $p->sendMessage("\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n");
            $p->sendMessage("§a» §fYour chat has been cleared!");
            break;
            
            case "donate":
            if(count($args) !== 1) {
            $p->sendMessage("§a» §fRanks:");
            $p->sendMessage("§7- §9Premium §7» §f250 RUB §8(§e3.99 USD§8)");
            $p->sendMessage("§7- §3Deluxe §7» §f500 RUB §8(§e6.99 USD§8)");
            $p->sendMessage("§7- §dPlatinum §7» §f750 RUB §8(§e9.99 USD§8)");
            $p->sendMessage("§7- §fRanks abilities: §e/donate <Rank>");
            return $p->sendMessage("§7- §fRank store: §eshop.drpe.ru §8(§fFor PayPal use: §epaypal.me/lemdyy§8)");
            }
            if($args[0] == "premium") {
            $p->sendMessage("§a» §fRank §9Premium§f:");
            $p->sendMessage("§7- §fClear own chat - §e/ccme");
            $p->sendMessage("§7- §fDeath particles - §e/d");
            $p->sendMessage("§7- §fKit: §9Premium §f- §bSky§fWars");
            $p->sendMessage("§7- §fPerk: §eOre auto-melting §f- §bSky§fWars");
            $p->sendMessage("§7- §fTwo §edouble §fjumps - §cTNT§fRun");
            $p->sendMessage("§7- §fCost: §e250 RUB §8(§e3.99 USD§8)");
            }
            if($args[0] == "deluxe") {
            $p->sendMessage("§a» §fRank §3Deluxe§f:");
            $p->sendMessage("§7- §fParticles - §e/p");
            $p->sendMessage("§7- §fKit: §3Deluxe §f- §bSky§fWars");
            $p->sendMessage("§7- §fRainbow §echestplate §fin main lobby");
            $p->sendMessage("§7- §fPerk: §eAbsorption effect after kill §f- §bSky§fWars");
            $p->sendMessage("§7- §fThree §edouble §fjumps - §cTNT§fRun");
            $p->sendMessage("§7- §fCost: §e500 RUB §8(§e6.99 USD§8)");
            }
            if($args[0] == "platinum") {
            $p->sendMessage("§a» §fRank §dPlatinum§f:");
            $p->sendMessage("§7- §fFly in lobby");
            $p->sendMessage("§7- §fGolden §6GG");
            $p->sendMessage("§7- §fNo §eAnti-AFK");
            $p->sendMessage("§7- §fKit: §dPlatinum §f- §bSky§fWars");
            $p->sendMessage("§7- §fPerk: §eStrength & Regeneration effect after kill §f- §bSky§fWars");
            $p->sendMessage("§7- §fPerk: §eStrength & Regeneration effect after kill §f- §fBed§cWars");
            $p->sendMessage("§7- §fCost: §e750 RUB §8(§e9.99 USD§8)");
            }
            break;
            
            case "gm":
            if(!$p instanceof Player) {
        	return $p->sendMessage("§c» §fOnly for players!");
            }
            if(count($args) !== 1) {
            $p->sendMessage("§a» §fUsage: §a/gm <Mode>");
            }
            if($args[0] == "0") {
            $p->setGamemode(0);
            $p->sendMessage("§a» §fYour new gamemode: §esurvival");
            }
            if($args[0] == "1") {
            $p->setGamemode(1);
            $p->sendMessage("§a» §fYour new gamemode: §ecreative");
            }
            if($args[0] == "2") {
            $p->setGamemode(2);
            $p->sendMessage("§a» §fYour new gamemode: §eadventure");
            }
            if($args[0] == "3") {
            $p->setGamemode(3);
            $p->sendMessage("§a» §fYour new gamemode: §espectator");
            }
            break;
            
            case "status":
            $tps = $this->getServer()->getTicksPerSecond();
            $online = count($this->getServer()->getOnlinePlayers());
            $p->sendMessage("§7- §fOnline: §e" . $online . " §7| §fTPS: §e" . $tps . "");
            break;
            
            case "ci":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
            $p->getInventory()->clearAll();
            break;
            
            case "offfly":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
            $p->setAllowFlight(false);
            $p->setGamemode(0);
            break;
            
            case "hub":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
            if($p->getLevel() === $this->getServer()->getDefaultLevel()) {
            return $p->sendMessage("§c» §fYou are in §emain §flobby already!");
            }
            $worlds = ["sw", "ml", "lbt"];
            $world = $p->getLevel()->getFolderName();
            if(in_array($world, $worlds)) {
            $p->getInventory()->clearAll();
            $p->teleport($this->getServer()->getDefaultLevel()->getSafeSpawn());
            return $p->sendMessage("§a» §aTeleporting you to the main lobby...");
            } else {
            $p->setHealth(0);
            $p->sendMessage("§a» §aTeleporting you to the main lobby...");
            }
            break;
            
            case "spec":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
            if(count($args) !== 1) {
            return $p->sendMessage("§a» §fUsage: §a/spec <Player>\n§7- §fExit: §e/hub");
            }
            $player = $this->getServer()->getPlayer($args[0]);
            if($player != null) {
            $x = $player->getX();
            $y = $player->getY() + 3;
            $z = $player->getZ();
            $name = $player->getName();
            $level = $player->getLevel();
            $p->teleport($level->getSafeSpawn());
            $p->setGamemode(3);
            $p->teleport(new Vector3($x, $y, $z));
            $p->sendMessage("§a» §fYou are spectating: §e" . $name . "\n§7- §fPing: §e100ms");
            } else {
            return $p->sendMessage("§c» §fPlayer not found!");
            }
            break;
            
            case "restart":
            $p->sendMessage("§a» §fServer is restarting...");
            foreach($this->getServer()->getOnlinePlayers() as $p) {
            $p->kick("§fServer is restarting§f!\n§fVK: §e@dragonflype", false);
            $this->getServer()->shutdown();
            }
            break;
            
            case "setpos":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
	        $name = $p->getName();
	        $cfg = $this->config;
	        $x = floor($p->getX());
	        $y = floor($p->getY());
	        $z = floor($p->getZ());
	        $level = $p->getLevel()->getFolderName();
		    if(!isset($args[0])) {
			return $p->sendMessage("§a» §fUsage: §a/setpos <Name>");
		    }
		    $p->sendMessage("§a» §fPosition successfully created!\n§7- §fName: §e{$args[0]}\n§7- §fCoords: §e{$x}, {$y}, {$z} §7| §e{$level}");
		    $params = [
		    "x" => $x,
		    "y" => $y,
		    "z" => $z,
		    "level" => $level
		    ];
		    $cfg->setNested("$args[0]", $params);
		    $cfg->save();
            $cfg->reload();
	        break;
	
	        case "stp":
	        if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
	        $cfg = $this->config;
	        if(!isset($args[0])) {
		    return $p->sendMessage("§a» §fUsage: §a/stp <Name>");
		    }
		    if(isset($args[0])) {
            $mp = $args[0];
		    $map = $cfg->getNested("$mp.level");
		    $x = $cfg->getNested("$mp.x");
		    $y = $cfg->getNested("$mp.y");
		    $z = $cfg->getNested("$mp.z");
            if($cfg->getNested("$mp.level") == null) {
 	        return true;
            }
		    $this->getServer()->loadLevel($map);
            $world = $this->getServer()->getLevelByName($map); 
            $p->teleport($world->getSpawnLocation(), 0, 0);
            $p->teleport(new Position($x, $y, $z));
            break;
            }
            
            case "world":
            if(!$p instanceof Player) {
            return $p->sendMessage("§c» §fOnly for players!");
            }
            if(count($args) !== 1) {
            return $p->sendMessage("§a» §fUsage: §a/world <World>");
            }
            if($this->getServer()->getLevelByName($args[0]) === null) {
            return $p->sendMessage("§c» §fWorld not found!");
            } else {
            $p->sendMessage("§a» §fTeleporting to the world: §e" . $args[0] . "");
            $this->getServer()->loadLevel($args[0]);
            $p->teleport($this->getServer()->getLevelByName($args[0])->getSafeSpawn());
            }
            break;

            case "help":
            $p->sendMessage("§a» §fServer help:");
            $p->sendMessage("§7- §fSupport: §edsc.gg/bluegames");
            break;

            }
            }
            
            
            
            
            public function getAuthor() {
            return $this->author;
            }
            
            public function getVersion() {
            return $this->version;
            }
            }
            ?>