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

Config vaule yaml_parse_file vs get()

Discussion in 'Development' started by XdmingXD, Jul 22, 2017.

  1. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    I am literally confussed with yaml_parse_file() and get()
    PHP:
    public function onEnable() {
    $this->getServer()->getPluginManager()->registerEvents($this$this);@mkdir($this->getDataFolder());
    $this->saveDefaultConfig();
    $this->config = new Config($this->getDataFolder() . "config.yml"Config::YAML);
    $this->config->set("test" true);
    $this->saveDefaultConfig();      //$this->config>get() is still "true" even I take out this line
    $data yaml_parse_file($this->getDataFolder() . "config.yml");
    foreach(
    $data as $a) {
    $this->data $a;
    }
    var_dump(yaml_parse_file($this->getDataFolder() . "config.yml"));
    var_dump($this->config->get("test"));
    $this->getLogger()->info("Loaded!");
    }
    Config :
    Code:
    ---
    test: false
    # true or false
    ...
    
    Console :
    Code:
    array(1) {
      ["test"]=>
      bool(false)
    }
    bool(true)
    
    Anyone can tell me why? Thanks
     
  2. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    yaml_parse_file parses all contents of a yaml file into an array, Config->get() directly returns the value of the key you're searching for.
     
  3. XdmingXD

    XdmingXD Baby Zombie

    Messages:
    158
    GitHub:
    xdmingxd
    I understand your point
    However, I can't understand why Config->get() doesn't reutrn the correct value from the config.yml
     
  4. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Maybe because you're doing REALLY weird things? You know, that code you posted up there, you can narrow it down to this:

    PHP:
    public function onEnable() {
      
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      
    $this->saveDefaultConfig();
      
    var_dump($this->getConfig()->get("test"));
      
    $this->getLogger()->info("Loaded!");
    }
    Set the value to false in the config.yml and it will return the right things.
     
    Last edited: Jul 22, 2017
    Muqsit likes this.
  5. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    I'll explain to you what exactly happened.
    PHP:
    public function onEnable() {
      
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      @
    mkdir($this->getDataFolder());
      
    $this->saveDefaultConfig(); // Saves the default Config. I assume "test" is by default false.
      
    $this->config = new Config($this->getDataFolder() . "config.yml"Config::YAML); // For some reason stores a new Config object, while you can already access one with $this->getConfig() after doing $this->saveDefaultConfig();
      
    $this->config->set("test" true); // Sets "test" in the Config object you just made to true, not sure why you do that. Note that you forgot to save, so it doesn't show up in the file.
      
    $this->saveDefaultConfig(); // For some reason you save the default config AGAIN. It will still be the same as earlier, trust me.
      
    $data yaml_parse_file($this->getDataFolder() . "config.yml"); // Parses the actual file, which has "test" as false because that's the way the default config was saved.
      
    foreach($data as $a) {
        
    $this->data $a;
      }
      
    var_dump(yaml_parse_file($this->getDataFolder() . "config.yml")); // Returns an array containing the correct information.
      
    var_dump($this->config->get("test")); // You modified the Config file but didn't save it, it will therefore return true, but show up as false in the file.
      
    $this->getLogger()->info("Loaded!");
    }
     
  6. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    This line is unneeded, see these lines from the PluginBase::saveResource() which gets called by PluginBase::saveDefaultConfig(-:
    PHP:
    if(!file_exists(dirname($out))){
       
    mkdir(dirname($out), 0755true);
      }
     
    Sandertv likes this.
  7. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    You used $this->saveDefaultConfig(); instead of $this->config->save();
    Config::saveDefaultConfig() doesn't save the current config, only the default if the file doesn't exist.
    Config::save() saves the current config
     
  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.