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

How to save data?

Comments in 'Plugin Development' started by abler98, Nov 18, 2014.

  1. abler98
    Offline

    abler98 New Member

    Joined:
    Nov 15, 2014
    Posts:
    26
    Minecraft User:
    abler98
    How to save data in files or database? I'm new to this, it is desirable to explain in detail
  2. GlaciercreepsMC
    Offline

    GlaciercreepsMC Active Member Plugin Developer

    Joined:
    Jan 21, 2014
    Posts:
    91
    Plugins:
    1
    Minecraft User:
    GlaciercreepsMC
    You should check out the pocketmine\utils\Config class here, and also here's another post similar to what you're asking (configs). I'll try to make a basic tutorial soon in the forums -> plugin development -> resources category.

    EDIT: The tutorial got removed, for small issues.
    Last edited: Nov 19, 2014
    abler98 likes this.
  3. Wantedkillerss
    Offline

    Wantedkillerss Active Member Plugin Developer

    Joined:
    May 9, 2014
    Posts:
    142
    Plugins:
    1
    Minecraft User:
    Wantedkillers
    $this->config->save(); always worked me or if u named your config
    $this->test->save();
    abler98 likes this.
  4. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    When working with data serialization you have a number of options, so I decided to write them up.

    Formats
    • The good old YAML, is a "should" for files which are edited by a user and not just the plugin. If you just want to store data for the plugin to use, it might not be the best option.
      • Available though Config class (Config::YAML)
      • Available using yaml_emit($data) and yaml_parse($str)
    • JSON is also possible through the Config class, JSON is much more structured although it doesn't work nearly as well as with JS. JSON is a good idea for things like user files.
      • Available though Config class (Config::JSON)
      • Available using json_encode($data) and json_decode($str, true)
    • Serialization, is a smart option if you know what you are doing but could be really easy to mess up. Serialize allows you to save any object (not just primitives) as a data representation.
      • Available though Config class (Config::SERIALIZED)
      • Available using serialize($data) and unserialize($str)
    • A properties or enum, these are simpler configs which are limited in what they can do, it probably would be wise to avoid these. The properties type is used for the server.properties ("key=value" format) it can't contain arrays more than one level deep and it changes "on", "true" and "yes" into true and "off", "false" and "no" into false. Enum just lists stuff, so you could use it to make a list of players if you didn't need to store anything about them individually. You will often hear that these formats are faster, this likely isn't true as the JSON, YAML and Serialize are written in C, whereas the properties and enum are done in PHP.
      • Available though Config class (Config::CNF and Config::ENUM)
    • An SQL database, I wouldn't suggest doing this unless you know SQL well because getting a database working is an annoyance but having to learn SQL at the same time is much worse. SQL is awesome though if you are planning on making a complex statistics system or something like that, this would be the thing chose.
      • Available using SQLite3 class
      • Available using mysqli.
    • A NoSQL database, the PHP bundled with PocketMine doesn't support any NoSQL databases. If you wanted to use a DB like Mongo you would have to install the extension yourself and your plugin wouldn't be accepted on the forums also everytime you updated the bin you wouldn't be able to just replace it. If you want to use something like http://orchestrate.io/ or https://www.firebase.com/, that is possible but your plugin likely wouldn't be approved if you posted it here.
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    That's exactly how I learnt SQL :p
    Falk likes this.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I also made a Table class in StatsCore. If your plugin has StatsCore dependency, you can use Table to store user-editing-friendly two-dimensional data.
    Falk likes this.
  7. abler98
    Offline

    abler98 New Member

    Joined:
    Nov 15, 2014
    Posts:
    26
    Minecraft User:
    abler98
    Thank you all
  8. abler98
    Offline

    abler98 New Member

    Joined:
    Nov 15, 2014
    Posts:
    26
    Minecraft User:
    abler98
    Most likely will be using SQL
  9. abler98
    Offline

    abler98 New Member

    Joined:
    Nov 15, 2014
    Posts:
    26
    Minecraft User:
    abler98
    How to start using sql in my plugin?
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    First, learn the SQL language.I recommend you using SQLite3 at first as it is easier to learn and test. You can search for SQLite3 documentation on Google.
    Then, start using SQLite3 in your script. example:
    PHP:
    $db = new \SQLite3($this->getDataFolder() . "db.sq3");
    $db->exec("CREATE TABLE IF NOT EXISTS foo (id INT PRIMARY KEY, bar TEXT);");
    $db->exec("INSERT INTO foo (id, bar) VALUES (1, 'abc');");
    $result $db->query("SELECT * FROM foo WHERE id = 1;");
    var_export($result->fetchArray(SQLITE3_ASSOC));
    var_export($result->fetchArray(SQLITE3_ASSOC));
    Output should be:
    Code:
    array(
      "id" => 1,
      "bar" => "abc"
    )
    false
    abler98 and iJoshuaHD like this.
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    so thats how it works xD i only know how mysqli works :p
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Note that exec() is expected to return void (nothing, maybe identical to null in PHP) while query() is expected to return an \SQLite3Result object. So you must use query() for SELECT. Also, you can use
    PHP:
    $stmt $db->prepare($query);
    to prepare a statement, then
    PHP:
    $stmt->bindValue(":arg"$arg);
    to bind replace all occurrences of :arg with the actual value of $arg :)arg must start with a colon, and $arg must be of a primitive type, i.e. number, string, etc. (Well, really nothing else))
    and then
    PHP:
    $result $stmt->execute();
    to execute the query. This function also returns SQLite3Result.
  13. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    In addition to @Falk's examples, I also recommend NBT and custom databases.
    NBT
    Create a \pocketmine\nbt\tag\Compound object and add values into it. Then instantiate a new \pocketmine\nbt\NBT, then $nbt->setData($compound) and file_put_contents($file, $nbt->write()) to save. To read, instantiate another NBT object and $nbt->read(file_get_contents($file)) to read, and $nbt->getData() to get the compound.

    Custom databases
    An old fashioned simple way of storing data, instead of using YAML, is to write the number directly. For example, you want to store strings $kills, $deaths and $kit for a player, so you do file_put_contents($dir . "$player.txt", implode("|", array($kills, $deaths, $kit))) to save for that player, and then list($kills, $deaths, $kit) = explode("|", file_get_contents($dir . "$player.txt")) to read data. If your data are not strings, convert them into strings. This is faster than using JSON or YAML if you only store simple data, or if you want to construct objects from these data.
    You can also use a custom binary database for all players. I recommend using this method if you want to load and save data on enable and disable. Look at PocketFactions for example.
    Falk likes this.
  14. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,196
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    idk but for me, the best way to store player statistics is using mysqli if you aim to make the datas available in a website.
    PEMapModder likes this.
  15. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    But for PocketFactions, I put it in a database because it is not that convenient to construct objects from a database, and I can just start a TCP server for queries on factions info.
    iJoshuaHD likes this.
  16. LDX
    Offline

    LDX Notable Member Plugin Developer

    Joined:
    Oct 2, 2013
    Posts:
    1,429
    Plugins:
    14
    That's what I used to do!!
    PEMapModder likes this.
  17. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    I invented and publicized it on the same day with you :p
    Last edited: Nov 19, 2014
    LDX likes this.

Share This Page

Advertisement