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

Tutorial Using Configs

Discussion in 'Resources' started by Jack Noordhuis, Apr 24, 2017.

  1. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Verified

    Messages:
    556
    GitHub:
    JackNoordhuis
    Far too often I've seen people manually saving, creating and writing to files within their plugins when easier ways exist. We can thank our PocketMine developer overlords for creating the pocketmine/utils/Config class, it's a great way to handle data saving within a plugin.

    The first thing you need to do when using config files is to make sure your plugin’s data folder exists, we can easily achieve this from our PluginBase class:
    PHP:
    @mkdir($this->getDataFolder());
    Next, we need to create or save the resource, we can achieve this easily with one line of code from our main class:
    PHP:
    $this->saveResource("MyConfig.yml");
    I usually save all my resources in the PluginBase::onEnable() method to make sure they exist every time my plugin is enabled. You can also skip the manual data folder creation by creating default resources in your plugin’s /resources/ directory and calling the PluginBase::saveResource() method.

    The next thing you need to do is figure out when and how you will be accessing your config, if you are only using your config for settings or a simple data storage solution I'd recommend creating a new config instance and storing it in a class property on the relevant class:
    PHP:
    $this->myConfig = new Config($this->getDataFolder() . "MyConfig.yml"Config::YAML);
    This code will store a new config instance into a class property that we can easily access at any time so we can easily save and read data. It is also assumed that you are putting this in your PluginBase::onEnable() function.

    You should end up with something that looks similar to this:
    PHP:
    namespace your\namespace;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\utils\Config;

    class 
    MyClass extends PluginBase {

        
    /** @var Config */
        
    public $myConfig;

        public function 
    onEnable() {
            @
    mkdir($this->getDataFolder());
            
    $this->saveResource("MyConfig.yml");
            
    $this->myConfig = new Config($this->getDataFolder() . "MyConfig.yml"Config::YAML);
        }

    }

    Configs are a useful way to handle data saving through plugins but they are not always necessary; if you just plan on calling Config::getAll() to return all the data, you're better off calling yaml_parse() or json_decode() directly on the data to cut all the useless function calls from a config object.
     
  2. SOFe

    SOFe A-Team Staff Member PMMP Team

    Messages:
    1,036
    GitHub:
    sof3
    I propose the addition of a global config API, or the invention of a global config library that helps plugins parse config contents. For example, a config entry 'Foo $player bar' should resolve to "Foo SOFe bar" given the context with a player object of "SOFe". This API can be extended with more plugins, e.g. $money for money in economy plugins.
     
  3. SOFe

    SOFe A-Team Staff Member PMMP Team

    Messages:
    1,036
    GitHub:
    sof3
    Actually, data folder is implicitly created before saveResource() call.

    Also, prefer using config.yml, $this->saveDefaultConfig() and $this->getConfig().
     
    HimbeersaftLP likes this.
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Verified

    Messages:
    556
    GitHub:
    JackNoordhuis
    Only if you're saving a resource from the /resources folder of a plugin. https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/plugin/PluginBase.php#L200L202

    These methods simplify the process but after all, there is only one default config. This thread allows users to see what goes on behind these methods and lets them replicate it without blindly calling the functions.
     
  5. SOFe

    SOFe A-Team Staff Member PMMP Team

    Messages:
    1,036
    GitHub:
    sof3
    What is the relationship between this and my post? It only checks if the resource is available from the phar, regardless it's created in data folder or not.
    Recommend adding them too :)
     
    MyNET, jasonwynn10 and HimbeersaftLP like this.
  6. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Verified

    Messages:
    556
    GitHub:
    JackNoordhuis
    Where did I mention a data folder? I am talking about the resources that are packaged with a phar (/resources directory). The link I provided clearly shows that if the resource you are saving is not present in the phar, the plugins data folder is not created; hence the need to create it yourself, if you're creating a config completely from code (not saving a copy of a predefined resource packaged within the archive).
     
    WreckagePE / ZAYD likes this.
  7. SOFe

    SOFe A-Team Staff Member PMMP Team

    Messages:
    1,036
    GitHub:
    sof3
    I would like to point out that this,
    , is not necessary before saveResource() call.
     
  8. xXiKhalediXx

    xXiKhalediXx Creeper

    Messages:
    1
    GitHub:
    Khaled098

Share This Page