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

Server instance AsyncTask

Discussion in 'Facepalm' started by Matthew, Feb 6, 2018.

  1. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    This can go right into the facepalm section but I'm trying to send 2 strings and an array to a async task.

    The error I'm getting:

    Code:
    [10:30:07] [Server thread/CRITICAL]: Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'PlayerInfo v1.3.3': Cannot serialize Server instance on Matthww\PlayerInfo\PlayerInfo
    [10:30:07] [Server thread/CRITICAL]: BadMethodCallException: "Cannot serialize Server instance" (EXCEPTION) in "src/pocketmine/Server" at line 2621
    [10:30:07] [Server thread/DEBUG]: #0 plugins/PlayerInfo/src/Matthww/PlayerInfo/Tasks/SaveTask(23): pocketmine\Server->__sleep()
    [10:30:07] [Server thread/DEBUG]: #1 plugins/PlayerInfo/src/Matthww/PlayerInfo/PlayerInfo(107): Matthww\PlayerInfo\Tasks\SaveTask->__construct(string Matthww, array Array(), string D:\Matthew\Documents\PocketMine-MP\plugins\PlayerInfo//players/matthww.json)
    [10:30:07] [Server thread/DEBUG]: #2 plugins/PlayerInfo/src/Matthww/PlayerInfo/PlayerInfo(63): Matthww\PlayerInfo\PlayerInfo->Save(pocketmine\Player object, string iOS, string iPhone 8, string Classic UI, string Maximum, string Touch)
    [10:30:07] [Server thread/DEBUG]: #3 src/pocketmine/plugin/MethodEventExecutor(38): Matthww\PlayerInfo\PlayerInfo->onJoin(pocketmine\event\player\PlayerJoinEvent object)
    [10:30:07] [Server thread/DEBUG]: #4 src/pocketmine/plugin/RegisteredListener(98): pocketmine\plugin\MethodEventExecutor->execute(Matthww\PlayerInfo\PlayerInfo object, pocketmine\event\player\PlayerJoinEvent object)
    [10:30:07] [Server thread/DEBUG]: #5 src/pocketmine/plugin/PluginManager(721): pocketmine\plugin\RegisteredListener->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [10:30:07] [Server thread/DEBUG]: #6 src/pocketmine/Player(974): pocketmine\plugin\PluginManager->callEvent(pocketmine\event\player\PlayerJoinEvent object)
    [10:30:07] [Server thread/DEBUG]: #7 src/pocketmine/Player(922): pocketmine\Player->doFirstSpawn()
    [10:30:07] [Server thread/DEBUG]: #8 src/pocketmine/level/Level(2532): pocketmine\Player->sendChunk(integer 5, integer 11, pocketmine\network\mcpe\protocol\BatchPacket object)
    [10:30:07] [Server thread/DEBUG]: #9 src/pocketmine/level/Level(2577): pocketmine\level\Level->sendChunkFromCache(integer 5, integer 11)
    [10:30:07] [Server thread/DEBUG]: #10 src/pocketmine/level/format/io/ChunkRequestTask(88): pocketmine\level\Level->chunkRequestCallback(integer 5, integer 11, pocketmine\network\mcpe\protocol\BatchPacket object)
    [10:30:07] [Server thread/DEBUG]: #11 src/pocketmine/scheduler/AsyncPool(164): pocketmine\level\format\io\ChunkRequestTask->onCompletion(pocketmine\Server object)
    [10:30:07] [Server thread/DEBUG]: #12 src/pocketmine/scheduler/ServerScheduler(342): pocketmine\scheduler\AsyncPool->collectTasks()
    [10:30:07] [Server thread/DEBUG]: #13 src/pocketmine/Server(2536): pocketmine\scheduler\ServerScheduler->mainThreadHeartbeat(integer 285)
    [10:30:07] [Server thread/DEBUG]: #14 src/pocketmine/Server(2279): pocketmine\Server->tick()
    [10:30:07] [Server thread/DEBUG]: #15 src/pocketmine/Server(2154): pocketmine\Server->tickProcessor()
    [10:30:07] [Server thread/DEBUG]: #16 src/pocketmine/Server(1742): pocketmine\Server->start()
    [10:30:07] [Server thread/DEBUG]: #17 src/pocketmine/PocketMine(553): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string D:\Matthew\Documents\PocketMine-MP\, string D:\Matthew\Documents\PocketMine-MP\plugins\)

    The PlayerJoinEvent function:
    PHP:
    public function onJoin(PlayerJoinEvent $joinEvent) {
        
    $data $this->PlayerData[$joinEvent->getPlayer()->getName()];
        
    $this->Save($joinEvent->getPlayer(), $data["DeviceOS"], $data["DeviceModel"], $data["UIProfile"], $data["GuiScale"], $data["CurrentInputMode"]);
    }
    My Save function (still needs cleaning):
    PHP:
    public function Save($usernamestring $OSstring $modelstring $UIstring $GUIstring $controls) {
        
    $DeviceOS = ["Unknown""Android""iOS""macOS""FireOS""GearVR""HoloLens""Windows 10""Windows""Dedicated""Orbis""NX"];
        
    $PlayerUI = ["Classic UI""Pocket UI"];
        
    $PlayerControls = ["Unknown""Mouse""Touch""Controller"];
        
    $PlayerGUI = [-=> "Minimum", -=> "Medium"=> "Maximum"];

        echo 
    "\nrunned save\n";

        if(
    $username instanceof Player) {

            echo 
    "\nrunning async task!\n";

            
    $player $username->getName();
            
    $ip $username->getAddress();
            
    $OS $DeviceOS[$OS];
            
    $model $this->DeviceModel($model);
            
    $UI $PlayerUI[$UI];
            
    $GUI $PlayerGUI[$GUI];
            
    $controls $PlayerControls[$controls];

            
    $data = array(
                
    "username" => $username,
                
    "IP" => $ip,
                
    "OS" => $OS,
                
    "DeviceModel" => $model,
                
    "UI" => $UI,
                
    "GUI" => $GUI,
                
    "Controls" => $controls);

            
    $path $this->getDataFolder() . "/players/" strtolower($player) . ".json";

            
    $this->getServer()->getScheduler()->scheduleAsyncTask(new SaveTask($player$data$path));
        }
    }
     
  2. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    You cant pass classes with server to the task
     
  3. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    What am I passing that is a class? All I passed was 2 string and an array.
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    What is line 23 of SaveTask? Have you tried var_dump()ing what you have passed?
     
  5. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    Totally forgot to paste my Task

    PHP:
        public function __construct($username$data$path) {
            
    $this->savedata = new Config($pathConfig::JSON, []);
            
    $this->username $username;
            
    $this->data $data;
            
    $this->path $path;
        }

        public function 
    onRun(): void {
            
    safe_var_dump($this->username);
            
    safe_var_dump($this->data);
            
    safe_var_dump($this->path);
            echo 
    "hello";

            if(
    is_file($this->path)) {
                
    $data json_decode(file_get_contents($this->path), true) ?? [];
            } else {
                
    $data = [];
            }

            
    safe_var_dump($this->data);

            
    file_put_contents($this->pathjson_encode($data));
        }
    Line 23 is bold:
    public function __construct($username, $data, $path) {
    $this->savedata = new Config($path, Config::JSON, []);
    $this->username = $username;
    $this->data = $data;
    $this->path = $path;
    }
     
  6. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    PHP:
     $player $username->getName();
    gotcha! Your $"username" is actually a player instance, which you are passing in the array.
     
    Matthew and SOFe like this.
  7. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    var_dump your $data before storing it to a class property :facepalm:
     
    Matthew likes this.
  8. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    Thanks! Now let's get this thread moved to facepalm.
     
    jasonwynn10 likes this.
  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.