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

Config Arrays

Discussion in 'Development' started by ShadowMCX, Oct 19, 2020.

  1. ShadowMCX

    ShadowMCX Creeper

    Messages:
    3
    Im trying to make a function that will add a "rank" to a player like the following

    Code:
    ---
    shadowmcx:
      ranks:
        - "Inmate"
        - "Admin"
    ...
    Currently im only able to have one rank like shown

    Code:
    ---
    shadowmcx:
      rank: Inmate
    ...
    But I wanna have multiple ranks per player, below is the working code I use to set a players rank, but I dont know how to do multiple ranks per player

    PHP:
    public function setRank($player$rank) {               
                    
                if(
    $player instanceof Player){
                        
                        
    $this->players->setNested(strtolower($player->getName()) . ".rank"$this->players->getAll() [strtolower($player->getName())]["rank"] = $rank);
                        
                        
                      
                    } else {
                        
                        
    $this->players->setNested(strtolower($player) . ".rank"$this->players->getAll() [strtolower($player)]["rank"] = $rank);
                        
                        
                        
                    }
                    
    $this->players->save();
                    return 
    true;
                }
     
  2. Primus

    Primus Zombie Pigman

    Messages:
    749
    First of all, get the current value
    PHP:
    // This could go into separate method (getRanks?)
    $name trim(strtolower($player instanceof Player $player->getName() : $player));
    $ranks $this->players->getNested("player.$name.rank") ?? [];
    Add the new rank to previous value (might need array_unique)
    PHP:
    $this->players->setNested("player.$name.rank"array_merge($ranks$rank));
    The naming doesn't make much sense now, I'd prefer addRank : void
     
  3. ShadowMCX

    ShadowMCX Creeper

    Messages:
    3
    Im getting this error

    Code:
    [Server thread/CRITICAL]: ParseError: "syntax error, unexpected '$player' (T_VARIABLE)" (EXCEPTION) in "plugins/ShadowRanks/src/ShadowMC/ShadowRanks/Loader" at line 87
    LINE 87
    PHP:
    $name trim(strtolower($player instanceof Player $player->getName() : $player));
     
  4. Primus

    Primus Zombie Pigman

    Messages:
    749
    This looks hopeless, however could you show me the whole code and context of that line?

    Just copying and pasting stuff will not resolve your issue!
     
  5. ShadowMCX

    ShadowMCX Creeper

    Messages:
    3
    PHP:
    public function setRank($player$rank) {              
                   
                
    $name trim(strtolower($player instanceof Player $player->getName() : $player));
               
    $ranks $this->players->getNested("$name.rank") ?? [];
    $this->players->setNested("$name.rank"array_merge($ranks$rank));
                    
    $this->players->save();
                    return 
    true;
                }
                
     
  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.