1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Tutorial Using Configs

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

  1. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    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.
     
    mmm545, ethaniccc, Doxestic and 8 others like this.
  2. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    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 Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    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 Poggit Reviewer

    Messages:
    618
    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 Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    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 Poggit Reviewer

    Messages:
    618
    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 Administrator Staff Member PMMP Team Poggit Admin

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

    xXiKhalediXx Spider

    Messages:
    8
    GitHub:
    Khaled098
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Then what's the whole OP about?
     
  10. GamingFR91

    GamingFR91 Spider Jockey

    Messages:
    29
    GitHub:
    kanekilechomeur
    how to set a config like that

    GetMe:
    - "one"
    - "two"
    - "three"
    - "four"
     
  11. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Use an array:
    PHP:
    $arr = ["one""two""three"]; // create array
    $arr[] = "four"// append to array
    $config->set("GetMe"$arr);
     
  12. GamingFR91

    GamingFR91 Spider Jockey

    Messages:
    29
    GitHub:
    kanekilechomeur
    Thanks
     
    HimbeersaftLP likes this.
  13. GamingFR91

    GamingFR91 Spider Jockey

    Messages:
    29
    GitHub:
    kanekilechomeur
    and to delete one of them from the config?
    Like maybe i want to delet "two" from it
     
  14. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
  15. TestDevelopment

    TestDevelopment Spider Jockey

    Messages:
    41
    GitHub:
    non
    PHP:
    $this->myConfig = new Config($this->getDataFolder() . "MyConfig.yml"Config::YAML);
    Its easyer to say 2 instead of Config::YAML

    PHP:
    $this->myConfig = new Config($this->getDataFolder() . "MyConfig.yml"2);
     
  16. Primus

    Primus Zombie Pigman

    Messages:
    749
    What does that say for readability? What does the 2 stands for? And that alone is big issue, but not the main one.

    What if the constant is changed in source code? What if number 2 no longer represents the YAML encoding?
     
  17. PiloudeDakar

    PiloudeDakar Witch

    Messages:
    62
    GitHub:
    piloudedakar
    Hey, how the function exists() work, I don't understand this code.
     
  18. mmm545

    mmm545 Baby Zombie

    Messages:
    152
    GitHub:
    mmm545
    the name is pretty self-explanatory, it checks if a specific key exists in the config and returns true if it does, false otherwise
    example:
    PHP:
    if($config->exists("some_key"false)){
        
    //do something here
    }
    the first parameter is the key you want to check if it exists, the second parameter is whether to make the search case insensitive
    example
    PHP:
    // let's imagine there's a key in the config called join_message
    // and we search for JOIN_MESSAGE
    // if we set the second parameter to false, the exists function would return false thus the code inside the if statement won't run
    if($config->exists("JOIN_MESSAGE"false)){
        
    $this->getLogger()->info("key exists"); // it won't print "key exists" on the console
    }

    // let's try setting the second parameter to true
    if($config->exists("JOIN_MESSAGE"true)){
        
    $this->getLogger()->info("key exists"); // it will print "key exists" on the console
    }
     
    PiloudeDakar likes this.
  19. PiloudeDakar

    PiloudeDakar Witch

    Messages:
    62
    GitHub:
    piloudedakar
    Thanks a lot
     
  20. PiloudeDakar

    PiloudeDakar Witch

    Messages:
    62
    GitHub:
    piloudedakar
    Hey, sorry, second question : in a yaml config, how to use under cotegories :
    Code:
    category1:
      category2: value
    In this case, how to take category2's value ?
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.