I'm attempting to create a plugin that opens a form for a player that lists their enchants as buttons, it also allows them to upgrade the enchant by clicking on a specific button, however I run into an issue. The buttons are created via a foreach loop like so: foreach($newArray as $key => $value) { $message = TextFormat::RED . TextFormat::BOLD . " {$newArray[$key][0]} {$newArray[$key][1]} " . TextFormat::GOLD . TextFormat::BOLD . "--> " . TextFormat::RESET . TextFormat::GREEN . "{$newArray[$key][2]}"; $form->addButton($message); } I'm creating the form through this function: $form = $api->createSimpleForm(function(Player $player, int $data = null) { if($data === null) { return true; } switch($data) { case 0: case 1: case 2: case 3: case 4: if(strpos($data, "Efficiency")) { $tool = $player->getInventory()->getItemInHand(); $efficiency = Enchantment::getEnchantment(15); $effLevel = $tool->getEnchantment(15)->getLevel(); if($effLevel + 1 <= 5) { $effInstance = new EnchantmentInstance($efficiency, $effLevel + 1); $tool->addEnchantment($effInstance); } else { $player->sendMessage(TextFormat::RED . "You already have max level efficiency!"); } } } }); Now I'm aware the strpos won't work as it's an int but would there be another way to do what I'm attempting to do? Thank you.
Example PHP: $form = $api->createSimpleForm(function(Player $player, $data) use (&$textArr) { if($data !== null) { $text = $textArr[$data]; $player->sendMessage($text); }});foreach(['a','b','c','d'] as $text){ $textArr[] = $text; $form->addButton($text);}
Any particular reason why this isn't working? PHP: public function openToolForm(Player $player) { $api = $this->plugin->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $api->createSimpleForm(function(Player $player, int $data) use (&$newArray) { if($data === null) { $text = $newArray[$data]; $player->sendMessage($text); } $text = $newArray[$data]; switch($data) { case $text: if(strpos($text, "Efficiency")) { $tool = $player->getInventory()->getItemInHand(); $efficiency = Enchantment::getEnchantment(15); $effLevel = $tool->getEnchantment(15)->getLevel(); if($effLevel + 1 <= 5) { $effInstance = new EnchantmentInstance($efficiency, $effLevel + 1); $tool->addEnchantment($effInstance); $player->sendMessage("Test"); } else { $player->sendMessage(TextFormat::RED . "You already have max level efficiency!"); } } } });
What do you mean by not working? Is there any error message? You did not add any buttons? Im not sure what you intend to do here? It most likely will not be equal in any situation PHP: switch($data) {case $text:
The buttons added don't actually do anything, I assume it's because I included an unnecessary switch/case statement?
Also would $text be defined the same as it is in if($data === null) because if so you're attempting to find an array in a string. $text isn't defined otherwise.
Im not sure what is the purpose of the switch statement, but it causes problems If the player closed the form ($data = null) usually i would just return there