1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Solved (Edited) sendMessage help

Discussion in 'Development' started by TheClimbing, Jan 3, 2018.

  1. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    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:
    <?php

    namespace 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");
            }

        }
    }
     
  2. Palente

    Palente Slime

    Messages:
    75
    GitHub:
    palente
    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");
     
  3. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    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!");
          
        }
      
     
  4. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    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.
     
  5. LewBr

    LewBr Zombie

    Messages:
    385
    GitHub:
    lewbr
    This code should work, are your plugin running correctly?
     
  6. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    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?
     
  7. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    Here is the zip file if you want to heck it too
     

    Attached Files:

  8. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Here:

    PHP:
    <?php

    namespace 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
     
    Levi and LewBr like this.
  9. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    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.
     
  10. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    Post your code, $plugin shouldn't be used anywhere (from the code I saw).
     
    Levi likes this.
  11. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    Thank you for everything I edited my previous reply.Problem solved!
     
    0x15f likes this.
  12. 0x15f

    0x15f Baby Zombie

    Messages:
    145
    GitHub:
    0x15f
    No problem, glad I could help.
     
    Levi likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.