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: <?phpnamespace 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; } }}?>
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.
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?
PHP: <?phpnamespace 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?
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
PHP: <?phpnamespace 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?
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
I got it to work I can finally make plugins with configs, Thank you @PEMapModder and @EvolSoft for helping me
Do not use $this->config directly, call it something other than $this->config; how many times I said that...
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.
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.