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

Add Player Name to Yml

Discussion in 'Facepalm' started by freakingdev, Apr 23, 2017.

  1. freakingdev

    freakingdev Spider Jockey

    Messages:
    33
    GitHub:
    imjayl
    Please give me a tip when typing command /add (playerName)
    It will add to player.yml
     
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You can use a Config class and the Config::set() method or yaml_emit_file, whatever works for you.
     
  3. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    You may find this thread I created earlier useful.

    After you have a Config instance you can call Config::get($key, $default), Config::set($key, $data) and Config::exists($key) to manage the data.

    A few examples (building off the thread I linked to):
    PHP:
    /** @var pocketmine\utils\Config */
    public $myConfig;

    // Assuming we're working from the main and class and it is a registered listener
    public function onJoin(pocketmine\event\player\PlayerJoinEvent $event) {
        
    $player $event->getPlayer();
        
    $name $player->getName();
        if(
    $this->myConfig->exists($name)) {
            
    $player->sendMessage("Welcome back $name!"); // Player has played before and is saved to the config
        
    } else {
            
    $player->sendMessage("Welcome to this server $name!"); // Player hasn't played before and isn't saved to the config
            
    $this->myConfig->set($nametrue);
            
    $this->myConfig->save(true); // Save the config file asynchronously
        
    }
    }
     
    Last edited: Apr 26, 2017
  4. freakingdev

    freakingdev Spider Jockey

    Messages:
    33
    GitHub:
    imjayl
    Thanks a lot guys! But i need tips in adding playername using command /add (name) and save it to config.yml
     
  5. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    You have already been given an example for how to store it in a config.yml. What else do you need? If you need help with handling commands, take a look at this.
     
  6. freakingdev

    freakingdev Spider Jockey

    Messages:
    33
    GitHub:
    imjayl
    Done! Thanks guys.
     
  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.