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

Solved Loading YAML configurations

Comments in 'Plugin Development' started by thebigsmileXD, Aug 14, 2015.

  1. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I have this:
    PHP:
    class Main extends PluginBase implements Listener {
        public function 
    onEnable(){
             
    $this->config = (new Config($this->getDataFolder()."config.yml"Config::YAML))->getAll(); // Load config in array
             
    $this->players = (new Config($this->getDataFolder()."players.yml"Config::YAML))->getAll(); // Load config in array
    }
    the files are in my source plugin, the structure of the plugin is:
    OITCPE
    .--resources
    . .|--config.yml
    . .|--players.yml
    .--src
    . .|--thebigsmileXD
    . . .|--OITCPE
    . . . .|--Main.php
    . . . .|--MakeSound.php
    .--plugin.yml

    But i get this error:
    and i still wonder why.
    Do i have to put them somewhere else or what?
    Last edited: Aug 14, 2015
  2. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    If you want to load as array, like this:
    PHP:
    public function onEnable(){
      
    $this->players = new Config($this->getDataFolder()."players.yml"Config::YAML, array());
    }
    To access it as array,
    PHP:
    public function blahblah(){
      
    $tmp $this->players->getAll();
    }
    thebigsmileXD likes this.
  3. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    btw, $this->config is a bad practice, use $this->saveDefaultConfig() instead and you can still access it as array
    thebigsmileXD likes this.
  4. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    btw, you should create this thread in 'Plugin Development'
  5. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Why don't you just edit your post? xD

    also.. doesn't saveDefaultConfig only save, and not get the data?
    i want to get the data
  6. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    after saveDefaultConfig, if you want to access the config with array use
    PHP:
    $this->getConfig()->getAll();
    thebigsmileXD likes this.
  7. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    getConfig() has build into PluginBase and you can always use it
  8. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Still, i get the same Error
    upload_2015-8-14_11-1-10.png

    My code:
    PHP:
    class Main extends PluginBase implements Listener {
        public function 
    onEnable(){
            
    $this->getPlayerConfig();
        }

        public function 
    onDisable(){
            
    //$this->updateConfig();
        
    }

        public function 
    getPlayerConfig(){
            
    //$this->players = new Config($this->getDataFolder()."players.yml", Config::YAML, array()); // Load config in array
            
    $this->saveDefaultConfig(); // Load config in array
            
    $this->players=$this->getConfig();
        }
    }
    upload_2015-8-14_11-4-24.png
  9. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Try putting
    PHP:
    @mkdir($this->getDataFolder());
    before saveDefaultConfig
    thebigsmileXD likes this.
  10. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Seems like me, as stupid as i am, forgot to write
    Into the configs.
    They get loaded now, and they are in the first OITCPE folder.
    Thanks for helping.
    jojoe77777 likes this.
  11. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    That error means it can't find the file or something in the file that you specified, in this case you haven't actually got it to save the config.
    Try this:
    PHP:
    class Main extends PluginBase implements Listener {

     public function 
    onEnable() {
        
    $this->saveResource($this->getDataFolder "config.yml"); //You need to save the file so it actually exists!!!
        
    $this->saveResource($this->getDataFolder "players.yml"); //You need to save the file so it actually exists!!!
        
    $this->config = (new Config($this->getDataFolder()."config.yml"Config::YAML))->getAll();
        
    $this->players = (new Config($this->getDataFolder()."players.yml"Config::YAML))->getAll();
    }
    iksaku and thebigsmileXD like this.
  12. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    I feel so stupid ;(

    i just thought i understand.. but no, i dont:
    PHP:
    public function getPlayerConfig(){
            
    //$this->players->getAll() = new Config($this->getDataFolder()."players.yml", Config::YAML, array()); // Load config in array
            
    @mkdir($this->getDataFolder());
            
    //$this->saveDefaultConfig(); // Load config in array
            
    $this->players = new Config($this->getDataFolder()."players.yml"Config::YAML, array());;
        }
    The array seems to get filled.
    But i cant write it to the config..
    i thought about
    PHP:
    $this->players->save();
    but it doesnt seem to save the players set in the array to the file..
    This seems to work fine.
    How do i save the new data to the yml?
    CrazedMiner likes this.
  13. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    If you just want a variable to access the config try this:
    PHP:
    $this->config = (new Config($this->plugin->getDataFolder() . "config.yml"Config::YAML))->getAll();
    Putting the array() into your variable sets the config to an empty array (to my understanding).
    thebigsmileXD likes this.
  14. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Well, maybe i should explain more in deep what i want to do.

    I make a plugin, and when you do
    , i want to check if the player already exists in the config, if not, then set deaths=0, kills=0 and playing to true.

    after a round playing i want to write the kills and deaths into the config. (or on
    )
  15. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    BAD PRACTICE
    jojoe77777 and PEMapModder like this.
  16. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Dont shout! My poor ears!
    And why bad practice, instead of your code it works
  17. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    No @PEMapModder said that..
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    getDataFolder is not required. You have to provide the path relative to your resources folder.
    iksaku and CrazedMiner like this.
  19. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    I was reminded of that about five minutes after I posted in this thread :p
  20. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    This still doesn't help me saving data in a .yml

Share This Page

Advertisement