PHP: public function onCommand(CommandSender $commandsender, Command $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'
$player = $sender->getPlayer() is incorrect, $sender is already an instanceof Player. You can just use $sender in place of $player.
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 $commandsender, Command $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; }
there's no "should" if it works, you cant say you SHOULD use "AND" not "&&" in the end they are the same
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!"
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
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.
$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).
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();