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

How to use config files

Comments in 'Plugin Development' started by Hittmana, Jan 26, 2016.

  1. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Hello thanks to @PocketKiller i was able to make my first working plugin. Now i would like to add config file usage: I need it to generate, and read a config file. Here is how I want the config to look.
    Code:
    join-message:
      -Ethan: The KeksMaster has joined
      -Hittmana: The owner has just joined
      etc..
    
    This is how I want the config to look so I need to know how to read this type of plugin. Thanks in advance!
  2. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    PHP:
    $cfg $this->getConfig()->getAll();
    $msg $cfg["join-message"];
    $player $msg["Hittmana"];
    This is how to get values on config
    Btw, I don't know what exactly you want :p
    applqpak likes this.
  3. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    I mean i need to know how to create a config and then read it. It needs to read -Ethan: and then set $message to the following text, in this case: The KeksMaster has joined
  4. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Just create resources folder, make config.yml, write the content like above, and if player joins, broadcast a message
    Wolfo likes this.
  5. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Oh ok where do I put the resource folder under source files or src?
  6. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Also what is the current PocketMine API version?
  7. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    Code:
    YourPlugin:
        - resources:
             - config.yml
        - src:
             - .....
        - plugin.yml
    
  8. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Ok thanks so much!
  9. Vaivez66
    Offline

    Vaivez66 Notable Member Plugin Developer

    Joined:
    Jun 10, 2015
    Posts:
    402
    Plugins:
    2
    You mean in plugin.yml? I don't know exactly, but I use 1.12.0 and it's still working
  10. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    PHP:
    <?php

    namespace Test;

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

    class 
    Main extends PluginBase Implements Listener {
        public 
    $cfg;

        public function 
    onEnable() {
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
             @
    mkdir($this->getDataFolder());
            
    $this->cfg $this->getConfig();
            
    $this->saveDefaultConfig();
            
    $this->getLogger()->info("[MessageTest] Enabled");

        }
        public function 
    onjoin(PlayerJoinEvent $event) {
            
    $player $event->getPlayer();
            
    $message $this->cfg->get("message");
            
    $name $event->getPlayer()->getName();
            if(
    $name === "Hittmana") {
                
    $this->getServer()->broadcastMessage("[Server] The Owner ".$name." Join the Server !");
            }else {
                
    $this->getServer()->broadcastMessage("[Server] ".$message."");
            }
        }
    }
  11. Ritch
    Offline

    Ritch Active Member

    Joined:
    Oct 4, 2015
    Posts:
    184
    Minecraft User:
    anonymous
    For the Config.yml look
    Code:
    ###############
    #                                 #
    # Config Message.  #
    ###############
    
    message: Player JOIN in server !
  12. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    Thank you Ritch Is there a way to make it where you can have a different messages for different people?
  13. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    The best working way to do a config file right now is:

    PHP:
    $config = new Config($this->getDataFolder() . "config.yml"Config::YAML);
    I usually use this method for get configurations:

    PHP:
    /**
    * @param string $file
    * @param string $object
    */
    public function getConfiguration($file$object)
    {
    $config = new Config($this->getDataFolder() . $file ".yml"Config::YAML);
    $config->get($object);
    }

    // If you want get better performance, you should define a real object ($this->config) with the configuration.
    Last edited: Jan 27, 2016
    Hittmana, TheDeibo and Vaivez66 like this.
  14. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    ok how do i get the data from the config. so say the config is:
    Code:
    Test: Test value
    
    how do i get the value of Test:
  15. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    PHP:
    $this->config = new Config($this->getDataFolder."config.yml"Config::YAML);

    use:
    $this->config->get("Test");

    or use:

    $this->configOtherMethod $this->config->getAll();

    $this->configOtherMethod["Test"];

    Both will return "Test value" ;)

    Skullex likes this.
  16. Hittmana
    Offline

    Hittmana Active Member

    Joined:
    Jul 26, 2015
    Posts:
    224
    Minecraft User:
    move_it
    ok thanks!
  17. jasonwynn10
    Offline

    jasonwynn10 Active Member

    Joined:
    Dec 20, 2015
    Posts:
    87
    Minecraft User:
    jasonwynn10
    I wish there were a youtube tutorial for this...
  18. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Avoid using $this->config. It is an already-declared private property of the PluginBase class.

    The proper method to use configs is to create resources/config.yml, run $this->saveDefaultConfig() on start, and use $this->getConfig() to get the config object.
    Or if you really want to create a config instance yourself, you should call it $this->cfg or something else that is not $this->config, to avoid overriding a class property that has different purpose.
    Vaivez66 likes this.

Share This Page

Advertisement