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"...
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();
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.
What about using the default config.yml resource? PHP: $this->saveDefaultConfig(); $this->config = $this->getConfig()->getAll();
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"
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.
You need to make /resources folder in your plugin folder with /src and place the default config.yml file into there.
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)
PHP: <?phpnamespace _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"]); }}