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

Save data to config 1 time and get it

Comments in 'Plugin Development' started by JonsMC, Aug 14, 2016.

  1. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    hi, im trying to save data to config one time only and read it

    So far this is my code
    PHP:
    $x mt_rand(1,10);
                   
    $y mt_rand(2,4);
                   
    $z mt_rand(5,10);
                  
    //this apply for blank world so y can be from 2 to 4
              
    $this->playercfg = new Config($this->getDataFolder() . "/users" strtolower($sender->getName()), Config::YAML, array (
                                            
    "x" => $x,
                                            
    "y" => $y,
                                            
    "z" => $z
                                            
    ));
    But it's always override x, y, z everytime i type the command. Is there a way i can stop this?
  2. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    Maybe with empty
  3. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    What do you mean....?
  4. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    I mean i want to save the player data one time only at /users/<player_name>.yml , but my code keeps changing the x y z in config, i want to stop that
  5. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    Still don't fully comprehend, but have you tried anything like:
    PHP:
    if(!(file_exists($this->getDataFolder() . "/users/" strtolower($sender->getName()) . ".yml"))
    {

      
    /* code here */

    }
  6. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Yeah but if file is exits, how can i get the folder then?
  7. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    What do you mean?
  8. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    I mean after it check file_exits and return true, how can i get that file,
  9. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    PHP:
    $x mt_rand(110);

    $y mt_rand(24);

    $z mt_rand(510);

    $playerFile $this->getDataFolder() . "/users/" strtolower($sender->getName()) . ".yml";

    if(!(
    file_exists($playerFile)))
    {

      
    $this->cfg = new Config($playerFileConfig::YAML, array("x" => $x"y" => $y"z" => $z));

    }
    else
    {

      
    /* do whatever with $this->cfg */

    }
  10. JonsMC
    Offline

    JonsMC Active Member

    Joined:
    Jul 26, 2016
    Posts:
    107
    Minecraft User:
    JonsMC
    Thanks
  11. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    You're doing the opposite, you are getting the file when it does not exist...
    Survingo likes this.
  12. JackboyPlay
    Offline

    JackboyPlay Active Member

    Joined:
    Apr 25, 2015
    Posts:
    507
    Minecraft User:
    JackboyPlay
    if(empty($config->get("abc")){
    //input
    }
  13. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    The answers are getting worse and worse...
    Can you show if the data are immediately overridden? Files kept the same before but immediately changed? Any other accessors or saving? Just from this snippet it is correct.
    applqpak and PocketKiller like this.
  14. applqpak
    Offline

    applqpak Active Member Plugin Developer

    Joined:
    Dec 16, 2015
    Posts:
    284
    Plugins:
    1
    Minecraft User:
    applqpak
    What? If the file does not exist, then make it. Else do whatever with $this->cfg, E.G: get data.
  15. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    Config::__construct() accepts a default parameter, which will only override the file if the original file cannot be loaded.
    applqpak likes this.
  16. TutoGamerWalid
    Offline

    TutoGamerWalid Active Member

    Joined:
    Jun 11, 2015
    Posts:
    69
    Minecraft User:
    TutoGamerWalid
    PHP:
            $p $event->getPlayer();
            
    $name $p->getName();
            
    $cfg = new Config($this->getDataFolder() . "Folder/$name.yml"Config::YAML);
            if(!
    $cfg->exists($name)) {
             
    $x mt_rand(110);
             
    $y mt_rand(24);
             
    $z mt_rand(510);  
            
             
    $cfg->set("name",$name);
             
    $cfg->set("pos_x",$x);
             
    $cfg->set("pos_y",$y);
             
    $cfg->set("pos_z",$z);
             
    $cfg->save();
    }
    Ect..

Share This Page

Advertisement