Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Write player's name into yml file

Comments in 'Plugin Development' started by myst02, Jul 13, 2015.

  1. myst02
    Offline

    myst02 Active Member

    Joined:
    Mar 23, 2015
    Posts:
    97
    Minecraft User:
    myst02
    Hi,

    how can I write player name into a file called "bad-players.yml"?
  2. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    252
    Minecraft User:
    RekkuzaRage
    PHP:
    $cfg = new Config($this->getDataFolder() . "bad-players.yml"Config::YAML);
    $cfg->set($player->getName());
    $cfg->save();
    # $player would have to represent the player like a player instance. Also the getName method would get their name. 
    myst02 likes this.
  3. myst02
    Offline

    myst02 Active Member

    Joined:
    Mar 23, 2015
    Posts:
    97
    Minecraft User:
    myst02
    OK, thanks. Does this also work with multiple player names?
  4. luca28pet
    Offline

    luca28pet Active Member

    Joined:
    Feb 20, 2014
    Posts:
    251
    Minecraft User:
    luca28pet
    If you want to save only the player names, I suggest you to use an ENUM Config (.txt instead of .yml):
    PHP:
    public function onEnable(){
      @
    mkdir($this->getDataFolder());
      
    $this->badPlayers = new Config($this->getDataFolder()."bad-players.txt"Config::ENUM);
    }
    Then to add/remove/check names in the config:
    PHP:
    $this->badPlayers->set($string); //to add a string (player name)
    $this->badPlayers->remove($string); //to remove a string (player name)
    if($this->badPlayers->exists($string)){
      
    //$string is in the config, he's a bad player
    }else{
      
    //$string is not in the config, he isn't a bad player
    }
    Remember to $this->badPlayers->save() in onDisable()
    Last edited: Jul 13, 2015
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    DunxandMinecraft likes this.
  6. Wuros
    Offline

    Wuros New Member

    Joined:
    Sep 12, 2014
    Posts:
    8
    Minecraft User:
    Wuros
    You can use array's for a lot of player names

    PHP:
    if(!is_dir($this->getDataFolder())){
    @
    mkdir($this->getDataFolder());
    }
    $config= new Config($this->getDataFolder()."players.yml"Config::YAML, array("players" => array()));
    And check if players exists
    PHP:
    $config= new Config($this->getDataFolder()."players.yml"Config::YAML);
    $players $config->get("players");
    if(
    in_array($playername$players)){
    // Player exists
    }else{
    //Player don't exists
    }
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Save $config. You are wasting CPU by loading the file again and again.

    And enum is better
  8. Wuros
    Offline

    Wuros New Member

    Joined:
    Sep 12, 2014
    Posts:
    8
    Minecraft User:
    Wuros
    No, config is better:):cool:
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Enum is a config. And if you use YAML, it simply makes the server more laggy.

    The best way is still to append to the file directly.
    Legoboy0215 likes this.
  10. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Can I do this?
    PHP:
    $string = array(TomSamLol);
    $this->badPlayers->set($string);
  11. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    1. String literals should have "quotes".
    2. No. Since you are doing it anyway, why not write to file directly?
    PHP:
    file_put_contents($fileimplode(" "$names));
  12. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    No, my "" button is glitchy.
    I never knew how to use file_put_contents. Thanks! So $name is the array right?
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Yes. And it is plural since it has multiple names :p
    Legoboy0215 likes this.
  14. xiaoq
    Offline

    xiaoq Active Member

    Joined:
    Dec 23, 2014
    Posts:
    232
    Minecraft User:
    xiaoq
  15. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,747
    Minecraft User:
    Legoboy0215
    Enum is a kind of .txt config.
    xiaoq likes this.
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Enum itself, is similar to a List in Java. It is a list of values of the same type. In this case, it is a plain list of strings. I emphasize plain because it has to be an array without keys.
    jojoe77777 likes this.

Share This Page

Advertisement