I have a array on my yaml... Code: array: - blabla1 - blabla2 - blabla3 How I can add a value to it? I want add blabla4. EX: Code: array: - blabla1 - blabla2 - blabla3 - blabla4
Try it: PHP: $array = $this->getConfig()->get('array');$new_array = $array;$new_array[] = 'blabla4';$this->getConfig()->set('array', $new_array);$this->getConfig()->save();
PHP: $array = $this->getConfig()->get('array'); // values: blabla1, blabla2, blabla3, blabla4$for_remove = 'blabla2';$new_array = [];foreach($array as $value) { if($value != $for_remove) { $new_array[] = $value; }}$this->getConfig()->set('array', $new_array);$this->getConfig()->save();
PHP: $new = "blabla2";$array = $this->getConfig()->get("array");for ($i = 0; $i <= count($array); $i++) {if ($array[$i] === $new) { unset($array[$i]);}}sort($array);$this->getConfig()->setNested("array", $array);$this->getConfig()->save();
It works! But I have a problem... I do a command, and it works the first time... But the second time it removes the first time value and add the new value. I mean: /aw add pedro And now the yaml shows: Code: ex: - ex - ex - pedro But if execute it another time (this time /aw add dro), the yaml shows: Code: ex: - ex - ex - dro
RemovePlayer not working. AddPlayer working bad (I said you the problem :'] ) PHP: private function addPlayer($playerName){ $newPlayer = $this->allowJoinList; $newPlayer[] = $playerName; $this->getConfig()->set("AllowJoin", $newPlayer); $this->getConfig()->save(); } private function removePlayer($playerName){ for ($i = 0; $i <= count($this->allowJoinList); $i++) { if ($this->allowJoinList[$i] === $playerName) { unset($this->allowJoinList[$i]); } }sort($this->allowJoinList); $this->getConfig()->setNested("AllowJoin",$this->allowJoinList); $this->getConfig()->save(); } Commands: PHP: # Commands public function OnCommand(CommandSender $sender, Command $command, $label, array $args){ if($command->getName() == "aw" and $sender->hasPermission("commands.alwaysjoin")){ switch(strtolower($args[0])){ case "add": if (isset($args[1])) { $this->addPlayer($args[1]); $sender->sendMessage("Added " . $args[1] . " to AlwaysJoin :)"); } else { $sender->sendMessage("Usage: /aw <add/remove> [player]"); } break; case "remove": if(isset($args[1])) { $this->removePlayer($args[1]); } else{ $sender->sendMessage("Usage: /aw <add/remove> [player]"); } break; default: $sender->sendMessage("Usage: /aw <add/remove> [player]"); break; } } }}
for remove try my code: PHP: $array = $this->getConfig()->get('array'); // values: blabla1, blabla2, blabla3, blabla4$for_remove = 'blabla2';$new_array = [];foreach($array as $value) { if($value != $for_remove) { $new_array[] = $value; }}$this->getConfig()->set('array', $new_array);$this->getConfig()->save(); and why addPlayer working bad? More info, please.
When I run the command two times, the first time it works fine, but the second it only delete the first line...