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

Config array error

Discussion in 'Development' started by #A6543, Mar 9, 2017.

  1. #A6543

    #A6543 Zombie

    Messages:
    267
    Hey. I save player names in a config. But if I kick a player when a new one joins, the config saves wrong.

    onJoin:
    Add to players array in config

    OnQuit:
    Remove from players array

    Normally it saves it like this:
    players:
    - Steve
    - Steve2

    But if I kick a player when a new one joins it looks like this:
    players:
    1: Steve
    2: Steve2

    How can I fix that? It makes my plugin not work...
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    Can you share your code?
     
  3. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Perfect example.
     
    jasonwynn10, HimbeersaftLP and Muqsit like this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    It's easy to fix, just modify the config directly. It's not like you have to have PMMP to modify a config. A config can easily be modified by hand.
    Before:
    PHP:
    players:
    1Steve
    2
    Steve2
    After:
    PHP:
    players:
    Steve
    Steve2
    It's very simple and doesn't require any special talent or skill at all, (just like the question :3)
     
  5. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    I doubt that
    PHP:
    Example:
    $cfg->getAll(true);
    Fixes it, I went through this situation before

    - I might be wrong, correct me if I'm wrong
     
  6. #A6543

    #A6543 Zombie

    Messages:
    267
    I can't modify it in each game round with my hand
     
  7. #A6543

    #A6543 Zombie

    Messages:
    267
    Where should I use this???
     
  8. InspectorGadget

    InspectorGadget Zombie Pigman

    Messages:
    462
    GitHub:
    InspectorGadget
    Can we see your full code so we can help? We can't be providing help as we don't know what you've done on your Main file..

    You're not gonna lose anything if you share...
     
    jasonwynn10 likes this.
  9. #A6543

    #A6543 Zombie

    Messages:
    267
    OnJoin:
    PHP:
    $players $this->players;
            
    array_push($players$name);
            
    $this->players $players;
            
    $conf->set("players"$players);
            
    $conf->save();
    OnQuit:
    PHP:
     $players $this->players;
            if(
    in_array($player->getName(), $conf->get("players"))) {
          
            unset(
    $players[array_search($player->getName(), $players)]);
          
            
    $this->players $players;
            
    $conf->set("players"$players);
            
    $conf->save();
            }
     
  10. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    Wait what? $players = $this->players and $this->players = $players? Please tell us what $this->players is. Is it an array?
     
  11. #A6543

    #A6543 Zombie

    Messages:
    267
    Yes.

    And it works! But if I kick a player, when another joins, it destroys the config
     
  12. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
  13. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Your code is overcomplicated. Try this instead.
    PHP:

    private $players = [];
    public function 
    onEnable(){
        
    $config yaml_parse_file($this->getDataFolder()."config.yml"); //Converts config to array.
        
    $this->players $config["players"] ?? []; //If "players" exists in config, $this->players will be assigned that, or else a new array will be assigned to $this->players.
    }

    public function 
    onJoin(PlayerJoinEvent $event){
        
    $player $event->getPlayer();
        
    $this->players[$player->getId()] = $player->getName(); //In your post, you were trying to save Player::class in the config. This probably will give you some YAML parsing error. You should have serialized it instead.
        
    $this->saveConfig();
    }

    public function 
    onQuit(PlayerQuitEvent $event){
        
    $id $event->getPlayer()->getId();
        unset(
    $this->players[$id]);
        
    $this->saveConfig();
    }

    public function 
    saveConfig(){
        
    $config yaml_parse_file($dir $this->getDataFolder()."config.yml");
        
    $config["players"] = $this->players//set "players" in config to the modified players global array.
        
    yaml_emit_file($dir$config); //save config.
    }
    But what are you getting from this? The key "players" in the config is emptied when the server stops.
     
    corytortoise likes this.
  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.