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

Add cases according to config.yml

Discussion in 'Development' started by MaxSteel, Apr 14, 2021.

  1. MaxSteel

    MaxSteel Creeper

    Messages:
    1
    Not everything will be added in order because the player needs permission for the option to appear, so even if I put several boxes inside, the button will say something and the box will do something else

    Code:
    ...
    options:
      OptionOne: This is the First Option
      OptionTwo: This is the Second Option
      //and more...
    ...
    PHP:
     
    public function OptionsF ($player) {
           
    $api $this->getServer()->getPluginManager()->getPlugin ("FormAPI");
           
    $form $api->createSimpleForm (function (Player $playerint $data null) {
               
    $result $data;
                if(
    $result === null) {
                    return 
    true;
                }
                switch(
    $result) {
                
    // Add cases with "$options"
                // exemple: case $options: // would generate all the cases that would connect to the buttons.
                // another exemple: if($result === $options) {$player->sendMessage("Selected: ".$options);}
                    
    caso 1:
                        
    // $player->sendMessage("You selected the option: ".$options->get ("msg");
                        
    break;
                    
    caso 2:
                        
    //código
                       
    break;
                
    // another cases... or no xD
                
    }

            });
            
    // Edit Menu
           
    $form->setTitle("Options");
            
    $config $this->getConfig();

            foreach(
    array_keys($config->get("options")) as $options) {
                if(
    $player->hasPermission("option.".$options)) {
                    
    $form->setContent("You options:");
         
                    
    // my configuration has 15 options and I intend to add more, I don't want to leave the giant code and that already solves
                    
    $form->addButton($options);
                    
    //Each button needs a "case", how can I make a case for each one in the same way that it was here?
         
                
    }else{
                    
    $form->setContent("You don't have option.");
                }
            }
           
    $form->sendToPlayer($player);

           return 
    $form;

        }
    }
     
    Last edited: Apr 14, 2021
  2. EndermannbugZJFC

    EndermannbugZJFC Slime

    Messages:
    84
    GitHub:
    endermanbugzjfc
    You can get all the options field in your array and foreach the keys to create button. Then inside the callback part array_values() the
    array and get the value by $result. ($value = $array[$result])
     
  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.