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

Arrays in config - YAML

Discussion in 'Development' started by InspectorGadget, Mar 22, 2017.

  1. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Hi! How do i create an array into my config and how do i call it?

    Sorry, i don't have any code as i wan't to learn this.

    Example:
    Code:
    InspectorGadget:
        IP: 0.0.0.0
    RTG:
        IP: 0.0.0.1
    
    The list should not be overwritten and it needs to go continously...

    I've tried
    PHP:
    array_push()
    but yet to find a better solution..

    Thanks
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Use yaml_parse_file to convert the config to a PHP array, use yaml_emit_file to save an array into a file in YAML-recognizable format.
    PHP:
    $path $this->getDataFolder()."config.yml";

    $toArray yaml_parse_file($path);
    $toArray["Anonymous"] = ["IP" => "127.0.0.1"];
    yaml_emit_file($path$toArray);

     
    Sandertv likes this.
  3. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    Why don't just use set()?
     
    Sandertv likes this.
  4. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Thanks but your code works for individual YAML addition?

    What I mean is it has to be like a format for an example something like "Groups.php" on PurePerms.
    Whenever someone does /addgroup it automatically sets the correct format and it doesn't overwrite any...

    I tried looking into PurePerms's SRC but i couldn't understand.
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Because SOF3 says not to. And I am on his side. PocketMine's Config::set() and Config::get() are crap. Just use yaml_parse/yaml_emit, json_encode/json_decode or file_get_contents/file_put_contents.

    I know it's a pain to parse and emit it, but I don't think there's a direct one-step command way of doing it. So, in my example, I copy everything in the YAML file into an array $toArray. Then I add key "Anonymous" to the array, then save the modified array using yaml_emit
     
  6. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Oh ok. Thanks!
     
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I never said exactly so.
    Facts here again, try to induce this mess into your own knowledge:
    1. (new Config($path))->getAll() is pointless if you aren't initializing any data there. Use yaml_parse_file() instead of using the complex calls in it.
    2. Config class simply serves convenience methods to manipulate the array that backs the data of the config. Therefore all Config operations can be replaced by array manipulation. But this doesn't mean that the Config class is completely useless.
    3. It is not a criminal offence to use a Config class. But you will make a fool of yourself if you replace a simple call on yaml_parse_file() with the complexities of a Config class.
     
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    N
    Noted, will try this method
     
  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.