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

Confused with arrays on config files.

Comments in 'Plugin Development' started by Dinokiller, Oct 22, 2013.

  1. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Hi guys, as a few of you may know I'm making a faction plugin however I've ran into a problem.

    This code is going to save the data of a faction to the config file.

    PHP:
    $this->config->set("name",$faction["name"]);
    $this->config->save();
    $this->config->reload();
    At the moment it's only saving the name which is going to be changed of course, no problem.
    But here's my config layout for storing save data for factions.

    PHP:
        $this->config = new Config($this->api->plugin->configPath($this) . "config.yml"CONFIG_YAML, array(
        
    "factions" => array(
          
    "example_faction" => array(,
            
    "name" => "Example Faction"
          
    )
          )
        ));
    Now as you can see there is an array called factions which of course holds all the factions and each faction in the faction array is also an array containing it's data (at the moment their is only data for the name just for this example).

    My question is how would I set a key that is in a array.
    Right now on the save code I'm making it set a value called name. This value only exists inside the example_faction so how would I set it. My question is slightly hard to explain so I hope you understand what I mean.

    Thanks.
  2. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    392
    Add this:
    PHP:
    $this->config $this->api->plugin->readYAML($this->api->plugin->configPath($this) . "config.yml");
    It will read the config file to an array so you can use $this->config['factions']['example_faction']['name'] = 'The best faction ever!'
  3. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    OK I'll try this, thanks for the help!

    EDIT: But how do I save it?
    Last edited: Oct 23, 2013
  4. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    To save the config use
    Code:
    $this->config->save();
    I believe.
  5. wies
    Offline

    wies Notable Member

    Joined:
    Aug 23, 2013
    Posts:
    392
    You can use that if the variable is a config object. But the readYAML function will return an array.
    So you can use this to save it:
    PHP:
    $this->api->plugin->writeYAML($this->api->plugin->configPath($this) . "config.yml"$this->config);
  6. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    Thank you.
  7. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    Oh, cool! I will use this in the future ;)

Share This Page

Advertisement