So when I try this. Which is a basic plugin to dosplay Message on player join I get an error telling me __construct() needs more arguments.I just started PHP I meaan like yesterday.Please can anyone tell me where i'm wrong? PHP: <?phpnamespace playerjoin;use pocketmine\event\Listener;use pocketmine\event\player\PlayerJoinEvent;use pocketmine\plugin\PluginBase;class PlayerJoin extends PluginBase implements Listener{ private $api; public function __construct(PlayerJoin $plugin) { $this->api = $plugin; $plugin->getServer()->getPluginManager()->registerEvents($this, $plugin); } public function onJoin(PlayerJoinEvent $e) { $p = $e->getPlayer(); foreach ($p as $pl) { $pl->plugin->sendMessage("Hello"); } }}
i think that is the main file so why use __construct(PlayerJoin) that is not needed the plugin work without it register events in the onEnable PHP: public function onEnable(){$this->getServer()->getPluginManager()->registerEvents($this, $this);} Now that will not work $p = $e->getPlayer(); is a player not an array so don't use the for expression use $p->sendMessage("Hello");
Why do you need to use __construct? You can load PlayerJoinEvent on your main file and you don't need foreach to send a message. PHP: public function onJoin(\pocketmine\event\player\PlayerJoinEvent $ev){ $player = $ev->getPlayer(); $player->sendMessage("Hello!"); }
Thank you a bunch guys a learned a lot from you replies.But I can't get the message in-game I rewrote it like that PHP: public function onPlayerJoinEvent(PlayerJoinEvent $e) { $player = $e->getPlayer(); $name = $player->getName(); $player->sendMessage(TextFormat::BLUE . "Welcome" . TextFormat::GOLD . $name . "To our server!"); } Edit: There are no errors either.
The structure of my plugin is fine,I have made the plugin.yml by standarts, it loads on server with no other plugins without errors.So i can say everything is checked but the plugin doesn't send the message to the player.Do I need onEnable() and registrer events?
Here: PHP: <?phpnamespace playerjoin;use pocketmine\event\Listener;use pocketmine\event\player\PlayerJoinEvent;use pocketmine\plugin\PluginBase;use pocketmine\utils\TextFormat;class PlayerJoin extends PluginBase implements Listener { public function onEnable() { $plugin->getServer()->getPluginManager()->registerEvents($this, $this); } public function onJoin(PlayerJoinEvent $e) { $ev->getPlayer()->sendMessage(TextFormat::BLUE . "Welcome" . TextFormat::GOLD . $ev->getPlayer()->getName() . " To our server!"); }} Most plugins need an onEnable function in the main class, the function is called when the plugin is enabled allowing you to do any number of things but in your case register an event listener. Event listeners must implement 'pocketmine\event\Listener', to register the listener you need to: PHP: <?php$server = $this->getServer(); //returns instance of pocketmine\Server$plugin_manager = $server->getPluginManager() //idk the path to the class but it returns a PluginManager class (i think)$class = $this; //whatever class that implements pocketmine\event\Listener$owner = $this; //'class owner' this is always the main class$plugin_manager->registerEvents($class, $owner); //your class is registered
I got confused. I used PHP: $this->getServer()->getPluginManager()->registerEvents($this, $this); But now I get Undefined variable $plugin.I don't know maybe it's too late. Edit I found it... it's PHP: $this->getServer()->getPluginManager()->registerEvents(new PlayerJoin($this), $this); I'm seriously retarded.However thank you everyone for the fast replies,understanding and calmness.