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

[NEW API]Help with Call to a member function ... on a non-object

Comments in 'Plugin Development' started by xionbig, Jun 17, 2014.

  1. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    Hello everyone, I began to developing my plugin for my server.
    I'm developing my plugin for the new API and it came out a problem that I can not fix! (I use windows)

    The console says: Call to a member function getOnlinePlayers () on a non-object ...

    here is my code:
    class main
    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\event\Listener;
    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;
    use 
    pocketmine\utils\TextFormat;

    class 
    main extends PluginBase implements Listener{
        public 
    $timer;

        public function 
    onLoad() {}
        public function 
    onEnable(){  
            
    $this->getServer()->getPluginManager()->registerEvents($this$this);
            
    $this->timer = new Timer($this);    
            
    $this->getServer()->getScheduler()->scheduleRepeatingTask($this->timer4800);
        }
        public function 
    eventi(){
            
    //Call to a member function getOnlinePlayers() on a non-object
            
    $online count($this->getServer()->getOnlinePlayers());
            
    Server::getInstance()->broadcastMessage("There are  ".$online." players ...");
        }
        public function 
    onDisable(){}
    }
    class time
    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\scheduler\PluginTask;

    class 
    Timer extends PluginTask{
        public 
    $main;
        public function 
    onRun($currentTick){

            
    $this->main = new main($this);
            
    $this->main->eventi();  
        }
    }

    In the old API this code works
    PHP:
    $this->api->chat->broadcast("Now there are: ".count($this->api->player->getAll())." players online...");
                    
    how can fix this error?
  2. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Try changing the Timer class to:
    PHP:
    <?php
    namespace Test;

    use 
    pocketmine\scheduler\PluginTask;

    class 
    Timer extends PluginTask{
        public function 
    onRun($currentTick){
            
    $this->getOwner()->eventi();
        }
    }
    xionbig likes this.
  3. xpyctum
    Offline

    xpyctum Active Member Plugin Developer

    Joined:
    Jan 13, 2014
    Posts:
    292
    Plugins:
    4
    Minecraft User:
    tylerdisney
    PHP:
    Server::getInstance()->broadcastMessage("Now there are : "$online" players online");
    Try this :/
  4. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    Thank you Falk, your solution works.
    I have another problem, I can not update this short code of the old API to the new API

    PHP:
    case 'entity.health.change':
    return 
    false;
    break; 

    I tried searching in http://docs.pocketmine.net/ something about the player's health, but I did not find anything, could you help me please?
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Because there is nine for that yet.

    Also, never make a new plugin instance.
    xionbig likes this.
  6. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Yeah, that is generally bad and would lead to an awful memory overflow if you actually initialized the onEnable :)
    xionbig likes this.
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It won't work because PluginBase::init() isn't called anyways.
  8. xionbig
    Offline

    xionbig Active Member Plugin Developer

    Joined:
    Jun 11, 2014
    Posts:
    106
    Plugins:
    2
    Minecraft User:
    NikoZazza
    Thanks to everyone for the help

Share This Page

Advertisement