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

is this right?

Discussion in 'Development' started by Zuruki, Apr 14, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    PHP:
        public function onCommand(CommandSender $commandsenderCommand $command$label, array $args){
            switch(
    $command->getName()){
            case 
    "spectate";
            if(!
    $sender instanceof Player){
            
    $sender->sendMessage("Execute command ingame!");
            } elseif(!isset(
    $args[0]) or is_int($args[0])) {
            
    $sender->sendMessage($prefix "§cUsage: /spectate <player>");
            }
            }elseif(
    $player->getLevel()->getName() == "KitMap1" or $player->getLevel()->getName() == "Lobby" or $player->getLevel()->getName() == "Queue"){
            
    $sender->sendMessage(TF::RED $player "isn't in a 1vs1 match at the moment!");
            }
            }else{
            
    $spectated $this->getServer()->getPlayer($args[0]);
            
    $player $sender->getPlayer();
            
    $player->setGamemode(3);
            
    $player->teleport($spectated);
            }
        }
        return 
    true;
    when I use a syntax checker I get an 'unexpected elseif'
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    You shouldn't put two closing brackets before else and elseif.
     
  3. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    ah, thankyou ;). Any errors with my code though, or is it just syntax?
     
  4. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    There should be a : and not a ;
     
  5. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    both works AFAIK, just code style
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    It works, yes, that's why I said "should" but the default variant is with a :

    switch docs
     
    Last edited: Apr 15, 2017
  7. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    I fixed syntax, however it's still not working. no errors...
     
  8. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    Undefined Variable "$player"
     
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    $player = $sender->getPlayer() is incorrect, $sender is already an instanceof Player. You can just use $sender in place of $player.
     
    XdmingXD and Kyd like this.
  10. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    His code is even wronger, cuz he defined $commandsender and not $sender
     
  11. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It is correct, but useless. I don't know why it even is a function in Player::class.

    Line 3: Replace the semi-colon with a colon.
    Line 4: $sender is not defined.
    Line 9: Organize it. Anyway, $player is undefined.
    PHP:
    $blacklisted = ["KitMap1""Lobby""Queue"];
    if(
    in_array($player->getLevel()->getName(), $blacklisted)){
        
    /*Line10: FIX*/$commandsender->sendMessage(TF::RED $player->getName() . "isn't in a 1vs1 match at the moment!");
    }
    TIP: Format your code so that it'll make the readers understand it easily.
    PHP:
        public function onCommand(CommandSender $commandsenderCommand $command$label, array $args){
            switch(
    $command->getName()){
                case 
    "spectate";
                    if(!
    $commandsender instanceof Player){
                        
    $commandsender->sendMessage("Execute command ingame!");
                    } elseif(!isset(
    $args[0])) {
                        
    $commandsender->sendMessage($prefix "§cUsage: /spectate <player>");
                    }
                    
    $player $this->getServer()->getPlayer($args[0]);
                    if(
    $player === null){
                        
    $commandsender->sendMessage("Player is offline.");
                        return 
    false;
                    }
                    
    $blacklisted = [
                        
    "KitMap1",
                        
    "Lobby",
                        
    "Queue"
                    
    ];
                    if(
    in_array($player->getLevel()->getName(), $blacklisted)){
                        
    $commandsender->sendMessage(TF::RED $player "isn't in a 1vs1 match at the moment!");
                    }else{
                        
    $commanssender->setGamemode(3);
                        
    $commandsender->teleport($player);
                    }
            }
            return 
    true;
        }
     
    Zuruki likes this.
  12. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    there's no "should" if it works, you cant say you SHOULD use "AND" not "&&"
    in the end they are the same
     
    HimbeersaftLP and Primus like this.
  13. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    I think his intention was for cleanliness and proper code. AFAIK not many people use the semi-colon. Is it a way? Yes. Is it bad practice? Also yes. When teaching someone something, it's good to teach them a proper way, instead of saying "Hey, remember how I told you that you can do this? Well... it is really bad practice so stop doing that and do this instead!"
     
    Zuruki likes this.
  14. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    here we go again arguing over syntax and code style... AGAIN
    i dont consider it "bad" since compiler threats them in the same manner on the surface view

    maybe you can say something is bad if someone uses dispatch command to interact with another plugin and you recommend them to use the plugin API instated which i see no problem of just using alternative syntax

    cleanliness is a matter of consistency or just opinion of yourself that what's matter most to have a clean codestyle

    you are right not a lot of people use semi-colon but that dosent invalidate it or it should be shunned for using it

    i dont believe why a syntax can result of a bad programming practice, you should back your claims up with reasoning, dont expect everyone to know what you meant, it's like saying if you use && over AND means it's a bad practice without reasonings it make no sense but if you mention lets say a edge case of precedence differences then you may be correct, but as for now i still dont get why
     
    jasonwynn10 and EdwardHamHam like this.
  15. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    I'm not shunning anything whatsoever, but I do agree to say cleanliness is 99% consistency. My only problem is the semi-colon is rarely documented in switch statements, so if he has errors and confronts others with the code, not the error itself, the person he confronts could be quick to see the semi-colon and tell him that it is the problem. Again, can you do it? Yes, but using good practice will help for other developers to read and understand the code. Like you said, && vs AND or || vs OR statements are very similar, but they do have the precedence differences as stated before. So, while there is nothing wrong with using the semi-colon, it can cause problems when being read by other developers who had no idea that the semi-colon can be used instead of the colon. Another case would be if he had multiple cases and alternated between the semi-colon and the colon. That would nullify the cleanliness because as you said before, "cleanliness is a matter of consistency." My last scenario would be if he had trouble using the switch statement and consulted the PHP manual and saw everyone using colons(although it is documented under the same page that you CAN use the semi-colon), but he was using the semi-colon. It's just really confusing altogether. So, while it may not matter in the whole scheme of things, learning good practice now just saves everyone from headaches in the future.


    EDIT: Just this page alone caused lots of confusion as someone told him that his syntax was wrong, when it was just a case of undefined variables.
     
    Last edited: Apr 15, 2017
  16. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    thankyou everyone for helping me! however I have a question.
    $sender returns a player object...?
     
  17. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    @Muqsit you're really helpful, thanks to you for this !
     
    Muqsit likes this.
  18. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    $sender won't always be a player. That's why you use if($sender instanceof Player), because then the code below will only be run if the commandsender is a Player object. $sender can also be the console(ConsoleCommandSender, I think).
     
    HimbeersaftLP likes this.
  19. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Interesting. It sounded familiar, but I assumed it didn't exist because of how useless it appears to be.
    PHP:
     /** @var Player $player */
     
    $player->getPlayer()->getPlayer()->getPlayer()->getPlayer()->getPlayer(); 
     
    HimbeersaftLP, Muqsit and jasonwynn10 like this.
  20. Indexfire

    Indexfire Baby Zombie

    Messages:
    137
    GitHub:
    Indexfire
    ???
     
  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.