Skip to content

Create a quick feature in Config.yml

VennV edited this page Jul 29, 2024 · 2 revisions

A: I want to add features like input, dropdown, ... to my file, what should I do? ClickedTran: Previously, you wanted to add one or more features like input, dropdown, toggle, ... to your form file, now you can do it through config .yml

Of course you will still need to create a new form file like this:

use venndev\vformoopapi\Form;
use vennvdev\vformoopapi\utils\TypeForm;

use pocketmine\utils\SingletonTrait;

#[VForm(
 title: "Custom Form In Config",
 type: TypeForm::CUSTOM_FORM
)]
final class YourFileName extends Form {
  use SingletonTrait;

   public function __construct(Player $player){
     parent::__construct($player);
     $this->setContent("This is an example for quickly creating menus in config.yml"); //<-- This is the content at 'content' before the class
   }
}

A: So how can I add it? ClickedTran: To add the above features from config.yml to your file, please follow these steps:

Config.yml

type: "input"
message: "You input {message}"

type: "dropdown"
option: ["Option 1", "Option 2"]
message: "You selected {option}"

CustomForm.php

use yourfolder\YourFileName;

use vennvdev\vformoopapi\attributes\custom\{
 VInput,
 vDropDown,
 VToggle
};

class CustomForm {
  public function yourFunctionText(Player $player) : void{
   $yourFileName = YourFileName::getInstance($player);
   
   foreach($cfg->getAll() as $configs){
    if($configs["type"] == "input"){
       $yourFileName->addContent(new VInput(
         text: $configs["text"]
       ), function(Player $player, mixed $data) : void{
            $player->sendMessage(str_replace(["{input}", $data, $cfgs["message"]));
       });
     }
     if($configs["type"] == "dropdown"){
       $yourFileName->addContent(new VDropDown(
        text: $configs["text"],
        options: $configs["option"]
       ), function(Player $player, mixed $data) : void{
         $player->sendMessage(str_replace(["{option}", $data, $configs["message"]));
       });
     }
   }
   $yourFileName->setFormClose(function(Player $player): void {
      $player->sendMessage("You closed the sneaking form!");
   });

   $yourFileName->setFormSubmit(function(Player $player, mixed $data): void {
       $player->sendMessage("You submitted the sneaking form!");
   });
   $yourFileName->sendForm();
  }
}
Clone this wiki locally