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

[New API]Creating Configs?

Comments in 'Plugin Development' started by wviper3, Jul 14, 2014.

Thread Status:
Not open for further replies.
  1. wviper3
    Offline

    wviper3 New Member

    Joined:
    Jul 8, 2014
    Posts:
    8
    Minecraft User:
    wviper3
    I recently set up a plugin for Pocketmine 1.4beta and I want to be able to save user data in a file and read and update that data while that are playing. I am new to this API and only recently learned the Old one, so if I ask dumb questions that is why. I have been using this:

    Code:
    new Config("saves.yml", Config::YAML, $playerName = [
                 $money = 1500,
             ]);
    
    and
    Code:
    new Config("saves.yml", Config::YAML, $playerName = [
                 $money = 1500,
             ], true);
    
    I have tried too many things to list and I have read the docs, but nothing is working. I have used this to include the config file:
    Code:
    use pocketmine\utils\Config;
    
    I assume it is like importing in java : import java.util.Config;

    Any help is appreciated.
    Thanks,
    wviper3
  2. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    371
    Plugins:
    3
    Minecraft User:
    Dutok
    What error does it give?
  3. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    did you put it in a veriable ?
  4. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    use this one $this->config = new Config($this->getDataFolder()."config.yml", Config::YAML, array(
    $money = 1500
    ));
  5. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    and if you want change some stuff in it do $this->config->set("something", "or another"); this well be in the config like something: or another and if you want want get what is "something" do $this->config->set("something); and it well return "or another" hopefully that helpped
  6. wviper3
    Offline

    wviper3 New Member

    Joined:
    Jul 8, 2014
    Posts:
    8
    Minecraft User:
    wviper3
    Does it work like this?
    Code:
    $this->config->set($varToSet, $newValue);
    
    Also, thank you for the config code. I want to have users data saved, so how can I call the array and use it? I have seen people do it like this for commands in the old API:
    Code:
    args[0] as $money
    
    Or something like that.
    Thanks
  7. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    if you want the players data saved do like
    $this->config->set($args[0], $args[1]); where $args[0] is the player's name and args[1] is the amount of money
  8. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    371
    Plugins:
    3
    Minecraft User:
    Dutok
    If you did it you way, you would make a new saves.yml every time that bit of code is called. You should probably do something like:
    PHP:
    public function onEnable(){
     @
    mkdir($this->getDataFolder());
     
    $this->configFile = new Config($this->getDataFolder()."saves.yml"Config::YAML, array());
    }
    Then to set their money:
    PHP:
    $this->configFile->set($sender->getName(), $sender->getMoney());
    $this->configFile->save();
    I hope that helped. :D
  9. wviper3
    Offline

    wviper3 New Member

    Joined:
    Jul 8, 2014
    Posts:
    8
    Minecraft User:
    wviper3
    Thanks! This will all work great. I do not understand the set method, but I will look at the docs now that I know how to use it. You all helped a lot! Thanks again!
    Dutok likes this.
  10. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    use @Dutok code because i forgot that it well generate a folder every time the server start :D if you need help just ask me(or anyone else) :)
  11. swagboy47
    Offline

    swagboy47 Active Member

    Joined:
    Jun 10, 2014
    Posts:
    445
    Minecraft User:
    swagking
    if you need help with your plugin make a github account and post it there and i well come and help you
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Not everyone wants to use $args[0]. Stop giving irrelevant variable names.
    Killman2 and Dutok like this.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It doesn't matter. It still writes the file, just that it is garbaged instantly.
    Dutok likes this.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Don't use the default include path. It will generate files at the root directory, which is improper for a plugin.
    Dutok likes this.
  15. wviper3
    Offline

    wviper3 New Member

    Joined:
    Jul 8, 2014
    Posts:
    8
    Minecraft User:
    wviper3
    How Do I access the array in the config? This is what I have:
    Code:
    $this->saves = new Config($this->getDataFolder()."saves.yml", Config::YAML, $this->saves->players = array(
                "wviper3" => array(
                        "money" => 1500,
                        "space" => 0
                )
             ));
    
    $this->saves->set($this->saves->players[0][1], $this->saves->players[0][1] + $rand01 + $rand02);
    $this->saves->save();
    
  16. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    371
    Plugins:
    3
    Minecraft User:
    Dutok
    Something like this:
    PHP:
    $this->saves->get("wviper3");
    or
    PHP:
    $this->saves->getAll("wviper3");
    I don't actually know if this would work because I'm too lazy to look it up. Instead, why don't you look it up yourself?
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    GetAll will return the whole array inputted.

    Also, why did you define $this->saves->players?
    Dutok likes this.
  18. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    371
    Plugins:
    3
    Minecraft User:
    Dutok
    He's doing his config file in a very backward and confusing way. He should do it the way I posted earlier.
  19. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Which way?
  20. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    371
    Plugins:
    3
    Minecraft User:
    Dutok
    I_Is_Payton_ likes this.
Thread Status:
Not open for further replies.

Share This Page

Advertisement