Hello, I’ve written some code for a repair ui, but it doesn’t work. I’ve contacted some other devs and they don’t know whats up with it either. Any help is appreciated... PHP: public function repairForm($player){ $options = []; foreach ($player->getInventory()->getContents() as $key => $content){ if($content instanceof Durable) $options[$key] = $content->getName(); } $form = $this->plugin->createCustomForm(function (Player $player, array $data) use ($options) { if(isset($data[1])){ var_dump([array_keys($options)]); $slot = $options[array_keys($options)[$data[1]]]; } }); $form->setTitle("§c--= §l§dBlacksmith§r§c =--"); $form->addLabel("Select an item below for repairing."); $form->addDropdown("Select an item below that youd like to repair.", $options); $form->sendToPlayer($player);}}
"it doesn't work" isn't certainly the most precise description of a problem. How does it not work? Does it display an error? (If so, what does it say?) Does it not open the Form? Does the Form not respond?
$items = $player->getInventory()->getContents(); foreach($items as $key => $item){ $form->addDropdown("Select an item below that youd like to repair.", [$item->getName()]); }
#use pocketmine\item\{ItemFactory, Item}; public function repairForm($player){ $formapi = $this->plugin->getServer()->getPluginManager()->getPlugin("FormAPI"); $form = $formapi->createCustomForm(function(Player $player, $data = null){ $options = $data[1]; if($options != null){ switch($options){ case 1: $player->sendMessage("testing..."); break; } } $form->setTitle("§c--= §l§dBlacksmith§r§c =--"); $form->addLabel("Select an item below for repairing."); $items = $player->getInventory()->getContents(); foreach($items as $key => $item){ $form->addDropdown("Select an item below that youd like to repair.", [$item->getName()]); } $form->sendToPlayer($player); } }
That does not work at all ;-;. I tried again with the method you gave and the only problem was that it was opening a form for every item in the player’s inventory...
PHP: public function repairForm($player){ $options = []; $items = $player->getInventory()->getContents();foreach($items as $key => $item){if($item instanceof Durable){ } $form = $this->plugin->createCustomForm(function (Player $player, array $data) use ($item) { if(isset($data[1])){ $slot = $options[array_keys($options)[$data[1]]]; } }); $form->setTitle("§c--= §l§dBlacksmith§r§c =--"); $form->addLabel("Select an item below for repairing.");$form->addDropdown("Select an item below that youd like to repair.", [$item->getName()]); $form->sendToPlayer($player);}}}
PHP: public function repairForm(Player $player){ $options = []; $optionsReference = []; foreach ($player->getInventory()->getContents() as $key => $content){ if($content instanceof Durable){ $options[] = $content->getName(); $optionsReference[$key] = $content->getName(); } } $form = $this->plugin->createCustomForm(function (Player $player, array $data) use ($optionsReference) { if(isset($data[1])){ $slot = array_keys($optionsReference)[$data[1]]; } }); $form->setTitle("§c--= §l§dBlacksmith§r§c =--"); $form->addLabel("Select an item below for repairing."); $form->addDropdown("Select an item below that you'd like to repair.", $options); $form->sendToPlayer($player); } Arrays with associative keys count as objects :/