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

Problem with config

Discussion in 'Development' started by Kyd, Mar 11, 2017.

  1. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Look at my code I want add player name to config when join arena, it do not add anything....
    PHP:
    public function getPlayers($arena){
        
    $config = new Config($this->getDataFolder()."Arenas/".$arena.".yml"Config::YAML);

        
    $playersXXX $config->get("Players");

        
    $players = array();

        foreach (
    $playersXXX as $x){
            if(
    $x != "steve steve"){
                
    $players[] = $x;
            }
        }

        return 
    $players;


    public function 
    addPlayerToArena($arena$name){

        
    $config = new Config($this->getDataFolder()."Arenas/".$arena.".yml"Config::YAML);

        
    $players $this->getPlayers($arena);

        
    $players[] = $name;

        
    $config->set("Players"$players);
        
    $config->save();
    }


    }
    When player join i use addPlayerToArena function
     
  2. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    I can't find an error here, allthough creating configs on each call is much overhead. Are you sure you call addPlayerToArena?
     
    hoyinm14mc likes this.
  3. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    Yes I adding player to arena.. When i change for example $config->set("Players", $players); to $config->set("Players", "test");
    It is working
     
  4. hoyinm14mc

    hoyinm14mc Silverfish

    Messages:
    22
    Try define the default value of the YAML file an array.
    Then when you want to access the config, use the following code:
    PHP:
    $v $this->config->getAll();
    foreach(
    $players as $name){
        
    $v["players"][] = $name;
    }
    $this->config->setAll($v);
    $this->config->save();
    ($v could be anything e.g. $c or $tmp or $cfg, just define the variable yourself)
     
    Last edited: Mar 12, 2017
  5. hoyinm14mc

    hoyinm14mc Silverfish

    Messages:
    22
    In my Jail plugin, I mainly use this method to store players' data so you could look into it for reference.
    https://github.com/hoyinm14mc/Jail/
     
    Last edited: Mar 12, 2017
  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.