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; }
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
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));
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!
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; }