ive recently started learning how to use configs, if i set everything in the config before, everything works it gets players level, xp, xp needed for next level etc, but i want to make it so every time a new player joins the server it stores default data for them. how can i do this?
Hello, this could be done using a listener. In the code I'm going to be providing below, please define any $config variable with a config file. PHP: public function onLogin(PlayerLoginEvent $event) {$player = $event->getPlayer();if ($config->exists($player->getName()) return;$config->set($player->getName(), ["kills" => 0]):$config->save();}public function onQuit(PlayerQuitEvent $event) {$player = $event->getPlayer();$config->set($player->getName(), $newData);$config->save();} in the code above i would reccommend only accessing the config file on join and quit. when they join get their data using $config->get($player->getName()); and maybe store it in a array somewhere. sessions are also a very nice option in the event you will be using config files. Here is the sessions folder of a old core i made that uses config storage. It is pretty fast i would say https://github.com/TacoNoBurrito/ZPrisons/tree/main/src/Taco/ZP/session if you need any help i would be happy to help here, or on discord: FakeTaco#4420
I would recommend not to use Config objects for just serializing and persisting data. PHP: writeData(string $file, array $data): void { file_put_contents($file, json_encode($data));}readData(string $file, array $data): array { return file_exists($file) ? json_decode(file_get_contents($file), true) : []}