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
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.
I understand your point However, I can't understand why Config->get() doesn't reutrn the correct value from the config.yml
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.
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!");}
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), 0755, true); }
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