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

Serialization of 'Closure' is not allowed

Comments in 'Plugin Development' started by haniokasai, Dec 19, 2015.

  1. haniokasai
    Offline

    haniokasai New Member

    Joined:
    Jun 22, 2015
    Posts:
    6
    Minecraft User:
    haniokasai
    hi
    im making inventory-backup plugin.
    Code:
    <?php
    
    namespace NoLostItem;
    
    use pocketmine\Server;
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\Player;
    use pocketmine\event\player\PlayerJoinEvent;
    use pocketmine\permission\Permission;
    use pocketmine\command\Command;
    use pocketmine\command\CommandExecutor;
    use pocketmine\command\CommandSender;
    use pocketmine\event\player\PlayerQuitEvent;
    use pocketmine\item\Item;
    use pocketmine\utils\TextFormat;
    use pocketmine\scheduler\CallbackTask;
    use pocketmine\scheduler\ServerScheduler;
    use pocketmine\utils\Config;
    
    class NoLostItem extends PluginBase implements Listener{
    
        public function onEnable(){
       
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
            $task = new CallbackTask(array($this, "schedule"));
            $time = 40;
            $this->getServer()->getScheduler()->scheduleRepeatingTask($task, $time);
            $dbf = $this->getDataFolder()."item.sqlite3.db";
            if(!file_exists($this->getDataFolder())){
                @mkdir($this->getDataFolder(), 0744, true);
            }
            if(!file_exists($dbf)){
                $this->db = new \SQLite3($dbf, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
            }else{
                $this->db = new \SQLite3($dbf, SQLITE3_OPEN_READWRITE);
            }
            $this->db->query("CREATE TABLE IF NOT EXISTS itemp (name TEXT PRIMARY KEY, id TEXT, armor TEXT)");
            $this->getLogger()->info(TextFormat::GREEN."sqlite3 was loaded!");
           
           
        }
    
        public function onDisable(){
            $this->schedule();
            $this->db->close();
            $this->getLogger()->info(TextFormat::GREEN."sqlite3 was unloaded!");
        }
        public function joinplayer(PlayerJoinEvent $event){
            $player = $event->getPlayer();
            $name = $player->getName();
            $player->getInventory()->clearall();
            $result = $this->db->query("SELECT name,id, armor FROM itemp WHERE name = \"$name\"");
            $rows = $result->fetchArray(SQLITE3_ASSOC);
    
            if($result){
                    if($rows['name'] != null and $rows['id'] != null and $rows['armor'] != null){
    
                        $player->getInventory()->setContents(unserialize(base64_decode($rows['id'])));
                       
                        $player->getInventory()->setArmorContents(unserialize(base64_decode($rows['armor'])));
                }}
        }
       
        public function quitgo (PlayerquitEvent $event){
            $this->dare($event->getPlayer());
            $event->getPlayer()->getInventory()->clearall();
       
        }
            public function schedule (){
            foreach($this->getServer()->getOnlinePlayers() as $player){
            #$this->dare($player);
    
           
       
        }        }
            public function dare ($player){
               
            $name = $player->getName();
            $this->db->query("DELETE FROM itemp WHERE name = \"$name\"");
           
            $id = base64_encode(serialize($player->getInventory()->getContents()));
            $armor = base64_encode(serialize($player->getInventory()->getArmorContents()));
            $this->db->query("INSERT OR REPLACE INTO itemp VALUES(\"$name\", \"$id\", \"$armor\")");
       
        }
    }
    
    http://haniokasai.com/file/NoLostItemsql.zip

    when i use a item and left the server,this error happened.

    Code:
    [14:39:38] [CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerQuitEvent' to 'NoLostItem v1.0.0': Serialization of 'Closure' is not allowed on NoLostItem\NoLostItem
    [14:39:38] [CRITICAL]: Exception: "Serialization of 'Closure' is not allowed" (EXCEPTION) in "/NoLostItemsql/src/NoLostItem/NoLostItem" at line 82
    [14:39:38] [INFO]: haniokasai_dev left the game
    [14:39:38] [INFO]: haniokasai_dev[/192.168.1.3:19132] logged out due to client disconnect
    Please tell how to fix it.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Format the item array yourself. Serializing the items may not work in new versions of PocketMine where items contain more data (and appears to contain reference to the server instance? Not sure but a wild guess)
  3. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    Maybe as @PEMapModder said, make an array your self like this:

    Code:
    array(
       array(
            id,
            meta,
            count        
       )
       array(
            id,
            meta,
            count        
       )
    )
    
    AndrewBit and haniokasai like this.
  4. haniokasai
    Offline

    haniokasai New Member

    Joined:
    Jun 22, 2015
    Posts:
    6
    Minecraft User:
    haniokasai
  5. haniokasai
    Offline

    haniokasai New Member

    Joined:
    Jun 22, 2015
    Posts:
    6
    Minecraft User:
    haniokasai
    @PEMapModder and @Legoboy0215 ,thanks<3
    my plugin is neary finished!
    Code:
    <?php
    
    namespace NoLostItem;
    
    use pocketmine\Server;
    use pocketmine\plugin\PluginBase;
    use pocketmine\event\Listener;
    use pocketmine\Player;
    use pocketmine\event\player\PlayerJoinEvent;
    use pocketmine\permission\Permission;
    use pocketmine\command\Command;
    use pocketmine\command\CommandExecutor;
    use pocketmine\command\CommandSender;
    use pocketmine\event\player\PlayerQuitEvent;
    use pocketmine\item\Item;
    use pocketmine\utils\TextFormat;
    use pocketmine\scheduler\CallbackTask;
    use pocketmine\scheduler\ServerScheduler;
    use pocketmine\utils\Config;
    
    class NoLostItem extends PluginBase implements Listener{
    
        public function onEnable(){
       
            $this->getServer()->getPluginManager()->registerEvents($this, $this);
            $task = new CallbackTask(array($this, "schedule"));
            $time = 400;
            $this->getServer()->getScheduler()->scheduleRepeatingTask($task, $time);
            $dbf = $this->getDataFolder()."item.sqlite3.db";
            if(!file_exists($this->getDataFolder())){
                @mkdir($this->getDataFolder(), 0744, true);
            }
            if(!file_exists($dbf)){
                $this->db = new \SQLite3($dbf, SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE);
            }else{
                $this->db = new \SQLite3($dbf, SQLITE3_OPEN_READWRITE);
            }
            $this->db->query("CREATE TABLE IF NOT EXISTS itemp (name TEXT PRIMARY KEY, id TEXT, armor TEXT)");
            $this->getLogger()->info(TextFormat::GREEN."sqlite3 was loaded!");
           
           
        }
    
        public function onDisable(){
            $this->schedule();
            $this->db->close();
            $this->getLogger()->info(TextFormat::GREEN."sqlite3 was unloaded!");
        }
        public function joinplayer(PlayerJoinEvent $event){
            $player = $event->getPlayer();
            $name = $player->getName();
            $player->getInventory()->clearall();
            $result = $this->db->query("SELECT name,id, armor FROM itemp WHERE name = \"$name\"");
            $rows = $result->fetchArray(SQLITE3_ASSOC);
    
            if($result){
                    if($rows['name'] != null and $rows['id'] != null and $rows['armor'] != null){
                        $stack =unserialize(base64_decode($rows['id']));
                        $i =1;$ok =1;
    var_dump($stack);
    while($i <=36 and $ok == 1){
    if(@$player->getInventory()->addItem(Item::get($stack[$i]["id"],$stack[$i]["da"],$stack[$i]["co"]))){
    $i++;
    }else{
    $ok =0;
    }
    }
    
    
                       
                        $player->getInventory()->setArmorContents(unserialize(base64_decode($rows['armor'])));
                }}
        }
       
        public function quitgo (PlayerquitEvent $event){
            $this->dare($event->getPlayer());
            $event->getPlayer()->getInventory()->clearall();
       
        }
            public function schedule (){
            foreach($this->getServer()->getOnlinePlayers() as $player){
            #$this->dare($player);
    
           
       
        }        }
            public function dare ($player){
               
            $name = $player->getName();
            $this->db->query("DELETE FROM itemp WHERE name = \"$name\"");
            $stack =array();
    $i = 1;
            foreach($player->getInventory()->getContents() as $items){
            $stack[$i]["id"] = $items->getId();$stack[$i]["da"] = $items->getDamage();$stack[$i]["co"] = $items->getCount();
            $i++;
           
                                }
       
    var_dump($stack);
    
    $id = base64_encode(serialize($stack));
    $armor = base64_encode(serialize($player->getInventory()->getArmorContents()));
            $this->db->query("INSERT OR REPLACE INTO itemp VALUES(\"$name\", \"$id\", \"$armor\")");
       
        }
    }
    
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    CallbackTask has been deprecated. Stop using it and sweep it away from your mind today.
  7. haniokasai
    Offline

    haniokasai New Member

    Joined:
    Jun 22, 2015
    Posts:
    6
    Minecraft User:
    haniokasai
    I've got it! thanks!
  8. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan
    Who made TimeCapsule? I want to link hin this thread
  9. yandere_hk
    Offline

    yandere_hk New Member

    Joined:
    Nov 13, 2015
    Posts:
    1
    Minecraft User:
    haniokasai
  10. thebigsmileXD
    Offline

    thebigsmileXD Banned

    Joined:
    May 19, 2015
    Posts:
    845
    Plugins:
    1
    Minecraft User:
    XenialDan

Share This Page

Advertisement