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

Broadcast msg only in 1 world

Comments in 'Plugin Development' started by ender_kenji, Nov 4, 2015.

  1. ender_kenji
    Offline

    ender_kenji New Member

    Joined:
    Oct 13, 2015
    Posts:
    29
    Minecraft User:
    EnderKenji
    Any code that can only broadcast msg in 1 world ,, not the whole server show the msg?
  2. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Yes, i have a command called levelbc
    Want a plugin or just a code snippet?
  3. ender_kenji
    Offline

    ender_kenji New Member

    Joined:
    Oct 13, 2015
    Posts:
    29
    Minecraft User:
    EnderKenji
    I just want code :3 thanks :D
  4. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Here you go :D
    PHP:
    $this->getServer()->broadcastMessage($line$this->getServer()->getLevelByName("levelname")->getPlayers());
    PEMapModder and ender_kenji like this.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    Foreach($this->getServer()->getLevelByName($name)->getPlayers() as $player) {
        
    $player->sendMessage("Message");
      }
  6. ender_kenji
    Offline

    ender_kenji New Member

    Joined:
    Oct 13, 2015
    Posts:
    29
    Minecraft User:
    EnderKenji
    T
    Wht pluginbase I should use?
  7. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    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
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    I wouldn't say its bad practice. Everything has more than one method, and this just happens to be one of them.
    hoyinm14mc likes this.
  9. minebuilder0110
    Offline

    minebuilder0110 Notable Member

    Joined:
    Dec 8, 2014
    Posts:
    485
    Minecraft User:
    Apparitional
  10. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    PHP:
    public function levelBroadcast(pocketmine\level\Level $level$message) {
        
    $level->broadcast((string) $message);
    }
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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($messageself::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.
    Hotshot_9930 likes this.
  12. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    @PEMapModder you had some boring time in school today right? xD
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I do that whenever I find something interesting. Fine performance comparison is an example.
    Hotshot_9930 and thebigsmileXD like this.

Share This Page

Advertisement