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

Config checking correct?

Comments in 'Plugin Development' started by xFlare, Dec 2, 2014.

  1. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    Im learning about config files now, So I decided to write up a plugin.

    Would this be correct for generating, And checking is something is true/false

    PHP:
    <?php

    namespace MaxConfig;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerDropItemEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{
        public function 
    onEnable(){
            @
    mkdir($this->getDataFolder());
            
    $this->worlds = (new Config($this->getDataFolder()."config.yml"Config::YAML, array(
                
    "allow-drop" => true,
                ))))->
    getAll();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("MaxConfig Loaded!");
        }
       
        
    /**
         * @param PlayerDropItemEvent $event
         *
         * @priority       NORMAL
         * @ignoreCanceled false
         */
        
    public function onDrop(PlayerDropItemEvent $event){
            
    $player $event->getPlayer();
            if(!
    $this->config->get("allow-drop") == false) {
                
    $player->sendMessage("[MaxConfig] A magical force is stoping you from doing this!");
                
    $event->setCancelled();
            }elseif(!
    $this->config->get("allow-drop") == true) {
                return 
    true;
            }
        }
    }
    ?>
    hoyinm14mc, Devin5411 and Adaasi like this.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    There is once extra close parentheses in the new Config line.
    And no need ?>
    And to check a value if it is false, just do if($expression !== false). You shouldn't use ! with === or ==. The ! means change true to false or false to true.
    Adaasi and xFlare like this.
  3. Sergey_D
    Offline

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    PHP:
    $this->config = new Config($this->getDataFolder() . "Config.yml"Config::YAML, array());
    LDX likes this.
  4. PixelGuy75
    Offline

    PixelGuy75 Notable Member Plugin Developer

    Joined:
    Feb 9, 2014
    Posts:
    316
    Plugins:
    2
    Minecraft User:
    PixelGuy75
    "A magical force is stoping you from doing this" It should be *stopping not stoping.
  5. Sergey_D
    Offline

    Sergey_D Active Member

    Joined:
    Jan 19, 2014
    Posts:
    75
    Minecraft User:
    CLILLZ
    He used getAll(), and as I understand it, the variable world will be an array.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I am seriously telling you, @shoghicp has confirmed that setting $this->config directly (by redeclaraing it) is discouraged and should better not be approved in the plugin repository to avoid setting bad examples in the future (I added the reasons). Who started this nasty trend?
    PixelGuy75 and xFlare like this.
  7. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    PHP:
    <?php

    namespace MaxConfig;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerDropItemEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{
        public function 
    onEnable(){
            @
    mkdir($this->getDataFolder());
            
    $this->worlds = (new Config($this->getDataFolder()."config.yml"Config::YAML, array(
                
    "allow-drop" => true,
                )))->
    getAll();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("MaxConfig Loaded!");
        }
     
        
    /**
         * @param PlayerDropItemEvent $event
         *
         * @priority       NORMAL
         * @ignoreCanceled false
         */
        
    public function onDrop(PlayerDropItemEvent $event){
            
    $player $event->getPlayer();
            if(
    $this->config->get("allow-drop") !== false) {
                
    $player->sendMessage("[MaxConfig] A magical force is stoping you from doing this!");
                
    $event->setCancelled();
            }elseif(
    $this->config->get("allow-drop") !== true) {
                return 
    true;
            }
        }
    }
    Like this?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Remove the getAll(). Store the new Config() directly to $this->worlds.

    Also, since you expect $this->worlds->get("allow-drop") to be a boolean, you can directly check:
    PHP:
    if(!$this->worlds->get("allow-drop")){ // if not allow drop; ! means not
      // cancel event
    }
    // and you don't need to do something for else.
    // first, since it is either true or false, if it is not true it must be false, so else is enough and no need elseif.
    // second, if nothing happens, you don't have to cancel, so just execute no code. You don't need to return anything for return handlers.
    Also, useful link: http://php.net/manual/en/language.operators.logical.php
    Last edited: Dec 3, 2014
  9. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    PHP:
    <?php

    namespace MaxConfig;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerDropItemEvent;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\level\Position;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\math\Vector3;

    class 
    Main extends PluginBase implements Listener{
        public function 
    onEnable(){
            @
    mkdir($this->getDataFolder());
            
    $this->worlds = (new Config($this->getDataFolder()."config.yml"Config::YAML, array(
                
    "allow-drop" => true,
                )))->
    getAll();
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->getLogger()->info("MaxConfig Loaded!");
        }
        
    /**
         * @param PlayerDropItemEvent $event
         *
         * @priority       NORMAL
         * @ignoreCanceled false
         */
        
    public function onDrop(PlayerDropItemEvent $event){
            
    $player $event->getPlayer();
            if(!
    $this->worlds->get("allow-drop")){
                
    $player->sendMessage("[MaxConfig] A magical force is stopping you from doing this!");
                
    $event->setCancelled();
            }
        }
    }
    Fixed the first 2, Replace line 12 with $this->worlds?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I
    I said, take away ->getAll(). Save new Config() to $this->worlds directly.
  11. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    Remember that if you do:
    PHP:
    $this->config $this->getConfig();
    //Or
    $this->config = new Config (etc...)...
    $this->config will be an instance of Config and you can get or set values using:
    PHP:
    //Get
    $this->config->get($var);
    //Set
    $this->config->set($var$var);
    But if you use getAll(), $this->config will be an array and you can get/set values as normal array. Example:
    PHP:
    //Get
    $example $this->config[$key];
    //Set
    $this->config[$key] = $value;
    Please like if I helped you :)
    xFlare likes this.
  12. xFlare
    Offline

    xFlare Active Member Plugin Developer

    Joined:
    Sep 28, 2014
    Posts:
    199
    Plugins:
    1
    Minecraft User:
    xFlare
    I got it to work :D

    I can finally make plugins with configs, Thank you @PEMapModder and @EvolSoft for helping me :D
    EvolSoft likes this.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Do not use $this->config directly, call it something other than $this->config; how many times I said that...
  14. EvolSoft
    Offline

    EvolSoft Notable Member Plugin Developer

    Joined:
    Sep 10, 2014
    Posts:
    821
    Plugins:
    15
    Minecraft User:
    Flavius12
    It was only an example to explain how the config works :)
    xFlare likes this.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes, but please stop posting that. People will follow your usage of $this->config and then the next thing I know will be everyone complaining $this->getConfig() doesn't work.
    xFlare likes this.
  16. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    Um, eheh, I may have done it accidentally about a month ago, in the Resources forum. I was showing how to make a config, but @Falk removed it quickly, the next day I believe, and told me the same thing about it being discouraged soooo...yeah. Sorry.
    hoyinm14mc and EvolSoft like this.

Share This Page

Advertisement