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

Need help with my code.

Comments in 'Plugin Development' started by iiDeathStrokeii, Aug 13, 2015.

  1. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    Heres my code:
    PHP:
    public function onLoad(){
            
    $this->getLogger()->info(TextFormat::YELLOW."DataSave");
            @
    mkdir($this->getDataFolder() . "players/");
        }

        public function 
    onEnable(){
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        }
       
         public function 
    PlayerJoinEvent(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $ign $player->getName();
            
    $datafile = ($this->getDataFolder() . "players/".$ign.".json");
                if(!
    file_exists($datafile)){
                    
    $playerdata = new Config($this->getDataFolder()."players/".$player->getName().".json"Config::JSON);
                }
                    
    $file = ($this->getDataFolder() . "players/".$player->getName().".json");
                    
    $items json_decode(file_get_contents($file));
                    
    $inv $player->getInventory();
                    
    $inv->setContents([]);
                            foreach(
    $items as $k => $v){
                                
    $inv->setItem($k$v);
                            }
        }
       
        public function 
    PlayerQuitEvent(PlayerQuitEvent $event){
            
    $player $event->getPlayer();
            
    $inventory $player->getInventory();
            
    $items = [];
            foreach(
    $inventory->getContents() as $slot => $item){
            
    $items[$slot] = [$item->getId(), $item->getDamage(), $item->getCount()];
            
    $file = ($this->getDataFolder() . "players/".$player->getName().".json");
            
    file_put_contents($filejson_encode($items));
        }
       
       
       
        }
    }
    Basically when a player quits the serer I want to save his inventory in a .json file which works fine (PlayerQuitEvent).
    However when the player joins the player doesnt recieve his inventory i get the following error.

    Code:
    16:12:28 [CRITICAL] Could not pass event pocketmine\event\player\PlayerJoinEvent to DataSave v1.0.0: Argument 2 passed to pocketmine\inventory\PlayerInventory::setItem() must be an instance of pocketmine\item\Item, array given, called in C:\Users\User\Desktop\SteadFast Hybrid\PocketMine-MP\plugins\DataSave\src\DataSave\DataSave.php on line 67 and defined on DataSave\DataSave
    16:12:28 [NOTICE] InvalidArgumentException: "Argument 2 passed to pocketmine\inventory\PlayerInventory::setItem() must be an instance of pocketmine\item\Item, array given, called in C:\Users\Mahid\Desktop\SteadFast Hybrid\PocketMine-MP\plugins\DataSave\src\DataSave\DataSave.php on line 67 and defined" (E_RECOVERABLE_ERROR) in "/src/pocketmine/inventory/PlayerInventory" at line 199
    16:17:38 [INFO] FuryTacticz/192.168.1.95 logged out due to timeout
  2. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    file_get_contents doesnt return array isn´t?
  3. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    im not sure but the error is on line 67 which is:
    PHP:
    $inv->setItem($k$v);
  4. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    you can not save whole array into a file
  5. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    try this:
    PHP:
    public function PlayerJoinEvent(PlayerJoinEvent $event){
            
    $player $event->getPlayer();
            
    $ign $player->getName();
            
    $datafile = ($this->getDataFolder() . "players/".$ign.".json");
                if(!
    file_exists($datafile)){
                    
    $playerdata = new Config($this->getDataFolder()."players/".$player->getName().".json"Config::JSON);
                }
                
    $inv $player->getInventory();
                
    $inv->clearAll();
                    
    $data $playerdata->getAll();
                            foreach(
    $data as $slot => $item){
                                list(
    $id$dmg$count) = explode(":"$item);
                                
    $inv->setItem($id$dmg$count);
                            }
        }
       
        public function 
    PlayerQuitEvent(PlayerQuitEvent $event){
            
    $player $event->getPlayer();
            
    $inventory $player->getInventory();
            
    $items = [];
            
    $playerdata = new Config($this->getDataFolder()."players/".$player->getName().".json"Config::JSON);
            foreach(
    $inventory->getContents() as $slot => $item){
                
    $playerdata->set($slot"$item->id:$item->damage:$item->count");
            }
        }
  6. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    uhmm
    Code:
    [[17,0,64],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[276,0,1],[63,0,64]]
    It saved...
  7. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    $playerdata->set($slot, "$item->id:$item->damage:$item->count");
    crashed the server
  8. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
  9. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
  10. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
  11. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    I cant remember as im not at my pc atm but id was private or undefined or something like that...
  12. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    so you can edit my code :p
  13. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
  14. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
  15. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    @Creeperface here the crash
    PHP:
    Fatal errorCannot access protected property pocketmine\item\ItemBlock::$id in C:\Users\USER\Desktop\SteadFast Hybrid\PocketMine-MP\plugins\DataSave\src\DataSave\DataSave.php on line 77
    Line 77 is $playerdata->set($slot, "$item->id:$item->damage:$item->count");
  16. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    this is why i hate steadfast xD
    iiDeathStrokeii and Creeperface like this.
  17. Creeperface
    Offline

    Creeperface Notable Member Plugin Developer

    Joined:
    Nov 8, 2014
    Posts:
    1,346
    Plugins:
    4
    Minecraft User:
    CreeperFace
    you are right xD

    use $getDamage(), $getId() etc...
    Xenturio likes this.
  18. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    maybe $playerdata->set($slot, "$item->getId():$item->getDamage():$item->count");?
  19. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    PHP:
    14:06:36 [INFOFuryTacticzTeleported FuryTacticz to 100100100
    14
    :06:44 [INFOFuryTacticz has just earned the achievement Getting Wood
    14
    :06:47 [CRITICALCould not pass event pocketmine\event\player\PlayerQuitEvent to DataSave v1.0.0Undefined propertypocketmine\item\ItemBlock::$getId on DataSave\DataSave
    14
    :06:47 [NOTICEUndefinedPropertyException"Undefined property: pocketmine\item\ItemBlock::$getId(E_NOTICEin "/plugins/DataSave/src/DataSave/DataSave" at line 77
    14
    :06:47 [INFOFuryTacticz/192.168.1.95 logged out due to client disconnect
    14
    :06:52 [NOTICEPlayer data not found for "furytacticz"creating new profile
    14
    :06:52 [INFOFuryTacticz/192.168.1.95 connected
    14
    :06:56 [CRITICALCould not pass event pocketmine\event\player\PlayerJoinEvent to DataSave v1.0.0Undefined variableplayerdata on DataSave\DataSave
    14
    :06:56 [NOTICEUndefinedVariableException"Undefined variable: playerdata" (E_NOTICEin "/plugins/DataSave/src/DataSave/DataSave" at line 64
  20. iiDeathStrokeii
    Offline

    iiDeathStrokeii Active Member

    Joined:
    Mar 30, 2015
    Posts:
    111
    Minecraft User:
    iiDeathstrokeii
    il try this

Share This Page

Advertisement