Wrong with Config array?

Discussion in 'Development' started by Hoyee, Mar 24, 2020.

  1. Hoyee

    Hoyee Slime

    Messages:
    89
    Code:
    public function onJoin(PlayerJoinEvent $e){
    
        $el = $e->getPlayer()->getName();
    
        $this->conf["Players"][] = "$el";
    
        $this->conf->setAll($this->con);
        $this->conf->save();
    
    }
    it says

    Code:
    "Cannot use object of type pocketmine\utils\Config as array"
    I just want to make if player join server, save join player's name in conf.yml
     
  2. Emirhan Akpınar

    Emirhan Akpınar Witch

    Messages:
    71
    GitHub:
    EmirhanWSD
    PHP:
    public function onJoin(PlayerJoinEvent $e){
        
    $el $e->getPlayer()->getName();
        
    $players $this->conf->get("Players");
        if (!
    in_array($el$players)) {
             
    $players[] = $el;
             
    $this->conf->set("Players"$players);
             
    $this->conf->save();
        }
    }
     
  3. Hoyee

    Hoyee Slime

    Messages:
    89
    but it says "in_array() expects parameter 2 to be array, bool given"
     
  4. Emirhan Akpınar

    Emirhan Akpınar Witch

    Messages:
    71
    GitHub:
    EmirhanWSD
    You didn't set "Players" on config.

    PHP:
    public function onJoin(PlayerJoinEvent $e){
        
    $el $e->getPlayer()->getName();
        
    $players $this->conf->get("Players");
        if (!
    $players) {
             
    $this->conf->set("Players", []);
             
    $this->conf->save();
        }
        if (!
    in_array($el$players)) {
             
    $players[] = $el;
             
    $this->conf->set("Players"$players);
             
    $this->conf->save();
        }
    }
     
  5. Hoyee

    Hoyee Slime

    Messages:
    89
    I added
    Code:
    @mkdir ($this->getDataFolder());
    
    $this->conf = new Config ($this->getDataFolder()."conf.yml",Config::YAML,[
                        "Players"
                ]);
                $this->con = $this->conf->getAll();
    but still same
     
  6. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    439
    GitHub:
    GamakCZ
    PHP:
    public function onJoin(PlayerJoinEvent $e){

        
    $el $e->getPlayer()->getName();

        
    $c $this->conf->getAll();
        
    $c["Players"][] = "$el";
        
    $this->conf->setAll($c);

        
    $this->conf->setAll($this->con); // this will overrid your attempt to save player's name... ???
        
    $this->conf->save();

    }
     

Share This Page

  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.