Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

broadcast a message only for certain world?

Comments in 'Plugin Development' started by vertx, Mar 28, 2015.

  1. vertx
    Offline

    vertx Active Member

    Joined:
    Dec 4, 2014
    Posts:
    61
    Minecraft User:
    Vertx
    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
    Last edited: Mar 28, 2015
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,710
    Plugins:
    22
    Minecraft User:
    Falkirknh
    You can do:
    PHP:
    foreach($this->getServer()->getLevelByName("levelName")->getPlayers() as $p){
      
    $p->sendMessage($this->getMsg "starting.deathrun-minigame" ));
    }
    vertx likes this.
  3. vertx
    Offline

    vertx Active Member

    Joined:
    Dec 4, 2014
    Posts:
    61
    Minecraft User:
    Vertx
    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 :p anyway thanks for helping, I'll study a little bit more :)
  4. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    you need to add "use pocketmine\Server;"
  5. vertx
    Offline

    vertx Active Member

    Joined:
    Dec 4, 2014
    Posts:
    61
    Minecraft User:
    Vertx
    I'm already using :/
  6. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    Its not: foreach( $level->getServer ()->getLevelByName( $levelname )->getPlayers () as $player){
    Correct: foreach($this->getServer ()->getLevelByName( $levelname )->getPlayers () as $player){
  7. vertx
    Offline

    vertx Active Member

    Joined:
    Dec 4, 2014
    Posts:
    61
    Minecraft User:
    Vertx
    Yep, also tryed with $this variable, but nope too... I'll test some more things to see where is the problem... Thanks :)
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $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 performance
    foreach($player->getLevel()->getPlayers() as $p){
      
    $p->sendMessage($msg);
    }
    TonyDroide and vertx like this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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.
  10. vertx
    Offline

    vertx Active Member

    Joined:
    Dec 4, 2014
    Posts:
    61
    Minecraft User:
    Vertx
    Now it works! :D Really helped a lot, thank you for the tip!
  11. shaan46
    Offline

    shaan46 New Member

    Joined:
    May 13, 2015
    Posts:
    1
    Minecraft User:
    sonic56
    Hey I'm a new person , how do u play ?

Share This Page

Advertisement