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); }
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
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); }
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->namesArray, 1); 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
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?
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
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
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"
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.
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->onlinePlayers, 1); 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->onlinePlayers, 1); //$data[0]$form->addInput("\nReason:", "Type a Reason!"); //$data[1]$form->sendToPlayer($player);
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);}
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...});
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
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()]);
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); }