Here you go PHP: $this->getServer()->broadcastMessage($line, $this->getServer()->getLevelByName("levelname")->getPlayers());
PHP: Foreach($this->getServer()->getLevelByName($name)->getPlayers() as $player) { $player->sendMessage("Message"); }
Everyone would tell you: Bad practice. And thats true. broadcastMessage() already is using foreach, and its alot faster. Its (almost) always better to use internal functions
I wouldn't say its bad practice. Everything has more than one method, and this just happens to be one of them.
PHP: public function levelBroadcast(pocketmine\level\Level $level, $message) { $level->broadcast((string) $message);}
No it isn't. It is the same, but strictly it is slightly faster to do it directly. PocketMine API functions are also written in PHP. It is not like using strpos() vs iterating through the string to search for the position, where strpos() calls an internal function run in C, which is faster than what you do in PHP. PocketMine itself is written in PHP. Therefore, all API functions you call will also be run in PHP. Now, let's look at the source code for broadcastMessage(): PHP: /** * @param string $message * @param Player[]|null $recipients * * @return int */ public function broadcastMessage($message, $recipients = null){ if(!is_array($recipients)){ return $this->broadcast($message, self::BROADCAST_CHANNEL_USERS); } /** @var Player[] $recipients */ foreach($recipients as $recipient){ $recipient->sendMessage($message); } return count($recipients); } This table compares @thebigsmileXD's method against @Hotshot_9930's method: Code: thebigsmileXD | Hotshot_9930 stores getServer()->getLevelByName()->getPlayers() into a value | {SAME} call broadcastMessage() function (function call overhead) | no need to call any extra functions checks if passed $recipients is_array (function call overhead) | nothing to check iterates over $recipients (no difference in terms of performance) | iterates over the value precalculated in step 1 calls sendMessage on each element (same performance) | {SAME} calls count() on $recipients (function call overhead) | no need to compare anything Therefore, @Hotshot_9930's method, strictly, is faster. However, there is actually no difference, and it is easier to write the code in @thebigsmileXD's method although it is slightly slower. But actually, the possible lag in the sendMessage() is much greater than the extra lag in @thebigsmileXD's method (many times greater), which makes function call overhead negligible. In conclusion, there is no difference which method to use. The most notable difference is how fast you code, but there is little difference anyway if you are using an IDE.