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

setting player data in config

Discussion in 'Development' started by Brant, Dec 18, 2021.

  1. Brant

    Brant Baby Zombie

    Messages:
    104
    GitHub:
    tetroyt
    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?
     
  2. Taylor R

    Taylor R Creeper

    Messages:
    1
    GitHub:
    tacoerror
    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
     
  3. Primus

    Primus Zombie Pigman

    Messages:
    749
    I would recommend not to use Config objects for just serializing and persisting data.

    PHP:
    writeData(string $file, array $data): void {
       
    file_put_contents($filejson_encode($data));
    }

    readData(string $file, array $data): array {
       return 
    file_exists($file
          ? 
    json_decode(file_get_contents($file), true)
          : []
    }
     
    minijaham likes this.
  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.