<?php
use pocketmine\plugin\PluginBase;
use pocketmine\event\Listener;
use pocketmine\command\Command;
use pocketmine\command\CommandSender;
use pocketmine\Player;
use pocketmine\event\entity\EntityLevelChangeEvent;
use pocketmine\event\player\PlayerInteractEvent;
use pocketmine\event\inventory\InventoryCloseEvent;
use pocketmine\event\inventory\InventoryTransactionEvent;
use pocketmine\inventory\ChestInventory;
use pocketmine\item\Item;
use pocketmine\tile\Chest;
use pocketmine\math\Vector3;
use pocketmine\nbt\tag\CompoundTag;
use pocketmine\nbt\tag\IntTag;
use pocketmine\nbt\tag\StringTag;
use pocketmine\block\Block;

class menu extends PluginBase implements Listener{

    private $menus = [];
    private $open = [];

    public function onEnable(){
        $this->getServer()->loadLevel("mlgrushwait");
        $this->getServer()->loadLevel("server");
                $this->getServer()->loadLevel("leaderboards");
        $this->menus["§r§l§bSky§fWars"] = [
            12 => Item::get(Item::DIAMOND)->setCustomName("§r§b§lNexus"),
        ];
        $this->menus["§r§l§cBed§fWars"] = [
            12 => Item::get(Item::EMERALD)->setCustomName("§r§c§lBuckens")
        ];
                $this->menus["§r§l§cDuels"] = [
            12 => Item::get(Item::DIAMOND_SWORD)->setCustomName("§r§c§lHub")
        ];
                $this->menus["§r§l§9The§cBridge"] = [
            12 => Item::get(Item::IRON_SWORD)->setCustomName("§r§c§lBridge Of Hesitation")
        ];
                $this->menus["§r§l§bMLG§fRush"] = [
            12 => Item::get(Item::STICK)->setCustomName("§r§c§lZambia")
        ];
                $this->menus["§r§l§eSumo"] = [
            12 => Item::get(Item::BLAZE_ROD)->setCustomName("§r§e§lSumo")
        ];
                $this->menus["§r§l§aMLG§fBlock"] = [
            12 => Item::get(Item::FEATHER)->setCustomName("§r§a§lFloating Turtle")
                    ];
            // SHOP MENU
$this->menus["§r§l§aShop"] = [
    8 => Item::get(Item::GOLD_INGOT)->setCustomName("§r§d§lPremium"),
    10 => Item::get(Item::IRON_INGOT)->setCustomName("§r§9§lPlatinum"),
    13 => Item::get(Item::DIAMOND)->setCustomName("§r§3§lDeluxe"),
    15 => Item::get(Item::EMERALD)->setCustomName("§r§5§lWarrior"),
];

// PARTICLES MENU (20)
$this->menus["§r§l§dParticles"] = [
    1  => Item::get(Item::DYE, 1)->setCustomName("§9Helix"),
    2  => Item::get(Item::DYE, 2)->setCustomName("§dWings"),
    3  => Item::get(Item::DYE, 3)->setCustomName("§eFire Storm"),
    4  => Item::get(Item::DYE, 4)->setCustomName("§cLoving"),
    5  => Item::get(Item::DYE, 5)->setCustomName("§0Void"),
    6  => Item::get(Item::DYE, 6)->setCustomName("§4Blood Rain"),
    7  => Item::get(Item::DYE, 7)->setCustomName("§bMagic Shield"),
    10 => Item::get(Item::DYE, 8)->setCustomName("§6Lava Drip"),
    11 => Item::get(Item::DYE, 9)->setCustomName("§bFrost Path"),
    12 => Item::get(Item::DYE, 10)->setCustomName("§cExplosion Trail"),
    13 => Item::get(Item::DYE, 11)->setCustomName("§6Double Helix Flame"),
    14 => Item::get(Item::DYE, 12)->setCustomName("§fMagic Halo"),
    15 => Item::get(Item::DYE, 13)->setCustomName("§8Phantom"),
    18 => Item::get(Item::DYE, 14)->setCustomName("§5Great Wings"),
    19 => Item::get(Item::DYE, 15)->setCustomName("§0Atomic Orbit"),
    20 => Item::get(Item::BLAZE_POWDER)->setCustomName("§fHoly Light"),
    21 => Item::get(Item::GHAST_TEAR)->setCustomName("§6Dragon Breath"),
    22 => Item::get(Item::SLIMEBALL)->setCustomName("§bRain Cloud"),
    24 => Item::get(Item::BLAZE_POWDER)->setCustomName("§cUltimate Chaos")
];
        
        
        $this->getServer()->getPluginManager()->registerEvents($this, $this);
    }

    public function onCommand(CommandSender $sender, Command $cmd, $label, array $args){
        
        if($sender instanceof Player){
        	switch($cmd->getName()){
        	case "menushop":
    $this->openMenu($sender, "§r§l§aShop");
    break;
case "mlgrushtesty":
                    $lesva = $sender->getServer()->getLevelByName("mlgrushwait");
                    $sender->teleport($lesva->getSafeSpawn());
                    break;
                    

case "hungergames":

                    //$lesva = $sender->getServer()->getLevelByName("mlgrushwait");

                    $sender->sendMessage("§8(§bB§aG§8)§c Under Development!");

                    break;
case "menupart":
    $this->openMenu($sender, "§r§l§dParticles");
    break;
        	case "menusw":
            $this->openMenu($sender, "§r§l§bSky§fWars");
            break;
            case "menubw":
            $this->openMenu($sender, "§r§l§cBed§fWars");
            break;
                        case "menudu":
            $this->openMenu($sender, "§r§l§cDuels");
            break;
                                    case "menutb":
            $this->openMenu($sender, "§r§l§9The§cBridge");
            break;
                                    case "menutnt":
            $this->openMenu($sender, "§r§l§cTNT§eTag");
            break;
                                    case "menuspe":
            $this->openMenu($sender, "§r§l§bSpleef");
            break;
                                    case "menumb":
            $this->openMenu($sender, "§r§l§aMLG§fBlock");
            break;
                    case "duels":
                    $duelshub = $sender->getServer()->getLevelByName ("leaderboards");
                    $sender->teleport($duelshub->getSafeSpawn());
 $sender->getInventory()->clearAll();    $duelitem = $sender->getServer()->getPluginManager()->getPlugin("LobbyCore");
                    $duelitem->duelsItems($sender->getPlayer());
                    break;
                    case "lboards":
					$this->getServer()->loadLevel("lb");
                    $dlbs = $sender->getServer()->getLevelByName("lb");
                    
                    $sender->teleport($dlbs->getSafeSpawn());

 $sender->getInventory()->clearAll();   
$wwe = $sender->getServer()->getPluginManager()->getPlugin("LobbyCore");
                    $wwe->leadItems($sender->getPlayer());

                    break;
        }
        return true;
    }
    }

    private function openMenu(Player $player, string $menu){
        if(!isset($this->menus[$menu])) return;

        $level = $player->getLevel();
        $x = (int)$player->x;
        $y = (int)$player->y - 3;
        $z = (int)$player->z;

        $level->setBlock(new Vector3($x, $y, $z), Block::get(Block::CHEST), true, true);

        $chunk = $level->getChunk($x >> 4, $z >> 4);

        $nbt = new CompoundTag("", [
            new StringTag("id", "Chest"),
            new IntTag("x", $x),
            new IntTag("y", $y),
            new IntTag("z", $z),
            new StringTag("CustomName", $menu)
        ]);

        $tile = new Chest($chunk, $nbt);
        $level->addTile($tile);

        $inv = $tile->getInventory();
        foreach($this->menus[$menu] as $slot => $item){
            $inv->setItem($slot, $item);
        }

        $this->open[$player->getName()] = [$menu, $tile];
        $player->addWindow($inv);
    }

    public function onClose(InventoryCloseEvent $event){
        $player = $event->getPlayer();
        if(isset($this->open[$player->getName()])){
            $tile = $this->open[$player->getName()][1];
            $pos = $tile->getBlock();
            $tile->getLevel()->setBlock($pos, Block::get(Block::AIR), true, true);
            $tile->getLevel()->removeTile($tile);
            unset($this->open[$player->getName()]);
        }
    }
public function onTransaction(InventoryTransactionEvent $event){
    $transaction = $event->getTransaction();
    $player = $transaction->getPlayer();
    $name = $player->getName();
$inv = $player->getInventory();
    if(!isset($this->open[$name])) return;

    foreach($transaction->getTransactions() as $action){
        $item = $action->getTargetItem();

        if($item->getId() === Item::DIAMOND && $item->getCustomName() === "§r§b§lNexus"){
            $event->setCancelled(true);

$inv->clearAll();
$bed = Item::get(355,0,1);
$bed->setCustomName("§o§cLeave");
$inv->setItem(8, $bed);
$player->getServer()->dispatchCommand($player, "skywars entrar solo");
            return;
        }
        if($item->getId() == Item::IRON_SWORD && $item->getCustomName() === "§r§c§lBridge Of Hesitation"){
        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "bridge join solo");
            return;
            }
        if($item->getId() == Item::TNT && $item->getCustomName() === "§r§c§lBomb Castle"){
        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "tnt join");
            return;
            }
        if($item->getId() == Item::EMERALD && $item->getCustomName() === "§r§c§lBuckens"){
        	            $event->setCancelled(true);
$inv = $player->getInventory();
$player->getServer()->dispatchCommand($player, "bw join");
            return;
            }
       if($item->getId() == Item::FEATHER && $item->getCustomName() === "§r§a§lFloating Turtle"){
        	            $event->setCancelled(true);
$inv = $player->getInventory();
$player->getServer()->dispatchCommand($player, "mlg entrar");
            return;
            }

       if($item->getId() == Item::DIAMOND_SHOVEL && $item->getCustomName() === "§r§e§lNamo"){
        	            $event->setCancelled(true);
$inv = $player->getInventory();
$player->getServer()->dispatchCommand($player, "sumo join");
            return;
    }
    // SHOP RANKS

        if($item->getId() == Item::GOLD_INGOT && $item->getCustomName() === "§r§d§lPremium"){

        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "ranks buy Premium");

            return;

            }

        if($item->getId() == Item::IRON_SWORD && $item->getCustomName() === "§r§c§lBridge Of Hesitation"){

        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "bridge join solo");

            return;

            }
        if($item->getId() == Item::IRON_INGOT && $item->getCustomName() === "§r§9Platinum"){
        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "ranks buy Platinum");
            return;
            }

        if($item->getId() == Item::DIAMOND && $item->getCustomName() === "§r§3§lDeluxe"){

        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "ranks buy Deluxe");

            return;

           }

        if($item->getId() == Item::IRON_SWORD && $item->getCustomName() === "§r§c§lBridge Of Hesitation"){

        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "bridge join solo");

            return;

            }
        if($item->getId() == Item::EMERALD && $item->getCustomName() === "§r§5§lWarrior"){
        	            $event->setCancelled(true);

$player->getServer()->dispatchCommand($player, "ranks buy Warrior");
            return;
            }
if($item->getCustomName() === "§9Helix"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 1");
    return;
}
if($item->getCustomName() === "§dWings"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 2");
    return;
}
if($item->getCustomName() === "§eFire Storm"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 3");
    return;
}
if($item->getCustomName() === "§cLoving"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 4");
    return;
}
if($item->getCustomName() === "§0Void"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 5");
    return;
}
if($item->getCustomName() === "§4Blood Rain"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 6");
    return;
}
if($item->getCustomName() === "§bMagic Shield"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 7");
    return;
}
if($item->getCustomName() === "§6Lava Drip"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 8");
    return;
}
if($item->getCustomName() === "§bFrost Path"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 9");
    return;
}
if($item->getCustomName() === "§cExplosion Trail"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 10");
    return;
}
if($item->getCustomName() === "§6Double Helix Flame"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 11");
    return;
}
if($item->getCustomName() === "§fMagic Halo"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 12");
    return;
}
if($item->getCustomName() === "§8Phantom"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 13");
    return;
}
if($item->getCustomName() === "§5Great Wings"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 14");
    return;
}
if($item->getCustomName() === "§0Atomic Orbit"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 15");
    return;
}
if($item->getCustomName() === "§fHoly Light"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 16");
    return;
}
if($item->getCustomName() === "§6Dragon Breath"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 17");
    return;
}
if($item->getCustomName() === "§bRain Cloud"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 18");
    return;
}
if($item->getCustomName() === "§eHellfire"){
    $event->setCancelled(true);
    $player->getServer()->dispatchCommand($player, "p 19");
    return;
}
if($item->getCustomName() === "§cUltimate Chaos"){
    $event->setCancelled(true);
    $player->removeWindow($action->getInventory());
    $player->getServer()->dispatchCommand($player, "p 20");
    return;
}
    }
}
}