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

Tutorial Tips & Tricks, Good & Bad Practices

Comments in 'Resources' started by Primus, Mar 21, 2016.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Everybody hates lag, right? There is two main reasons for lag: Hardware and plugins.
    Well upgrading hardware isn't always that cheap and usually out of our possibilities so all we have left is to optimise software and especially plugins. This thread is about how to do it right and how you shouldn't do it.

    Loops are evil
    When loops are executing the thread it runs in will wait for it to stop executing. It's bad idea to call slow/heavy functions in it.

    Lets look at this example:
    PHP:
    // Bad
    // In context of PluginTask
    public function onRun($currentTick)
    {
        foreach(
    $this->getPlugin()->getServer()->getOnlinePlayers() as $player)
        {
            
    $popup $player->getName().' | '.$player->getLevel()->getName().' | Time: '.date('G.i'time());
            
    $player->sendPopup($popup);
        }
    }
    As you can see above name and name of level will always differ from player to player but not Time so we don't have to format it and call time function on each loop. This is how you should do.
    PHP:
    // Good
    // In context of PluginTask
    public function onRun($currentTick)
    {
        
    $date date('G.i'time());
        foreach(
    $this->getPlugin()->getServer()->getOnlinePlayers() as $player)
        {
            
    $popup $player->getName().' | '.$player->getLevel()->getName().' | Time: '.$date;
            
    $player->sendPopup($popup);
        }
    }
    Recommended interval to show constant popup: 10 - 30 ticks. (Needs more testing)

    Strings
    Single or Double quotes?
    String that's wrapped with double quotes will be parsed to find variables and convert their value to readable text. Example:
    PHP:
    $website 'http://forums.pocketmine.net/';
    echo 
    "Visit <a href='$website'>PocketMine Forums</a>";
    Output (HTML Code):
    Code:
    Visit <a href='http://forums.pocketmine.net/'>PocketMine Forums</a>
    
    Output if you used single quotes instead of double:
    Code:
    Visit <a href='$website'>PocketMine Forums</a>
    
    When a string is specified in double quotes or with heredoc, variables are parsed within it. Even if string wrapped with double quotes or with heredoc are empty it still be parsed to search for variable and it takes a really tiny bit amount of time but if you have huge code with strings this time is increased. Next time you create a string, think if you really need double quotes or not. Concatening variables is faster than just putting them in a double-quotation mark string.

    Facts/Tips/Tricks
    • Did you know, string can be as large as up to 2GB (2147483647 bytes maximum) ?
    A string literal can be specified in two more ways:
    Have you ever tried to increment string? (Source)

    PHP:
    $a 'fact_2';
    echo ++
    $a;

    $a '2nd_fact';
    echo ++
    $a;

    $a 'a_fact';
    echo ++
    $a;

    $a 'a_fact?';
    echo ++
    $a;
    Guess what.
    Code:
    fact_3
    2nd_facu
    a_facu
    a_fact?
    

    Surprised? Using an increment operator on a string that ends with a number has the effect of increasing the letter (the one following it in the alphabet, e.g. t -> u). Regardless if the string starts with digits or not, the last character is changed. But there is no effect if the string ends with a non-alphanumeric character.

    This is well documented by the official documentation about the incrementing/decrementing operators but most of you may not have read it because you probably thought nothing special was there. I must admit I thought the same until a short time ago.

    $string[0] / $string{0}

    Strings may also be accessed as arrays but this returns character from index. Example:
    PHP:
    $string 'PocketMine';
    print 
    $string{0}.$string{6}; // PM
    Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 6. Use square brackets instead.
    Last edited: Mar 22, 2016
    PocketKiller likes this.
  2. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    More content as I get spare time to spent.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Loops are evil?
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I know. But you're a bit premature optimization here ;)
    PHP 6 never came out. And in PHP 7, using curly braces still works.

    About single/double quotes, double quotes existed for a reason. The difference you mentioned is extremely negligible. In my recent tests, I obtained a result of {176,170} vs {175, 180}. The maximum difference is 17:18. This is extremely negligible, given that it takes about 0.017 second to run 1000000 times. Many other things would create lag before this one creates lag. That's why I pointed out that you are suggesting premature optimization. Rather than replacing all your quotes, you should be spending your time to find out real major issues that lag the server.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    I'll try :)
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  10. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder

Share This Page

Advertisement