1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved InvMenu Errors

Discussion in 'Development' started by ZackyVN, Nov 18, 2018.

  1. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    When i click on the item it didnt do anything and the inventory doesnt close too. Can someone help me? The code is below
    P/S: Sorry for the messy code. Im Newbie
    PHP:
    <?php

    namespace HyperGames;

    use 
    muqsit\invmenu\{InvMenuInvMenuHandler};
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\Server;
    use 
    pocketmine\Player;
    use 
    pocketmine\command\ConsoleCommandSender;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\event\Listener;

    class 
    Main extends PluginBase implements Listener{

    public function 
    onEnable() : void{
    if(!
    InvMenuHandler::isRegistered()){
    InvMenuHandler::register($this);
    $this->getLogger()->info("§9§l>§aRankShop Enable");
    }
    }

    public function 
    onDisable() : void{
    $this->getLogger()->info("§9§l>§cRankShop Disable");
    }

    public function 
    onCommand(CommandSender $sender,Command $cmdstring $commandLabel, array $args) : bool{
    if(
    $cmd->getName() == "rankshop"){
    $menu InvMenu::create(InvMenu::TYPE_CHEST);
    $shop $menu->getInventory();
    $vip1 Item::get(Item::IRON_BLOCK)->setCustomName("§lVIP-I§e - 10 Coins");
    $vip2 Item::get(Item::GOLD_BLOCK)->setCustomName("§l§eVIP-II - 20 Coins");
    $vip3 Item::get(Item::DIAMOND_BLOCK)->setCustomName("§l§bVIP-III§e - 30 Coins");
    $vip4 Item::get(Item::EMERALD_BLOCK)->setCustomName("§l§aVIP-IV§e - 40 Coins");
    $vip5 Item::get(Item::OBSIDIAN)->setCustomName("§l§5VIP-V§e - 50 Coins");
    $shop->setItem(11$vip1);
    $shop->setItem(13$vip2);
    $shop->setItem(15$vip3);
    $shop->setItem(21$vip4);
    $shop->setItem(23$vip5);
    $menu->readonly();
    $menu->setName("     §l§eHyperGames§a Rank Shop");
    $menu->send($sender);
    $menu->setListener(function(Player $playerItem $itemTakenOutItem $itemPutInSlotChangeAction $inventoryAction) : bool{
    if(!
    $itemTakenOut->isNull()){
    $player->removeWindow($inventoryAction->getInventory());
    }
    if(
    $itemTakenOut->getId() == ITEM::IRON_BLOCK){
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup " $player->getName() . " vip1");
    $this->getServer()->getPluginManager()->getPlugin("Coins")->grantMoney($player->getName(), -10true);
    $this->getServer()->broadcastMessage("§a[§l§eHyperGames§r§a] Bạn§e " $player->getName() . "§a đã mua gói§f§l VIP-I");
    $player->addTitle("Bạn đã mua gói §f§lVIP-I");
    }elseif(
    $itemTakenOut->getId() == ITEM::GOLD_BLOCK){
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup " $player->getName() . " vip2");
    $this->getServer()->getPluginManager()->getPlugin("Coins")->grantMoney($player->getName(), -20true);
    $this->getServer()->broadcastMessage("§a[§l§eHyperGames§r§a] Bạn§e " $player->getName() . "§a đã mua gói§e§l VIP-II");
    $player->addTitle("Bạn đã mua gói §e§lVIP-II");
    }elseif(
    $itemTakenOut->getId() == ITEM::DIAMOND_BLOCK){
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup " $player->getName() . " vip3");
    $this->getServer()->getPluginManager()->getPlugin("Coins")->grantMoney($player->getName(), -30true);
    $this->getServer()->broadcastMessage("§a[§l§eHyperGames§r§a] Bạn§e " $player->getName() . "§a đã mua gói§b§l VIP-III");
    $player->addTitle("Bạn đã mua gói §b§lVIP-III");
    }elseif(
    $itemTakenOut->getId() == ITEM::EMERAlD_BLOCK){
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup " $player->getName() . " vip4");
    $this->getServer()->getPluginManager()->getPlugin("Coins")->grantMoney($player->getName(), -40true);
    $this->getServer()->broadcastMessage("§a[§l§eHyperGames§r§a] Bạn§e " $player->getName() . "§a đã mua gói§a§l VIP-IV");
    $player->addTitle("Bạn đã mua gói §a§lVIP-IV");
    }elseif(
    $itemTakenOut->getId() == ITEM::OBSIDIAN){
    $this->getServer()->dispatchCommand(new ConsoleCommandSender(), "setgroup " $player->getName() . " vip5");
    $this->getServer()->getPluginManager()->getPlugin("Coins")->grantMoney($player->getName(), -50true);
    $this->getServer()->broadcastMessage("§a[§l§eHyperGames§r§a] Bạn§e " $player->getName() . "§a đã mua gói§5§l VIP-V");
    $player->addTitle("Bạn đã mua gói §5§lVIP-V");
    }
    return 
    false;
    });
    }
    return 
    true;
    }
    }
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    $player->removeAllWindows(); maybe?
     
  3. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    and about the Item? When i clicked on it it didnt appear anything
     
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    PHP:
    $player->removeWindow($inventoryAction->getInventory());
    should work, do var_dump($itemTakenOut);

    P.S. You'd probably be getting errors about using $this in that callable.
     
    Last edited: Nov 18, 2018
  5. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    You Need to First Have the Player Open a Window with
    $ sender-> addWindow ($ menu);
    and here below, set the items that go inside ...

    then you have to use the InventoryTransactionEvent to do so that when the player clicks on the item the desired function can be performed and the code that works right is just a Kom
     
  6. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    c
    Cant i use
    PHP:
     $menu->send($sender);?
    What is the different between 
    [PHP$sender->addWindow($menu);
    and
    [
    PHP$menu->send($sender
     
  7. HeyDeniis_

    HeyDeniis_ Baby Zombie

    Messages:
    137
    public $windows = [];
    public function onCommand(CommandSender $sender, Command $cmd, string $lbl, array $a) : bool{
    if($cmd->getName() == "rankshop"){
    $win = new WindowInventory($sender, 27, "§aRank Shop");

    $sender->addWindow($win);
    $this->windows[strtolower($sender->getName())] = $win;
    $p = item::get(item::FEATHER, 0, 20);
    $p->setCustomName("§eNAME");
    $win->setItem(11, $p);
    }
    }
    public function onClick(InventoryTransactionEvent $e){
    $put = $e->getTransaction();
    $p = $put->getPlayer();
    $inv = $this->getWindow($p);
    if(is_null($inv)){
    foreach($put->getInventories() as $inventory){
    if($inventory instanceof WindowInventory){
    $e->setCancelled();
    }
    }
    return true;
    }
    foreach($put->getTransactions() as $action){
    $item = $action->getTargetItem();
    $custom = $item->getCustomName();
    $inv = $this->getWindow($p);
    if(is_null($perm)){
    $p->sendMessage("§aInv Null");
    }
    if($item->getCustomName() == "§eNAME"){
    $e->setCancelled(true);
    //cod of commmand
    $p->removeWindow($inv); //close chest

    }
    }
    }
    public function getWindow(Player $p){
    $name = strtolower($p->getName());
    if(isset($this->windows[$name])){
    return $this->windows[$name];
    }
    return null;
    }
    //To use WindowInventory Download the WindowInventory + WindowHolder
    Dowload
    https://www.sendspace.com/file/jynjzt and
    https://www.sendspace.com/file/mfsm7q
     
  8. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    Thank for helping me :D
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.