Saving object's fields. [Please help its been days!]

Discussion in 'Plugin Development' started by Amazing_kid, Oct 5, 2013.

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

    Amazing_kid

    Hi,

    I am making a largish plugin and am wondering how plugins such as factions and towny go about storying their data. I have looked at the code but do not fully understand how it saves such data to a file. They seem to save it to the files on shutdown/reload. But what I am looking at in particular is the most efficient way to save object's fields to a data folder so I have the data on startup.(Also how to get the data on startup). An example being like saving a towns names, balance, mayor etc. Thanks
     
  2. Offline

    CubieX

    There is no "most efficient way".
    It highly depends on your application.

    Your options are to save it in a YAML file (recommended for smaller sets of data or data that does not change frequently)
    or to save it into a database. (recommended for bigger sets of data and data that may change frequently)

    The basic approach is always the same.
    The data gets saved to disk as often as needed. Means, it may be saved each time data has changed, so no loss would ocurr if the server crashes.
    Or it may be saved in regular intervals or only on shutdown.
    You have to decide how and when you save your data to disk depending on how bad it would be to loose data of the last seconds or minutes.
    When using a database, writing changes immediately to DB is a common practice for most plugins.
    More complex ones may cache changed data in RAM for some time, for performance reasons.

    But how an when you should do this really depends on your plugin.
    You have to consider how much data you expect and how frequently it will change.
    If you can describe this, we can help you with your question more specific.
     
  3. Offline

    Amazing_kid

    Yes I will require it to read/write player data along with clan and nation data. So similar to a town within a towny nation. So I'm thinking it would be best to have a schedular save every so often, along with on shutdown. But what im wondering is how factions and towny get/write their info. When you look at the code, I don't see where they get the stored data from. https://github.com/MassiveCraft/Fac...com/massivecraft/factions/entity/Faction.java or https://github.com/ElgarL/Towny/blob/master/src/com/palmergames/bukkit/towny/object/Town.java

    Thanks
     
  4. Offline

    CubieX

    Factions seems to use a set of text files for saving which are accessed via JSON.
    It's an alternative for the Bukkit config file API.

    Towny uses a database. (supports H2, mySQL and SQLite)

    Personally I would use a database for this and save values immediately upon change.
    For reading (which will happen very frequently here) it might be a good idea to cache data in HashMaps for example.
     
  5. Offline

    Amazing_kid

    So something like this?
    Code:
    private double balance = //value from database using id
    private int id;
     
    public clan(int id){
        this.id = id
    }
     
    public void setBalance(double balance){
        this.balance = balance;
    }
     
    public double getBalance(){
        return balance;
    }
     
    public void saveALL(){
        //set database's balance = to the field balance using the id
        //do same with any other fields
    }
     
    
    then every like 5 min and on shutdown I would have to run something to save it to the database, how would I do that?
     
  6. Offline

    Amazing_kid

  7. Offline

    Amazing_kid

  8. Database communication isn't a one liner and a little bit harder to do than a simple config file. And communication with the database shouldn't be done in the game thread, as it can cause heavy lag, especially for remote databases. I suggest you to look for tutorials on this one. Perhaps someone else has a link to a good one for you.
     
  9. Offline

    Amazing_kid

    Thanks, if anyone has a tutorial let me know
     
  10. Offline

    Amazing_kid

  11. Offline

    Ultimate_n00b

    Search it yourself.. we can't help you if you don't try to do stuff for yourself.
     
  12. Offline

    Amazing_kid

    I have, sorry if I did not make that clear. I was unable to answer my question though.
     
  13. Offline

    Ultimate_n00b

    Try this, first result.
     
  14. Offline

    Amazing_kid

    I am not looking for how to use mysql!! I am wondering the best way to save my objects fields to a database, along with getting them.
     
  15. Offline

    Ultimate_n00b

    This might be my new quote.

    "a database" = mysql
    "save my objects" + "getting them" = retrieving data from database
     
  16. Offline

    Amazing_kid

    Forget it, you obviously don't understand what im getting at!
     
  17. Offline

    Ultimate_n00b

    If you want help, you have to tell us what you want help with. If I'm not getting it, it isn't my fault.
     
  18. Offline

    Amazing_kid

    I want to know the most efficient way to get all an objects fields so I can save them to a database. Along with how to use the information I get from the database to recall the objects on startup.
     
  19. Offline

    Ultimate_n00b

    Again with the "database". When I think of a database, I go to MySQL.
     
  20. Offline

    Amazing_kid

    Yes ok, so lets say I managed to get my data from a mysql database. Now what I am wondering is what is the most efficient way to create the object once again with the same data. Same thing with the most efficient way to get the objects fields so I can save it to a mysql database?
     
  21. Offline

    Ultimate_n00b

    So getting and storing things from MySQl, of which that page off google would've worked fine.
     
  22. Offline

    Amazing_kid

    Im wanting to get the info from the object though. Whats the best way to do that?
     
  23. Offline

    Ultimate_n00b

    I don't understand what you mean here. The object is the info, you set the info to the object.
     
  24. Offline

    Amazing_kid

    Ok but how exactly did factions do it? https://github.com/MassiveCraft/Factions I don't understand this part. "that" Google searches reveal nothing

    Code:
    public Faction load(Faction that)
        {
            this.setName(that.name);
            this.setDescription(that.description);
            this.setCreatedAtMillis(that.createdAtMillis);
            this.setHome(that.home);
            this.setPowerBoost(that.powerBoost);
            this.setOpen(that.open);
            this.setInvitedPlayerIds(that.invitedPlayerIds);
            this.setRelationWishes(that.relationWishes);
            this.setFlags(that.flags);
            this.setPerms(that.perms);
     
            return this;
        }
     
  25. Offline

    Ultimate_n00b

    decides to slowly remove himself from the thread..
     
Thread Status:
Not open for further replies.

Share This Page