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

formAPI dropdown players list help

Discussion in 'Development' started by Atomization, Jun 19, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    hey, i'm trying to kick a player using a form menu but it doesn't work
    i have stored each players name in an array in the onJoin function

    PHP:
    $name $player->getName();

    $this->namesArray = [ ];
    array_push($this->namesArray$name);
    Then i loop though all the names in the array and check if the name exists in the array and then kick the player if it does exist.

    PHP:
    foreach($this->namesArray as $name){
        if(
    in_array($name$this->namesArray)){
             
    $name->kick(color::RED.$reason);
        }
    }
    but i get this error:
    Code:
    [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\server\DataPacketReceiveEvent' to 'FormAPI v1.1': Call to a member function kick() on string on jojoe77777\FormAPI\FormAPI
    
    [Server thread/CRITICAL]: Error: "Call to a member function kick() on string" (EXCEPTION) in "__kickUI/src/atomization/Main" at line 207
    
    Full Code:
    PHP:
    public function kickForm($player){
            
    $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $formapi->createCustomForm(function (Player $event, array $data) {

                
    $playerToKick $data[0];
                
    $reason $data[1];

                
    $player $event->getPlayer();

                if(
    $playerToKick == null){
                }   
           
                switch(
    $data){
                    case 
    0:
                        foreach(
    $this->namesArray as $name){
                            if(
    in_array($name$this->namesArray)){
                                
    $name->kick(color::RED.$reason); //line 207
                            
    }
                        }
                }
               
           
            });

            
    $form->setTitle("§o§cKick A Player!");
            
    $form->addDropdown("\n§oSelect Player from list\n"$this->namesArray); //$data[0]
            
    $form->addInput("\nReason:""Type a Reason!"); //$data[1]
            
    $form->sendToPlayer($player);

        }
     

    Attached Files:

    xXNiceAssassinlo YT likes this.
  2. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    It's because the name is a string it needs to be a player instance, do it like that:
    PHP:
    /** @var Server $server */
    $player_get $server->getPlayer($name);
     
    $player_get->kick(color::RED.$reason); //line 207
     
  3. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    So I've edited the code, but nothing happens, no errors ether.
    PHP:
    public function kickForm($player){
            
    $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $formapi->createCustomForm(function (Player $event, array $data) {

                
    $playerToKick $data[0];
                
    $reason $data[1];

                
    $player $event->getPlayer();

                switch(
    $data){
                    case 
    0:
                        
    /** @var Server $server */
                        
    $player_get $server->getPlayer($playerToKick);
                        
    $player_get->kick(color::RED.$reason);
                }
           
            });

            
    $form->setTitle("§o§cKick A Player!");
            
    $form->addDropdown("\n§oSelect Player from list\n"$this->namesArray);
            
    $form->addInput("\nReason:""Type a Reason!");
            
    $form->sendToPlayer($player);

        }
     
  4. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    use /kick player reason instead
     
  5. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    So, ive tried editing the code a little but still didn't work
    PHP:
            $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $formapi->createCustomForm(function (Player $player, array $data) {

              
    $result $data[0];
                
    $reason $data[1];

                if(
    $result != null){
                    
    $player->sendMessage("Null");
                }
       
                switch(
    $result){
                    case 
    0:
                        
    //$this->namesArray is a public variable array: - public $nameArray = [ ];
                        
    foreach($this->namesArray as $name){
                            
    $name $result;
                            
    $name->kick(color::RED.$reason); //line 206
                        
    }

                }
       
            });
    I want $result to = the name of the player that an admin clicks on via the dropdown...
    PHP:
    $form->addDropdown("\n§oSelect Player from list\n"$this->namesArray1);
    ERROR:
    Code:
    Could not pass event 'pocketmine\event\server\DataPacketReceiveEvent' to 'FormAPI v1.1': Call to a member function kick() on integer on jojoe77777\FormAPI\FormAPI
    
    Error: "Call to a member function kick() on integer" (EXCEPTION) in "__kickUI/src/atomization/Main" at line 206
    
     
    Last edited: Jun 20, 2018
  6. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Any possible solutions?
     
  7. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Use var_dump($data) to see what it looms like. I assume that , $data[0] is just an integer that says which option of the dropdown has been selected, I'm not sure though as I've never used FormAPI really much.
    Also if you want $reason to be the name, why do you check if it is zero?
     
    SleepSpace9 and Muqsit like this.
  8. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    I want $result to be the name, not $reason

    Should I use default: instead of case 0:?
    There's no particular value to check for, just the name of the player selected from the drop down
     
  9. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Sorry, I meant $result, I confused their names.

    Just don't use a switch at all, you don't need one, do you?


    Also, don't forgot to change the != here:
    to ===, because you want to abort if it is null, not if it isn't.
    You'll also neet do add either a return at the end of that content of the if or put the other code for kicking into an else, otherwise the code still gets executed even if the user doesn't select a player.

    Edit: Fixed a typo
     
  10. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    PHP:
    $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form $formapi->createCustomForm(function (Player $player, array $data) {

                
    $result $data[0]; //data from the $this->names array in the dropdown
                
    $reason $data[1]; //data from the addInput() for the reason

                
    if($result === null){
                     return;
                } else {
                    
    $this->getServer()->dispatchCommand($player"kick ".$result." ".color::RED.$reason);
                }
           
    });
    when i select the player from the drop down and type a reason, it says "player not found"
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can't call $this inside a callable to access the class from which youre calling the callable.
    Use "use (...$variables)" to call $variables inside the callable.
    PHP:
    $server $this->getServer();
    $form $formapi->createCustomForm(function (Player $player, array $data) use ($server) {
        
    $server->dispatchCommand...
    });
    If you haven't got any errors regarding this issue, then $result is null. Use var_dump() to get the value of $result like what @HimbeersaftLP said.
     
    Atomization and HimbeersaftLP like this.
  12. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    OK, i used var_dump($data); and got this:
    Code:
    array(2) {
      [0]=>
      int(0)
      [1]=>
      string(4) "Test"
    }
    
    SO the 1st item in the $data array needs to = the player from the dropdown, not an integer 0
    PHP:
    $form->addDropdown("\n§oSelect Player from list\n"$this->onlinePlayers1);
    and I'm not sure how to do that... $this->onlinePlayers is the array that stores all online players in the dropdown
    PHP:
    $server $this->getServer();

    $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
    $form $formapi->createCustomForm(function (Player $player, array $data) use ($server){

              
    $result $data[0];
              
    $reason $data[1];

              if(
    $result === null){
                    return;
              } else {
                    
    $server->dispatchCommand($player"kick ".$result." ".color::RED.$reason);
                    
    var_dump($data);
              }
          
    });

    $form->setTitle("§o§cKick A Player!");
    $form->addDropdown("\n§oSelect Player from list\n"$this->onlinePlayers1); //$data[0]
    $form->addInput("\nReason:""Type a Reason!"); //$data[1]
    $form->sendToPlayer($player);
     
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    You can call $server->getOnlinePlayers() to return all players on the server.
     
  14. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    I have then stored all online players in an array and push them into the array onJoin()
    PHP:
    public $onlinePlayers = [ ];
    public function 
    onJoin(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $name $player->getName();

            
    array_push($this->onlinePlayers$name);
    }
     
  15. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Hmm, I'm not familiar with your code structure but this should do.
    PHP:
    $server $this->getServer();
    $onlinePlayers $this->onlinePlayers;
    $form $formapi->createCustomForm(function (Player $player, array $data) use ($server$onlinePlayers) {
        
    //Use $onlinePlayers to get $this->onlinePlayers
        
    $server->dispatchCommand...
    });
     
    Lolipop78709 likes this.
  16. Todo56

    Todo56 Silverfish

    Messages:
    17
    GitHub:
    todo56
    It gives me an error:
    [21:15:49] [Server thread/CRITICAL]: InvalidArgumentException: "Failed to encode form JSON: Recursion detected" (EXCEPTION) in "src/pocketmine/Player" at line 3440
     
  17. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    My old code
    PHP:
            $this->players[$player->getName()] = [];
            foreach(
    Core::get()->getServer()->getOnlinePlayers() as $online){
                if(
    $online->getName() !== null){
                    
    array_push($this->players[$player->getName()], $online->getName());
                }
            }

            
    $form->addDropdown(C::GREEN "Choose Player:"$this->players[$player->getName()]);
     
  18. Todo56

    Todo56 Silverfish

    Messages:
    17
    GitHub:
    todo56
    It gives me an error, the variable is not defined. (The last $player)
     
  19. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Define it than
     
  20. Akmal

    Akmal Spider

    Messages:
    10
    GitHub:
    AkmalFairuz
    I think this code working for you:
    PHP:
    public function kickForm($player){
            
    $formapi $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $formapi->createCustomForm(function (Player $event, array $data) {

                
    $playerToKick $data[0];
                
    $reason $data[1];
                if(!isset(
    $this->namesArray[$player->getId()])) return;

                
    $player $event->getPlayer();

                if(
    $playerToKick == null){
                }

                        
    $target = \pocketmine\Server::getInstance()->getPlayer($this->namesArray[$player->getId()][$playerToKick]);
                        
    $target->kick(color::RED.$reason);
                       unset(
    $this->namesArray[$player->getId()]);
                                   
                            
                
           
       
            });

            
    $form->setTitle("§o§cKick A Player!");
            foreach(\
    pocketmine\Server::getInstance()->getOnlinePlayers() as $pl){
                    if(
    $player->getName() != $pl->getName()){
                            
    $this->namesArray[$player->getId()] = $pl->getName();
            }
            }
            
    $form->addDropdown("\n§oSelect Player from list\n"$this->namesArray[$player->getId()]);
            
    $form->addInput("\nReason:""Type a Reason!"); //$data[1]
            
    $form->sendToPlayer($player);

        }
     
    Last edited: Jun 17, 2019
  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.