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

How to send args to PluginTask()

Comments in 'Plugin Development' started by blocksandgold, Jul 9, 2014.

  1. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    I found this example,
    PHP:
    class BroadcastPluginTask extends PluginTask{

       public function 
    onRun($currentTick){
         
    Server::getInstance()->broadcastMessage("[ExamplePlugin] I've run on tick " $currentTick);
      }
    }
    but i need to send args to my task. For example the player to the send a message.
    How should i do ?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Add it to the constructor. Learn PHP about classes and objects if you don't know how.
    64FF00 and Tuff like this.
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    blocksandgold likes this.
  4. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    i've done it but i have another issue, so i supposed it was not the good technic :
    PHP:
    class ForceKickPluginTask extends PluginTask{

       private 
    $playername;
       private 
    $message;

       public function 
    __construct($playername$message){
         
    $this->playername $playername;
         
    $this->message $message;
       }
       
       public function 
    forceKick($playername$message){
         
    //if(($player = $this->getServer()->getPlayerExact($player)) instanceof Player){
         //$player->close($data[1]);
         //}
         
    $player $this->getServer()->getPlayerExact($playername);
         if (
    $player instanceof Player) {
           
    $player->close($message);
         }
         else {
           
    Main::log("WARNING""forceKick but target is not a player");
         }     
       }
       public function 
    onRun($currentTick){
         
    $this->forceKick($this->playername$this->message);
       }
    }
    PHP:
          $this->getServer()->getScheduler()->scheduleDelayedTask(new ForceKickPluginTask($player->getName(), $msg), 200);
    I have this error :

    Code:
    Fatal error: Call to a member function isEnabled() on a non-object in phar:///home/minecraft/pocketmine/PocketMine-MP.phar/src/pocketmine/scheduler/ServerScheduler.php on line 167
    
    
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You have to pass the plugin object too.

    Call parent::__construct($plugin) at the constructor.
    blocksandgold likes this.
  6. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Ok thank :)
    I suppose it's that, but you callback class in utils perfect :) ==> parent::__construct($plugin);
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    :)
    64FF00 likes this.
  8. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    oh we are talking at the same time :) thx a lot :)
    64FF00 and PEMapModder like this.
  9. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    ok that works fine, but i still have the infinite loop with the sheduledelayedtask call !

    Code:
    2014-07-09 12:01:00 [WARNING] [bag_auth] player kicked by server (forcekick)
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:00 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:01 [WARNING] [bag_auth] forceKick but target is not a player
    2014-07-09 12:01:01 [WARNING] [bag_auth] forceKick but target is not a player
    
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Also, why did you pass the name instead of the object directly?
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Whole source please.
    64FF00 likes this.
  12. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    PHP:
    <?php

    namespace  blocksandgold\Bag_auth\lib;

    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Server;

    use 
    blocksandgold\Bag_auth\Main;

    class 
    ForceKickPluginTask extends PluginTask{

       private 
    $playername;
       private 
    $message;

       public function 
    __construct(Plugin $plugin$playername$message){
         
    parent::__construct($plugin);
         
    $this->playername $playername;
         
    $this->message $message;
       }
     
       public function 
    forceKick($playername$message){
         
    //if(($player = $this->getServer()->getPlayerExact($player)) instanceof Player){
         //$player->close($data[1]);
         //}
         
    $player Server::getInstance()->getPlayerExact($playername);
         if (
    $player instanceof Player) {
           
    $player->close($message);
           
    Main::log("WARNING""player kicked by server (forcekick)");
         }
         else {
           
    Main::log("WARNING""forceKick but target is not a player");
         }   
       }
       public function 
    onRun($currentTick){
         
    $this->forceKick($this->playername$this->message);
       }
    }
    And the call from the plugin =>
    PHP:
          $this->getServer()->getScheduler()->scheduleDelayedTask(new ForceKickPluginTask($this$player->getName(), $msg), 200);
  13. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    I think there is a bug in the scheduleDelayedTask() method because i have no bug with the scheduleRepeatingTask() (even if i use the CallBack Task)
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I told you, plugin task is the same except it is disabled when you are unloaded.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I want the full code from the main class.
  16. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    ok i'll zip you and send it to you but, i warn you, it is big ! (i cannot copy-past it here because there is security informations inside)

    Also, i read your worldeditart plugin, and i saw you use scheduleDelayedTask() too, did you test it with the last build ?
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I didn't test it at all :p
  18. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    Can you send me your mail or something ? To send you the source code ?
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PM?
  20. blocksandgold
    Offline

    blocksandgold Active Member

    Joined:
    May 7, 2014
    Posts:
    68
    Minecraft User:
    bifag
    It was a bug in the core. Fixed since a couple of hours by shogicp ... thx
    PEMapModder likes this.

Share This Page

Advertisement