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

Making config.yml

Discussion in 'Development' started by kazuya, Jun 11, 2017.

  1. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    I am trying to make a messages.yml where people can edit the message

    PHP:
    <?php

    namespace Kaz;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat as O;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements Listener{

      public function 
    onEnable(){

        @
    mkdir($this->getDataFolder());
             
    $this->saveResource("config.yml");
             
    $this->config= new Config($this->getDataFolder() . "config.yml",
    Config::YAML);
     
    $this->getServer()->getPluginManager()->registerEvents($this$this);
    }
      public function 
    onJoin(PlayerJoinEvent $event){

        
    $player $event->getPlayer();
        
    $name $event->getPlayer()->getName();

        
    $player->sendMessage($this->getConfig()->get("join-message"));
      }
    }
    I made a resources folder with config.yml which looks like this

    PHP:
    join-messagetest
    they didn't work.. sorry I'm new to making configs..
     
    Last edited: Jun 11, 2017
  2. Teamblocket

    Teamblocket Zombie

    Messages:
    301
    GitHub:
    teamblocket
    try this
    PHP:
    <?php

    namespace Kaz;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat as O;
    use 
    pocketmine\utils\Config;

    class 
    Main extends PluginBase implements Listener{

      public function 
    onEnable(){
          
          
    $this->saveDefaultConfig();
          
    $this->getServer()->getPluginManager()->registerEvents($this$this);
      }
     
      public function 
    onJoin(PlayerJoinEvent $event){
          
          
    $player $event->getPlayer();
          
    $name $event->getPlayer()->getName();
          
    $player->sendMessage($this->getConfig()->get("join-message"));
      }
    }
    and for your config
    PHP:
    join-message"message"
     
    SOFe likes this.
  3. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    You can load a config in 2 ways. One by calling it under a Config class onEnable() and another one is $this->saveDefaultConfig() ; which you pre-make a config and then you call it.

    After having a look at your config which you made earlier.
    Code:
     
    join-message: "" 
    
    That's "" means it is a string, if it's not placed nothing will work and you'll have errors. It's a good practice to add a "" on strings.
     
    kazuya likes this.
  4. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    error
    Code:
    [01:25:08] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'Kaz v1.0.0': yaml_parse(): end of stream reached without finding document 0 on Kaz\Main
    [01:25:08] [Server thread/CRITICAL]: ErrorException: "yaml_parse(): end of stream reached without finding document 0" (EXCEPTION) in "/src/pocketmine/utils/Config" at line 145
    
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Bro, make sure your config.yml is like this:

    Code:
     
    ---
    join-message: "" 
    ... 
    
     
    kazuya likes this.
  6. kazuya

    kazuya Slime

    Messages:
    79
    GitHub:
    xdqrknez
    it worked ;O!!

    what if you want to name config.yml to a different one like message.yml?
     
  7. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    The saveDefaultConfig() is simply an alias of saveResource("config.yml").

    The getConfig() method will do multiple things:
    1. If the config is already loaded, return the config
    2. If the config is not loaded yet, try to create a config file at config.yml using new Config()
    3. Parse config.yml from resources folder of plugin. If there is a property that exists in the resources one but not in the user-editable one, fill it with the default value from the resources one.

    On the other hand, "" is not only a good practice but also a should. If you put nothing there, it would become a null rather than an empty string, which might lead to strict type errors. The plugin should be responsible for casting it to string.
     
    jasonwynn10 likes this.
  9. Bruhuey

    Bruhuey Creeper

    Messages:
    4
    use:
    @mkdir($this->getDataFolder());
    $this->saveDefaultConfig();
    $this->getResource("config.yml");

    and when uses the config yml message:

    $player->sendMessage($this->getConfig()->get("join-message"));
     
  10. Axon

    Axon Zombie

    Messages:
    276
    Please don't bump threads that are older than a year.
     
    Agent and minijaham like this.
  11. Bruhuey

    Bruhuey Creeper

    Messages:
    4
    ikr
     
  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.