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

Solved Basic Form UI Command

Discussion in 'Development' started by NavyPixel, May 10, 2020.

  1. NavyPixel

    NavyPixel Creeper

    Messages:
    3
    Totally new to minecraft plugins

    I want to type a command ( "/test")

    then be shown a basic modal form with around 4 buttons, with each one to perform its own command (example "/spawn")

    do I need any dependencies for this? and is there a template plugin I can use to build upon?

    I understand the need for a plugin.yml in root where I would set the command and plugin details;

    Code:
    name: MyPlugin
    version: 1.0.0
    author: navypixel
    api: [3.4.0]
    
    main: navypixel\myplugin\MyPlugin
    
    commands:
      test:
        description: OPENS A FORMUI
        usage: /test <usage here>
    
    then the myplugin.php needs to start with the namespace and I guess an OnEnable function.

    if someone has some basic template for the php file I can hopefully work on that and add/remove what is needed.

    thanks
     
  2. KinokiYT

    KinokiYT Spider Jockey

    Messages:
    25
    GitHub:
    kinokiyt
    PHP:
    <?php

    namespace navyPixel\myplugin;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;

    use 
    pocketmine\event\Listener;

    use 
    jojoe77777\FormAPI;
    use 
    jojoe77777\FormAPI\SimpleForm;

    class 
    MyPlugin extends PluginBase implements Listener {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool {
            switch (
    $cmd->getName()) {
                case 
    "test":
                    if(
    $sender instanceof Player) {
                        
    $this->mainMenu($sender);
                    }
                    break;
            }
            return 
    true;
        }
        public function 
    mainMenu($player) {
            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $api->createSimpleForm(function (Player $player, ?int $data) {
                if (
    $data === null) {
                    return 
    true;
                }
                switch(
    $data) {
                    case 
    0:
                    case 
    1:
                    case 
    2:
                    case 
    3:
                        break;
                }
            });
            
    $form->setTitle("Main Form");
            
    $form->addButton("Button 1");
            
    $form->addButton("Button 2");
            
    $form->addButton("Button 3");
            
    $form->addButton("Button 4");
            
    $form->sendToPlayer($player);
            return 
    $form;
        }
    }
    If you want it to execute an existing command when they click one of the buttons, you can use dispatchCommand in the cases.
     
  3. NavyPixel

    NavyPixel Creeper

    Messages:
    3
    legend, I can work with this,

    is it possible for a form button to go to another form (a sub menu with a back button?)
     
  4. KinokiYT

    KinokiYT Spider Jockey

    Messages:
    25
    GitHub:
    kinokiyt
    PHP:
    <?php

    namespace navyPixel\myplugin;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;

    use 
    pocketmine\plugin\PluginBase;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;

    use 
    pocketmine\event\Listener;

    use 
    jojoe77777\FormAPI;
    use 
    jojoe77777\FormAPI\SimpleForm;

    class 
    MyPlugin extends PluginBase implements Listener {
        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
        public function 
    onCommand(CommandSender $senderCommand $cmdstring $label, array $args) : bool {
            switch (
    $cmd->getName()) {
                case 
    "test":
                    if(
    $sender instanceof Player) {
                        
    $this->mainMenu($sender);
                    }
                    break;
            }
            return 
    true;
        }
        public function 
    mainMenu($player) {
            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $api->createSimpleForm(function (Player $player, ?int $data) {
                if (
    $data === null) {
                    return 
    true;
                }
                switch(
    $data) {
                    case 
    0:
                        
    $this->subMenu($player);
                        break;
                    case 
    1:
                    case 
    2:
                    case 
    3:
                        break;
                }
            });
            
    $form->setTitle("Main Form");
            
    $form->addButton("Button 1");
            
    $form->addButton("Button 2");
            
    $form->addButton("Button 3");
            
    $form->addButton("Button 4");
            
    $form->sendToPlayer($player);
            return 
    $form;
        }
        public function 
    subMenu($player) {
            
    $api $this->getServer()->getPluginManager()->getPlugin("FormAPI");
            
    $form $api->createSimpleForm(function (Player $player, ?int $data) {
                if (
    $data === null) {
                    return 
    true;
                }
                switch(
    $data) {
                    case 
    0:
                        break;
                    case 
    1:
                        
    $this->mainMenu($player);
                        break;
                }
            });
            
    $form->setTitle("Sub Form");
            
    $form->addButton("Button 1");
            
    $form->addButton("Back");
            
    $form->sendToPlayer($player);
            return 
    $form;
        }
    }
    You can have one of the buttons named "Back", and put the case to open the mainForm.
     
  5. NavyPixel

    NavyPixel Creeper

    Messages:
    3
    thank you Kino
     
  6. NZSigourney

    NZSigourney Silverfish

    Messages:
    23
    GitHub:
    NZSigourney
    If We make ModalForm for Back then?
     
  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.