Hi guys. I dont understand this error. Anyone help me? PHP: public function getArenaPlayer(string $arenaName){ $arenaConfig = new Config($this->getDataFolder()."Arenas/$arenaName.yml", Config::YAML); $arenaPlayers = $arenaConfig->get("Players"); $players = array(); foreach ($arenaPlayers as $player) { $newPlayer = Server::getInstance()->getPlayer($player); if($newPlayer instanceof Player){ $players[] = $player; }else{ $this->removeArenaPlayer($arenaName, $player, 1); } } return $players;}
Send me the config file. I am pretty sure you cannot foreach that. You could use arrays, but we are trying to fix an error here, not to prevent it by trying something else that may be better.
--- Status: Lobby StartTime: 61 EndTime: 16 Team: 2 PlayersPerTeam: 1 Players: [] BLUE: X: -329 "Y": 21 Z: -347 RED: X: -307 "Y": 17 Z: -343 Lobby: X: -315 "Y": 35 Z: -354 Yaw: 179.332642 Pitch: 2.059753 World: ew World: ew ...
PHP: public function getArenaPlayer(string $arenaName){ $arenaConfig = new Config($this->getDataFolder()."Arenas/$arenaName.yml", Config::YAML); $arenaPlayers = $arenaConfig->get("Players"); foreach ($arenaPlayers as $player => $fix) { $newPlayer = Server::getInstance()->getPlayer($player); if($newPlayer instanceof Player){ $arenaPlayers[] = $player; $arenaConfig->set("Players",$arenaPlayers); }else{ $this->removeArenaPlayer($arenaName, $player, 1); } } return $arenaPlayers;}
You don't have to have "=> $value" in a foreach loop. Since OP hasn't specified the array in question being associative, it's unnecessary. Since the error says invalid argument supplied, it implies the input is the problem, not the output. It would be surprising for me to be right twice in one thread, but I believe [] is proper array syntax for Yaml files, according to this source(May have to scroll to find it). You are right that the array needs to have an element to pass correctly. The solution depends on how OP wants the plugin to function. Are you running this function before or after you add player names to the config? If it's after, send the code where you do that. If not, you need to either only run this code when the Arena has players in it, or use is_array to check before, or just cast the data using PHP: foreach((array) $arenaPlayers as $player) {
actually no class::method just meant as a way to tell other the class and the method you are never ever suppose to copy and paste that then use it as if it's code while yes it's also a way to invoke a STATIC method in this context we are just pointing out the class+method
I almost thought we didn't need this. https://github.com/SOF3/forums-common-sense/wiki/Code-Environment#code-context
Besides the above comments, I'm still wondering why you are saving players names to a config file at all. Is the list of players in an arena supposed to survive a server restart?