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

Error with task / probably somthing simple

Discussion in 'Facepalm' started by Bagoschwago, Dec 26, 2016.

  1. Bagoschwago

    Bagoschwago Spider

    Messages:
    6
    GitHub:
    bagoschwago
    Hello,

    I have been going through the tutorial at learn-mc.ga/MCPE/ and its a great learning resource. From what I have learned there I have started to play with a test plugin and am running into trouble getting my task to execute.

    Here is my the reference to calling the task

    Command with the task

    PHP:
        public function onCommand(CommandSender $senderCommand $cmd$label, array $args){
            if(
    $cmd->getName() == "info"){
                if(!
    $sender instanceof Player){
                        
    $sender->sendMessage("This Command Only Works for players! Please perform this command IN GAME!");
                }else{
                        
    $sender->getInventory()->addItem(Item::get(364,0,4));
                        
    $sender->sendMessage("You have just recieved 4 steak!");
                        
    $task = new tasks\remindertext($this$sender->getName()); // Create the new class Task by calling
                        
    $this->getServer()->getScheduler()->scheduleDelayedTask($task5*20); // Counted in ticks (1 second = 20 ticks)
                
    }
            }
            return 
    true;
        }

    remindertext.php file located in Bago/tasks


    PHP:
    <?php

    namespace Bago\tasks;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    Bago\Main;

    class 
    remindertext extends PluginTask {

    public function 
    __construct(Main $mainstring $playername) {
        
    parent::__construct($main);
        
    $this->playername $playername;
    }

    public function 
    onRun($tick) { //
        
    $player $this->getOwner()->getServer()->getPlayer($this->playername());
        if(
    $player instanceof Player) {
            
    $player->sendMessage("10 seconds has past!");
        }
    }
    }
    When I then go into the game do the /info command it works fine, gives me the steaks then this error in console occurs.

    [Server thread/CRITICAL]: Could not execute task Bago\tasks\remindertext: Call to undefined method Bago\tasks\remindertext::playername()
    [Server thread/CRITICAL]: Error: "Call to undefined method Bago\tasks\remindertext::playername()" (EXCEPTION) in "/phar_BagoDev_uGuj5mz8tuc6lYm.phar/src/Bago/tasks/remindertext" at line 16

    I have tried to use pocketmine/player and server in the remindertext.php and that doesnt fix it nor should I need to call these since they are already called in the Main.php I believe but I tried just in case.

    Thanks for your help in advance!
    -Bago
     
  2. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Second file, remove the () from $this->playername where you want to get the player.
     
  3. Bagoschwago

    Bagoschwago Spider

    Messages:
    6
    GitHub:
    bagoschwago
    Thank you for the information! I see why that would cause the issue with the error.
    I have updated the 2nd file to read like this

    PHP:
    <?php

    namespace Bago\tasks;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    Bago\Main;

    class 
    remindertext extends PluginTask {

    public function 
    __construct(Main $mainstring $playername) {
        
    parent::__construct($main);
        
    $this->playername $playername;
    }

    public function 
    onRun($tick) { //
        
    $player $this->getOwner()->getServer()->getPlayer($this->playername);
        if(
    $player instanceof Player) {
            
    $player->sendMessage("10 seconds has past!");
        }
    }
    }
    Now I do not get the error YaY! however the task never occurs and there is no error in console. Humm.....

    -Bago
     
  4. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Debug everything and 5*20 = 100 and 100 ticks are actually 5 seconds. 1 second is 20 ticks.
     
  5. Bagoschwago

    Bagoschwago Spider

    Messages:
    6
    GitHub:
    bagoschwago
    Thanks! I know I need to update the text to 5 seconds. I just wanted to change the ticks so I didnt have to wait so long to see if it worked. It must be a conflict with some other part of the Main.php although the only other commands are join event and plugin loading messages in console. Here is the full code for the Main.php file.

    PHP:
    <?php

    namespace Bago;

    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\event\Listener;
    use 
    pocketmine\event\player\PlayerJoinEvent;
    use 
    pocketmine\event\player\PlayerQuitEvent;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\item\Item;



    class 
    Main extends PluginBase implements Listener{
        
        public function 
    onLoad(){
            
    $this->getLogger()->info(TextFormat::GREEN."Bago's Plugin Loading");
        }
        public function 
    onEnable(){
              
    $this->getServer()->getPluginManager()->registerEvents($this,$this);
              
    $this->getLogger()->info(TextFormat::GREEN."Bago's Plugin Enabled");
        }
        public function 
    onDisable(){
                
    $this->getLogger()->info(TextFormat::GREEN."Bago's Plugin Disabled");
        }
        public function 
    onJoin(PlayerJoinEvent $event){
              
    $player $event->getPlayer();
              
    $name $player->getName();
              
    $this->getServer()->broadcastMessage(TextFormat::RED."$name Joined Bagos test Server! Awesome!");
        }
        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args){
            if(
    $cmd->getName() == "info"){
                if(!
    $sender instanceof Player){
                        
    $sender->sendMessage("This Command Only Works for players! Please perform this command IN GAME!");
                }else{
                        
    $sender->getInventory()->addItem(Item::get(364,0,4));
                        
    $sender->sendMessage("You have just recieved 4 steak!");
                        
    $task = new tasks\remindertext($this$sender->getName()); // Create the new class Task by calling
                        
    $this->getServer()->getScheduler()->scheduleDelayedTask($task5*20); // Counted in ticks (1 second = 20 ticks)
                
    }
            }
            return 
    true;
        }
    }
     
  6. Primus

    Primus Zombie Pigman

    Messages:
    749
    In second file, the task class, import 'pocketmine/Player'
     
  7. Bagoschwago

    Bagoschwago Spider

    Messages:
    6
    GitHub:
    bagoschwago
    Primus Thank you! That was the final missing piece. You and imYannic are my heros! Thank you again!
     
  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.