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

Creating And Reading Config.yml File Help

Comments in 'Plugin Development' started by Comedyman937, Mar 1, 2014.

Thread Status:
Not open for further replies.
  1. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Hello,

    I'm working on a new version of my plugin and in order for it to work the way I want it to I will need to set up a config.yml file.

    I have the config generating but i cant seem to find out how to make the plugin read the file.

    The config code i have right now:
    PHP:
    $this->config = new Config($this->api->plugin->configPath($this)."config.yml"CONFIG_YAML, array(
              
    'enableConf' => false,
              
    'spawnWorld' => 'world',
              
    'X' => 0,
              
    'Y' => 0,
              
    'Z' => 0,
     
    ));
    Im trying to read the config with this:
    PHP:
    $this->config $this->api->plugin->readYAML($this->path."config.yml");
    Lastly this is how im trying to select the different parts of the config:
    PHP:
    if($this->config['enableConf']=="true"){
     
    //Code for if enableConf is set to true in config.yml
     
    }elseif($this->config['enableConf']=="false"){
     
    //Code for if enableConf is set to false in config.yml
     
    }else{
     
    console("[WARNING] There is nothing set for enableConf in the config.yml file!");
     
    }
    What did I do wrong or forget to add?

    Thanks in advance!
    Last edited: Mar 2, 2014
  2. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Don't put quotes around true or false when testing for it. It should be
    PHP:
    if($this->config['enableConf']=true){
    //Code for if enableConf is set to true in config.yml
    }elseif($this->config['enableConf']=false){
    //Code for if enableConf is set to false in config.yml
    }else{
    console("[WARNING] There is nothing set for enableConf in the config.yml file!");
    }
    And for the readYaml, because you wrote the config with $this->config, the readYaml should be
    PHP:
    $this->path $this->api->plugin->readYAML($this->config);
    Or, if that doesn't work, then doing
    PHP:
    $this->path $this->api->plugin->readyYAML($this->api->plugin->configPath($this)."config.yml");
    should work. I am pretty tired though, so if none of those work, let me know and I'll probably be able to think better after some sleep :)
  3. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Nope. Its still not showing any errors nor is it doing what it should be...
    Last edited: Mar 2, 2014
  4. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Ok I forgot to mention that after you make those changes, you have to do a few more things like this:
    PHP:
    $enableConf=$this->config->get('enableConf');
    if(
    $this->config['enableConf']=="true"){
     
    //Code for if enableConf is set to true in config.yml
     
    }elseif($this->config['enableConf']=="false"){
     
    //Code for if enableConf is set to false in config.yml
     
    }else{
     
    console("[WARNING] There is nothing set for enableConf in the config.yml file!");
     }
    And you may or may not even need to have the readYAML section of code. I don't think you'll need it. So, in final, it should look like this:
    PHP:
    $this->config = new Config($this->api->plugin->configPath($this)."config.yml"CONFIG_YAML, array('enableConf' => false,'spawnWorld' => 'world','X' => 0,'Y' => 0,'Z' => 0,
    ));
    PHP:
    $enableConf=$this->config->get('enableConf');
    if(
    $this->config['enableConf']=="true"){
     
    //Code for if enableConf is set to true in config.yml
     
    }elseif($this->config['enableConf']=="false"){
     
    //Code for if enableConf is set to false in config.yml
     
    }else{
     
    console("[WARNING] There is nothing set for enableConf in the config.yml file!");
     }
    This should work now as long as $this->config is set in the init section.
  5. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    It still doesnt seem to work... :/

    Just in case theres something that I did wrong with the rest of the code, heres my entire script:
    PHP:
    <?php
     
    /*
    __PocketMine Plugin__
    name=AlwaysSpawn
    description=Force your users to automaticaly spawn every time they login!
    version=2.0.1dev
    author=Comedyman937
    class=AlwaysSpawn
    apiversion=6,7,8,9,10,11,12
    */
     
     
         
    class AlwaysSpawn implements plugin{
              private 
    $api$path$config;
     
         public function 
    __construct(ServerAPI $api$server false)
         {
              
    $this->api $api;
         }
     
         public function 
    init()
         {
              
    $this->config = new Config($this->api->plugin->configPath($this)."config.yml"CONFIG_YAML, array(
                   
    'enableConf' => false,
                   
    'spawnWorld' => 'world',
                   
    'X' => 0,
                   
    'Y' => 0,
                   
    'Z' => 0,
    ));
     
              
    $this->api->addHandler("player.spawn", array($this"eventHandler"), 100);
               
    console("[INFO] AlwaysSpawn Loaded!");
         }
     
         public 
    fulction eventHandler($data$event)
         {
              
    $enableConf=$this->config->get('enableConf');
     
                   if(
    $this->config['enableConf']=="true"){
                        
    console("[AlwaysSpawn] The Config.yml file is enabled!");
                   switch(
    $event){
                       case 
    'player.spawn':
     
                            
    $XPos=$this->config->get('X');
                            
    $YPos=$this->config->get('Y');
                            
    $ZPos=$this->config->get('Z');
                            
    $Level=$this->config->get('spawnWorld');
     
                            
    $spawn=new Position($XPos$YPos$ZPos$Level);
                            
    $player->teleport($spawn);
     
                             break;
     
                   }
         }elseif{
    $this->config['enableConf']=="false"){
     
              
    console("[AwlaysSpawn] The Config.yml File is Disabled!");
              switch(
    $event){
                   case 
    'player.spawn':
     
                        
    $this->api->shedule(31, array($this"teleport"), $data);
     
                   break;
              }
     
         }else{
              
    console("[ERROR] [AlwaysSpawn] There is nothing set for enableConf in the config.yml file!");
         }
    }
     
         public function 
    teleport($player)
         {
              
    $player->teleport($this->api->level->getSpawn());
         }
     
         public function 
    __destruct(){
              
    console("[INFO] AlwaysSpawn Unloaded!");
         }
     
    }
    There may be a few typos in there since I had to retype the entire script by hand (Copy/Paste wasnt working) but the basics are right there and i didnt miss parts that were needed.

    Did i do something wrong with the script as a whole? Because (test it if you would like) it doesnt run any of the code inside the if, elseif, or else statements...
  6. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    With the if/else statement, it should be if($enableConf=true){} and if($enableConf=false){}. Sorry I forgot to mention that :p
  7. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    *sigh* Still nothing XD
  8. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Ok. Let me test this a bit and I'll get back to you as soon as possible.
    Comedyman937 likes this.
  9. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Ok thank you.
  10. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Ok this is what I got so far:
    PHP:
    <?php
    /*
    __PocketMine Plugin__
    name=AlwaysSpawn
    description=Force your users to automaticaly spawn every time they login!
    version=2.0.1dev
    author=Comedyman937
    class=AlwaysSpawn
    apiversion=6,7,8,9,10,11,12
    */
    class AlwaysSpawn implements Plugin{
        private 
    $api$path$config;

        public function 
    __construct(ServerAPI $api$server false)
        {
            
    $this->api $api;
        }

        public function 
    init()
        {
            
    $this->config = new Config($this->api->plugin->configPath($this)."config.yml"CONFIG_YAML, array(
                
    'enableConf' => false,
                
    'spawnWorld' => 'world',
                
    'X' => 0,
                
    'Y' => 0,
                
    'Z' => 0,
            ));
            
    $this->api->addHandler("player.spawn", array($this"eventHandler"), 100);
            
    console("[INFO] AlwaysSpawn Loaded!");
        }

        public function 
    eventHandler($data$event)
        {
            
    $enableConf=$this->config->get('enableConf');
            
    $username=$data["player"]->username;
            if(
    $enableConf=true){
                
    console("[AlwaysSpawn] The Config.yml file is enabled!");
                switch(
    $event){
                    case 
    'player.spawn':
                        
    $XPos=$this->config->get('X');
                        
    $YPos=$this->config->get('Y');
                        
    $ZPos=$this->config->get('Z');
                        
    $Level=$this->config->get('spawnWorld');
                        
    $spawn=new Position($XPos$YPos$ZPos$Level);
                        
    $this->api->player->teleport($username$spawn);
                    break;
                }
            }elseif(
    $enableConf=false){
                
    console("[AwlaysSpawn] The Config.yml File is Disabled!");
                switch(
    $event){
                    case 
    'player.spawn':
                        
    $this->api->shedule(31, array($this"teleport"), $data);
                    break;
                }
            }else{
                
    console("[ERROR] [AlwaysSpawn] There is nothing set for enableConf in the config.yml file!");
            }
        }
       
        public function 
    teleport($player)
        {
            
    $player->teleport($this->api->level->getSpawn());
        }
       
        public function 
    __destruct(){
            
    console("[INFO] AlwaysSpawn Unloaded!");
        }
    }
    ?>
    But there are 2 issues I know of so far. The first is that it won't let me use $data["player"] because it says it's an array, which it shouldn't be. The second problem is that it says that $Level shouldn't be a string, which it is and I'd think it has to be, unless PocketMine gives numerical outputs for levels and doesn't use the level's name. In that case, I have no clue how you could set that up in a config file :/
  11. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    well dor the first issue couldnt it be just $data and the have that inside the first if statement and then use a second one in another if statement if needed?
  12. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    It doesn't matter where you put that line of code, it always gives an error saying it is an array. And I don't think it can just be $data, but you can try.
  13. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Well i said that because in my first version of AlwaysSpawn (and when enableConf is set to false) its set to $this->api->shedule(31, array($this, "teleport"), $data); which worked fine. Ill try it and do what i can to figure it out. Thanks for the help! Ill give you credit once i publish this update ;)
  14. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Ok. Sorry I wasn't able to help too much. Lately, I haven't been able to connect to my WiFi, so I've had a hard time testing plugins. Good luck with the rest of your plugin and I can't wait to see the update when it's done! :D
    Comedyman937 likes this.
  15. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    Quick question: Im guesing that you tested the code you posted with a console at the lease? If so, was it posting console medssages as if it were reading the config.yml file? (I havent tested it yet ill have to wait until tomorrow or tuesday)
  16. Darunia18
    Offline

    Darunia18 Staff Member Sectional Moderator

    Joined:
    Aug 23, 2013
    Posts:
    755
    Plugins:
    2
    Minecraft User:
    Darunia18
    Yeah those work! :D I didn't know that was a thing, so I'm excited to start using that in future projects just for overall aesthetics :)
    Comedyman937 likes this.
  17. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    If I wanted to save certain information to parts of the config.yml file (like if I wanted to post the players current x position to the x value in the config) how would I do that?

    Thanks
  18. PMPCT
    Offline

    PMPCT New Member Plugin Developer

    Joined:
    Mar 4, 2014
    Posts:
    3
    Plugins:
    1
    Minecraft User:
    pmpct
    You can create a variable that will read out the contents assigned to this variable, come again to judge his ture or false
  19. DA_Swagnemite
    Offline

    DA_Swagnemite Notable Member Plugin Developer

    Joined:
    Nov 17, 2013
    Posts:
    388
    Plugins:
    7
    Minecraft User:
    DA_SWAGNEMITE
    I mean straight from in-game so a player could type something like /asset and it would save the location.
  20. zgeorge2222
    Offline

    zgeorge2222 New Member

    Joined:
    Mar 3, 2014
    Posts:
    9
    Minecraft User:
    zgeorge22
    I have a similar issue. My plugin will be swapping 2 players positions. How would I save the positions of each player to a config file and then "immediately" (so they actually swap exact positions), get player1's positions and make it player2's position and vice versa. Thanks for any help.
Thread Status:
Not open for further replies.

Share This Page

Advertisement