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

Wrong with Config array?

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

  1. Hoyee

    Hoyee Baby Zombie

    Messages:
    126
    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 Slime

    Messages:
    90
    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 Baby Zombie

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

    Emirhan Akpınar Slime

    Messages:
    90
    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 Baby Zombie

    Messages:
    126
    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:
    598
    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();

    }
     
  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.