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

How to get a Server Instance in AsyncTasks in API 4.0.0 without modifying the Task constructor

Discussion in 'Facepalm' started by Darku, Oct 8, 2018.

  1. Darku

    Darku Spider Jockey

    Messages:
    49
    GitHub:
    DarkWav
    As of recent API changes in PocketMine-MP, the onCompletion() function of AsyncTasks does no longer receive a Server Instance.
    e.g.:
    Code:
    public function onCompletion(Server $server) : void
    {
    }
    
    changed to
    Code:
    public function onCompletion() : void
    {
    }
    
    Now: How do we get the server instance again WITHOUT having to try to serialize it in the constructor (which sometime is impossible)?
    The answer is very simple: Create a direct call to the static getInstance() function of the Server class.
    To use this, make sure you have
    Code:
    use pocketmine\Server;
    
    in you asynctask's imports.
    Now, to regain your $server variable,
    just create the direct call to the Server::getInstance() function in your onCompletion() function.
    So the code changed from:
    Code:
    public function onCompletion(Server $server) : void
    {
    }
    
    to:
    Code:
    public function onCompletion() : void
    {
      $server = Server::getInstance();
    }
    
    Small notice for staff:
    It might be a good idea to move this in the low quality section as it
    is not relevant for a lot of people...
     
    XenialDan likes this.
  2. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    This is very easy and I do not think it would help, but if anyone has that issue, the class for Server is pocketmine\Server. This means you have to add use pocketmine\Server; in the imported classes.
     
  3. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    4.0.0 is indev and subject to change, so this cannot be considered a "resource".

    This is a "quick fix" solution which anyone with half a brain could come up with on their own. A walkthrough of the usage of thread-local storage would have been more valuable (most plugin tasks don't need a server, they need their plugin instance).
     
  4. Darku

    Darku Spider Jockey

    Messages:
    49
    GitHub:
    DarkWav
    I agree. I just thought the developemnt section might be the wrong place to post this since this is a small fix description rather than a request. I designed this with the hope that it will save a bit of someone's time, but if you think a how to get a plugin's instance guide would be more usefull I could do that aswell, of course.
    I will post the next guide directly in the facepalm section, just to make sure.
     
  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.