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

[New API] Saving/Loading Config

Comments in 'Plugin Development' started by Adaasi, Aug 29, 2014.

  1. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    I'm trying to develop my first simple plugin. I've a big problem with reading configuration.
    This is my code:
    PHP:
    namespace ChatTest;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\IPlayer;
    use 
    pocketmine\utils\Config;

    use 
    pocketmine\utils\TextFormat;

    class 
    Test extends PluginBase implements Listener{
        public 
    $config;
        public function 
    onEnable(){
            @
    mkdir($this->getDataFolder());
            
    $this->getServer()->getPluginManager()->registerEvents(new Main($this), $this);
            
    $this->config = (new Config($this->getDataFolder()."config.yml"Config::YAML, array(
            
    "info" => "Join Player Test")))->getAll();
        }
      
        public function 
    onPlayerJoin(PlayerJoinEvent $event){
            
    $this->config $this->getConfig()->getAll();
            
    $event->setJoinMessage($this->TestInfo(). "test");
        }
        
    //Custom Functions
        
    public function TestInfo(){
      
            return 
    $this->config["info"];
          
        }

      
    }
    When a player joins I get this error:
    [NOTICE] An E_NOTICE error happened: "Undefined index: info-message" in "/ChatTest/src/ChatTest/Test"...
  2. Kagehis4
    Offline

    Kagehis4 New Member

    Joined:
    Aug 20, 2014
    Posts:
    44
    Minecraft User:
    Kagehis4
    just use $this->getConfig() no need thefilename
  3. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    The problem is "->getAll()"
    You should use this:
    Code:
    $this->config = new Config($this->getDataFolder()."config.yml", Config::YAML, array("info" => "Join Player Test"));
    $this->config = $this->config->getAll();
  4. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    And remove this line:
    "$this->config = $this->getConfig()->getAll();"
  5. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    372
    Plugins:
    3
    Minecraft User:
    Dutok
    You don't need to get the config file every time a player joins. I'll fix it for when I get back to my computer.

    Edit: Opps, forgot.
    Last edited: Aug 30, 2014
    Adaasi and iJoshuaHD like this.
  6. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    What about using the default config.yml resource?
    PHP:
    $this->saveDefaultConfig();
          
    $this->config $this->getConfig()->getAll();
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't use $this->getConfig() if you decide to set $this->config yourself.
    64FF00 likes this.
  8. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    I think you have written some different code here because this should work, but the error says
    "Undefined index: info-message" so you probably have to change that into"info"
  9. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    Doesn't work. Can someone write the correct code?
  10. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    PHP:
        public function onEnable(){
            @
    mkdir($this->getDataFolder());
            
    $this->saveDefaultConfig();
            
    $this->getServer()->getPluginManager()->registerEvents(new Main($this), $this);
            
    $this->config $this->getConfig()->getAll();
            
    $this->getLogger()->info($this->TestInfo()); //<-Get Config "info" message
        
    }
    In OnEnable() Event the logger gets the config message.
    The problem is in OnPlayerJoin Event.
  11. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    Please Help Me!
  12. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    You need to make /resources folder in your plugin folder with /src and place the default config.yml file into there.
    Praxthisnovcht likes this.
  13. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    I did it
  14. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    Can I load variables in OnEnable and share the value between functions?
    64FF00 likes this.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes. Instead of storing as $varName, store as $this->varName (fields instead of variables). Look at https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/plugin/PluginBase.php and look for private $...; lines. Do not use those names. (Therefore, do not use $this->config because it will break the PocketMine code that uses $this->config (as in $this->getConfig(), $this->reloadConfig() functions). Use another name, like $this->cfg)
    64FF00 likes this.
  16. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    PHP:
    <?php

    namespace _64FF00\ExamplePlugin;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;

    use 
    pocketmine\plugin\PluginBase;

    class 
    ExamplePlugin extends PluginBase implements Listener
    {   
        public function 
    onEnable()
        {
            
    $this->saveDefaultConfig();
           
            
    $this->cfg $this->getConfig()->getAll();
           
            
    $this->getLogger()->info('$this->cfg["TEST"] -> ' $this->cfg["TEST"]);
        }
       
        public function 
    onPlayerJoin(PlayerJoinEvent $event)
        {
            
    $event->setJoinMessage($this->cfg["TEST"]);
        }
    }
    Adaasi and Praxthisnovcht like this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Clever method, the underscore, to make 64FF00 a valid namespace :) :+1:
    iJoshuaHD and 64FF00 like this.
  18. 64FF00
    Offline

    64FF00 Active Member Plugin Developer

    Joined:
    May 11, 2014
    Posts:
    298
    Plugins:
    2
    Minecraft User:
    64FF00
    :) x999
    Adaasi, Praxthisnovcht and iJoshuaHD like this.
  19. Adaasi
    Offline

    Adaasi Active Member

    Joined:
    Aug 14, 2014
    Posts:
    80
    Thanks 64FF00 :):).
    I solved the problem using __construct.
    Last edited: Sep 3, 2014
    64FF00 likes this.

Share This Page

Advertisement