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

Help

Comments in 'Plugin Development' started by Primus, May 19, 2015.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    So i need help:

    I want add extra hearts on setMaxHealth() to players screen
    and how to make a txt file to save player's info and read it?
  2. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Use YAML instead of text files
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,472
    Minecraft User:
    PrimusLV
    Yeah but how to generate it? And why not txt? I just need to check if the file exist
  4. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Never do premature optimizations. Yes, checking the existence of a .txt file is faster than parsing a YAML, but you won't notice the tiny difference unless you are using a computer of 20th century.

    YAML support is built into PocketMine. Simply put a key "health-buff" and by getting/setting its boolean value, so you won't have to use the uglier way of checking a magical .txt file.
  5. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    To check if a .yml file exists simply do file_exists("file.extension");
    To create a YAML configuration do:
    PHP:
    $cfg = new Config("file.yml"Config::YAML);
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I think it is a good practice to do optimizations on the most negligible things. Also, it is faster to write a file directly rather than creating an object and trigger many internal methods (especially with the slight function call lag in PHP 5.6).
    Why must it be YAML? Text is OK if you only save one datum.
    And why file_exists? Just put the default value at the third parameter for Config::__construct
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Write to/Overwrite $filename with $contents:
    PHP:
    file_put_contents($filename$contents);
    Get the contents as $contents from $filename:
    PHP:
    $contents file_get_contents($filename);
    For more information, please read http://php.net/manual/en/ref.filesystem.php

    Also a reminder: Convert $contents from string (e.g. "123") to integer (e.g. 123) by doing cast:
    $contents = "123"; // example
    var_dump($contents === 123, $contents); // bool(false), string(3) "123"
    $contents = (int) $contents;
    var_dump($contents === 123, $contents); // bool(true), int(123)

    Casting it into an integer makes your code run faster.
  8. TigerHix
    Offline

    TigerHix Active Member

    Joined:
    May 11, 2015
    Posts:
    61
    Minecraft User:
    TigerHix
    Depend on your needs ;) I believe in "premature optimization is the root of all evil", therefore unless stress tests on my code are failed or have a dissatisfying result, I never go after the extremely tiny performance boost. In this case, OP wanted to add a customization on their plugin, which is an option for enabling the health buff for players. Considering there may have more configurable options afterwards, choosing YAML to read/write values will benefit the scalability and extensibility of the plugin.
    PEMapModder likes this.
  9. CD123XD
    Offline

    CD123XD New Member

    Joined:
    Jul 12, 2014
    Posts:
    13
    Minecraft User:
    RinsibleRocket4
    cant you just put .yml at the end for example (playerinfo.yml)?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    So? And then?
    TigerHix likes this.

Share This Page

Advertisement