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

Cant use $this->getServer() - server::getInstance()

Comments in 'Plugin Development' started by Sensej, Oct 4, 2014.

  1. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Hello coders, i have a seconds class calles SenListener.
    In this task i extends the Plugin Base But i cant call on this class the $this->getServer() methode. Im using
    the server::getInstance() methode, can somebody tell my whi this does not working?

    -Sensej
  2. Dinokiller
    Offline

    Dinokiller Notable Member

    Joined:
    Sep 22, 2013
    Posts:
    376
    Minecraft User:
    Dinokiller_
    SenListener won't have $this->getServer(), I recommend you pass the server into the listener when it's created.
  3. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    so you mean on my main class
    PHP:
    $this->server $this->getServer();
    To the Listener
    PHP:
    public function __construct(mainclass $plugin)
        {    
            
    $this->plugin $plugin;
        }

    and 
    call them this->plugin->server
    ?
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $this->getOwner()->getServer(). The beat method.
    All things you can do as $this->blah() in $this->getServer() should be done as $this->getOwner()->blah() in a PluginTask. Note that it must be a public function. And $this->blah properties (@Dinokiller, you win) should be accessed with the same way $this->getOwner()->blah, except they must be public, again.
    CaptainKenji17 likes this.
  5. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Get this Error
    PHP:
    Fatal errorCall to undefined method Senplay\SenListener::getOwner() in C:\Server\SenServer\plugins\Senplay\src\Senplay\SenListener.php on line 3
    5
  6. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Line 35:
    PHP:
    $this->economy $this->getOwner()->getPluginManager()->getPlugin("economy");
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Wait you're not in a listener? Then pass the object via constructor.
  8. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,429
    Plugins:
    14
    No, it's supposed to be:
    PHP:
    $this->economy $this->getOwner();
  9. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    @LDX Nope the Economy Plugin is not not the Main Plugin.
    I use for the main class this:

    PHP:
    public function __construct(Senplay $plugin)
        {    
            
    $this->main $plugin;
            
    $this->economy $this->getOwner()->getPluginManager()->getPlugin("Economy");
        }
    LDX likes this.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What superclass are you extending?
  11. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,429
    Plugins:
    14
    PHP:
    $this->main->getServer()->getPluginManager()->getPlugin("Economy");
  12. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    PluginTask
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    $this->getOwner() returns your main class, so the server object is $this->getOwner()->getServer().

    Edit: remember to pass owner parameter to parent constructor.
  14. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    ...
    PHP:
    Fatal errorCall to a member function getServer() on null in ...
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Read my edit: pass the plugin object to the parent constructor.
    LDX likes this.
  16. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    PHP:
    My Code
    <?php

    namespace SenPlay;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerChatEvent;
    use 
    pocketmine\event\block\BlockBreakEvent;
    use 
    pocketmine\Player
    use 
    pocketmine\level\Position
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\level\Level;
    use 
    pocketmine\block\Block
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\item\Item;
    use 
    pocketmine\server

    class 
    SenListener extends PluginTask implements Listener
    {
        public function 
    __construct(SenPlay $plugin)
        {    
            
    $this->main $plugin;
            
    $this->economy $this->getOwner()->getServer()->getPluginManager()->getPlugin("Economy");
        }
         
    /*
         
         My Events
         
         */
    }
  17. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    and yes the Onrun function
  18. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Sorry found my Mistake....
    @PEMapModder is it better to use
    PHP:
     $this->main->getServer()->...; 
    or
    PHP:
    server::getInstance()->...;
    PS: I need the fastest methode :D
  19. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    I think what @PEMapModder is trying to tell you is
    Code:
    public function __construct(SenPlay $plugin)
        {   
            parent::__construct($plugin); //parent constructor
            $this->economy = $this->getOwner()->getServer()->getPluginManager()->getPlugin("Economy");
        }
    That's supposed to set the owner, I believe. Correct me if I'm wrong, anyone.
    PEMapModder likes this.
  20. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why did you both extend PluginTask and implement Listener?

Share This Page

Advertisement