Hey guys Well, I started to make a simple minigame plugin for my server, and I'm trying to broadcast the minigame messages only on world where the minigame are running... For example, I'm using this code, PHP: public function StartGame(Player $player) { $player->getServer ()->broadcastMessage ( $this->getMsg ( "starting.deathrun-minigame" ) ); // some code } But with this code, the messages are shown on all worlds, (also players can spam the minigame sign, and it's not cool xD) did you guys know what kind of code i can use to limit these minigame messages for just one world? Thanks
You can do: PHP: foreach($this->getServer()->getLevelByName("levelName")->getPlayers() as $p){ $p->sendMessage($this->getMsg ( "starting.deathrun-minigame" ));}
Ok, i tryed out this code, and I got "Call to undefined method vertx\deathrun\Main::getServer()" error, I tryed everything possible to solve but nope, I'm using this code: PHP: foreach( $level->getServer ()->getLevelByName( $levelname )->getPlayers () as $player){ $player->sendMessage( $this->getMsg ( "starting.deathrun-minigame" ) ); (Already tested your "pure" code and got the same issue) Probally I need to register the getSetver () method on the listener, but I don't know how yet, because I started to learn pocketmine API just few days ago... Now i'm testing myself anyway thanks for helping, I'll study a little bit more
Its not: foreach( $level->getServer ()->getLevelByName( $levelname )->getPlayers () as $player){ Correct: foreach($this->getServer ()->getLevelByName( $levelname )->getPlayers () as $player){
Yep, also tryed with $this variable, but nope too... I'll test some more things to see where is the problem... Thanks
$this refers to your main plugin class's context. If you aren't running on the main class, you should replace $this with a variable pointing to your main class. BTW, regarding your main post, you should use: PHP: $msg = $this->getMsg("blah"); // preload this message to optimize performanceforeach($player->getLevel()->getPlayers() as $p){ $p->sendMessage($msg);}
That simply registers an alias. If you don't directly refer to the Server class in the code, that is not necessary. BTW, using an IDE like PHPStorm can help you through these problems.