When a player is in a particular gamemode it will add to count on HUD This doesn't seem to work? PHP: foreach($this->plugin->getServer()->getOnlinePlayers() as $player){ foreach($player as $p){if($p->isSurvival()){$survival=$survival+1;}} foreach($player as $p){if($p->isSpectator()){$spectator=$spectator+1;}}
Wtf? Why are you foreaching already foreached array? Try this: PHP: foreach($this->getServer()->getOnlinePlayers() as $p){ if($p->getGamemode() == Player::SURVIVAL){ $survival++; } if($p->getGamemode() == Player::SPECTATOR){ $spectator++; }}
Why are you checking if a player is a spectator AFTER you've already checked if they're in survival? PHP: $survival = $spectator = 0;foreach($this->getServer()->getOnlinePlayers() as $p) { if($p->getGamemode() == Player::SURVIVAL) { $survival++; } elseif($p->getGamemode() == Player::SPECTATOR) { $spectator++; }}
A simple gamemode check may not be a big difference in performance but it's bad practice. If the check costs more CPU time then it becomes a performance problem.
This is not a deal as big as you make it sound like. Evaluating the expression $player->getGamemode() == Player::SPECTATOR takes extremely negligible time; focus on the important things and stop complaining about these minor issues and distracting everyone from the main topic. You may say that it's easier to understand the code flow if it is an elseif, but trying to make several posts complaining about it is as pointless as trying to determine if I should write the integral of 1/x as ln(x) or ln|x| at the cost of re-reading the question several times and evaluating the min/max points of the denominator terms (which is unfortunately required in my exam), and equally as pointless as me now trying to determine which words in my post should have the Courier New font.