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

Correct command usage format

Discussion in 'Development' started by rektpixel, Jan 11, 2018.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hey I have this code:
    PHP:
        public function onCommand(CommandSender $playerCommand $command$label, array $args): bool{
            switch(
    $command->getName()){
                case 
    "sg":
                if(!empty(
    $args[0])){
                    switch(
    $args[0]){
                        case 
    "make":
                        if(
    $player->isOp()){
                            if(!empty(
    $args[1])){
                                if(
    file_exists($this->getServer()->getDataPath() . "/worlds/" $args[1])){
                                    
    $this->getServer()->loadLevel($args[1]);
                                    
    $this->getServer()->getLevelByName($args[1])->loadChunk($this->getServer()->getLevelByName($args[1])->getSafeSpawn()->getFloorX(), $this->getServer()->getLevelByName($args[1])->getSafeSpawn()->getFloorZ());
                                    
    array_push($this->arenas$args[1]);
                                    
    $this->currentLevel $args[1];
                                    
    $this->mode 1;
                                    
    $player->sendMessage("§eYou are now in §6SETUP MODE§r§e. Tap a block to set the first spawn point.");
                                    
    $player->setGamemode(1);
                                    
    $player->teleport($this->getServer()->getLevelByName($args[1])->getSafeSpawn(), 00);
                                }else{
                                    
    $player->sendMessage("§c[ERROR] §7Couldn't find a world with this name");
                                }
                            }else{
                                
    $player->sendMessage(TextFormat::YELLOW "/sg make <world> §7make an sg arena");
                            }
                        }else{
                            
    $player->sendMessage("§c[ERROR] You don't have permission to use this command.");
                        }
                   break;
                   case 
    "join":
                   if(!empty(
    $args[0])){
                       if(!empty(
    $args[1])){
                           if(
    file_exists($this->getServer()->getDataPath() . "/worlds/" $args[1])){
                               
    $this->getServer()->loadLevel($args[1]);
                               
    $this->getServer()->getLevelByName($args[1])->loadChunk($this->getServer()->getLevelByName($args[1])->getSafeSpawn()->getFloorX(), $this->getServer()->getLevelByName($args[1])->getSafeSpawn()->getFloorZ());
                               
    array_push($this->arenas,$args[1]);
                               
    $this->currentLevel $args[1];
                               
    $this->mode 1;
                                  
                               
    $level $this->getServer()->getLevelByName($this->currentLevel);
                               
    $levelArena $level;
                               
    $playersArena $levelArena->getPlayers();
                               
    $gamecount count($this->getServer()->getLevelByName($this->currentLevel)->getPlayers());
                               
    $this->currentLevel $level;
                                  
                               
    $player->setFood(20);
                               
    $player->setHealth(20);
                               
    $player->setGamemode(0);
                               
    $level $this->currentLevel;
                               
    $x = -963;
                               
    $y 33;
                               
    $z 837;
                               
    $pos = new Vector3($x,$y,$z);
                               
    $player->teleport($level->getSafeSpawn($pos));
                               
    $levelArena $level;
                               
    $playersArena $levelArena->getPlayers();
                               
    $gamecount count($this->getServer()->getLevelByName($args[1])->getPlayers());
                               foreach(
    $playersArena as $pl)
                               
    $pl->sendMessage(TextFormat::YELLOW $player->getNameTag() . TextFormat::YELLOW " joined the game. (" TextFormat::AQUA $gamecount TextFormat::RESET TextFormat::AQUA "/24" TextFormat::YELLOW ")");
                           }
                        }else{
                            
    $player->sendMessage(TextFormat::YELLOW "/sg join <world> §7join and sg game");
                        }
                    }else{
                        
    $player->sendMessage(TextFormat::YELLOW "/sg make <world> §7make an sg arena");
                        
    $player->sendMessage(TextFormat::YELLOW "/sg join <world> §7join an sg game");
                    }
                break;
                    }
                }
            }
            return 
    true;
        }
    (/sg join is incomplete) Everything works fine. however, when the player types /sg I want it to show those last 2 messages in the code. but it won't show anything.
     
    Last edited: Jan 11, 2018
  2. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    To start you should clean up your code because that is not understandable (at least for me). Too many else loops there
     
    rektpixel and Teamblocket like this.
  3. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    but the else's actually do something. there not there to look good. :eek:
     
  4. Rateek

    Rateek Silverfish

    Messages:
    15
    GitHub:
    RATEEK
    "CommandSender $player, Command $command, $label, array $args"

    Look new API.
     
  5. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Refer to the CommandExecutor interface.
     
  6. iiFlamiinBlaze

    iiFlamiinBlaze Witch

    Messages:
    59
    GitHub:
    iiflamiinblaze
    Why CommandExecutor...

    Alpha7 typehint changes, add string before $label.
     
  7. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    The CommandExecutor interface has the proper onCommand() syntax.
     
    Last edited: Jan 22, 2018
  8. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    If the command is long like that, I prefer to use
    PHP:
    YourCommandClass extends Command{
         public function 
    __construct(){
                
    parent::__construct("command""description""usage", ["aliases"]);
         }

         public function 
    execute(CommandSender $senderstring $label, array $args){
             
    // Your command code here
            // Also for args, use switch($args[1]){ // body }
         
    }
    }
    Now register the command so it can be used
    PHP:
    $this->getServer()->getCommandMap()->register("Your_command_name", new YourCommandClass());
     
  9. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    I would only do that if my plugin has two or more commands.
     
  10. Az928

    Az928 Baby Zombie

    Messages:
    140
    GitHub:
    theaz928
    Depends on what you want to do... but pmmp registers plugin commands similar to that...Just a short cut...
     
  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.