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

Task and Objects

Discussion in 'Development' started by M4nt0s, Oct 23, 2018.

  1. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    Can someone please explain me how to add Objects(Player, Level...) to class. So I can use them in my Task(onRun() Function). Just want to send Message to Player via Task.
    First time making a task. #newbie
    Main Class:
    ->Just the part for Task<-
    PHP:
    $this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer()), 120);
    BroadcastTask.php
    PHP:
    class BroadcastTask extends Task implements Listener{

        
    /** @var Server */
        
    private $server;

        public function 
    __construct(Server $server){
            
    $this->server $server;
        }

        public function 
    onRun(int $currentTick) : void{
            
    $this->server->broadcastMessage("[ExamplePlugin] Derzeitiger Tick " $currentTick);
        }
     
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    $this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer(), $player), 120);
    public function __construct(Server $server, \pocketmine\Player $player){
    $this->server = $server;
    $this->player = $player;
    }
     
  3. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    Now?

    Server thread/CRITICAL ErrorException: "Undefined variable: player" (EXCEPTION) in "FZ.phar/src/FZ/Main" at line 44 23.10 19:58:57 [Server] Server thread/DEBUG #0 FZ.phar/src/FZ/Main(44): pocketmine\utils\Utils::errorExceptionHandler(integer 8, string Undefined variable: player, string phar:///plugins/FZ.phar/src/FZ/Main.php, integer 44, array Array())
     
  4. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    I told you how to pass the player in the constructor. You have to define the player. What do you want to do, so I will be able to help you?
     
  5. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    Define player ✔️
    First of all I want to learn how to make a task. I think I got it now a bit.
    My goal is to make a sign refreshing itself and show me the players in a specific world.
     
  6. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    you could do this
    PHP:
    foreach($this->server->getOnlinePlayers() as $player){
    $player->sendMessage("You are " $player->getName());
    }
     
  7. M4nt0s

    M4nt0s Spider Jockey

    Messages:
    31
    GitHub:
    M4nt0s
    My mistake. I want to count the players in a world not the name.
     
  8. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    Use the code you had then.
    Do something like that:
    $count = count($this->server>getLevelByName("worldname")->getPlayers());
    then send a message with $count.
    like with $currentTick in your example.
     
    Last edited: Oct 24, 2018
    xXNiceAssassinlo YT likes this.
  9. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    I wouldn't pass any objects to your task unless you need to directly modify or use those objects, and even then, you could just use player name or some ID rather than the Player object. Assuming you want to do it on a task, I'd do something like this:
    PHP:
     //In your PluginBase class:
     
    $this->getScheduler()->scheduleRepeatingTask(new BroadcastTask($this->getServer(), count($this->getServer()->getLevelByName("worldname")->getPlayers())), 120);

    //In your BroadcastTask class:
    class BroadcastTask extends Task implements Listener{

        
    /** @var Server */
        
    private $server;

        
    /** @int PlayerCount */
        
    private $count 0;

        public function 
    __construct(Server $serverint $count){
            
    $this->server $server;
            
    $this->count $count//You can now use the player count however you want in your task.
        
    }

        public function 
    onRun(int $currentTick) : void{
            
    $this->server->broadcastMessage("[ExamplePlugin] Derzeitiger Tick " $currentTick);
        }
    }
    You probably see that you can add any variable to a task just by putting it in the object class' constructor and by specifying it when you create an object, like you did with the server object. I didn't include a player object because it was unnecessary. Assuming the task isn't intended for only one specific player, you can use the Server object you already have to get all players on the server, get the server IP, specific Levels, etc. However, you didn't include the PluginBase object, which most people do instead of the Server. PluginBase can access everything you would ever need, so it's typically more useful, but not necessarily in every case.
     
  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.