Best way to save inventory contents

Discussion in 'Plugin Development' started by randomizer1234, Apr 11, 2013.

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


    Hello everybody,
    I'm currently developing a plugin regarding virtual inventories.
    I need a efficient way to save (possibly) a lot of inventories.
    I already have a working version of the plugin: a object containing a HashMap, containing player names as the key, and as the object another HashMap with an Integer as the Key and a object containing the inventory contents (in which every item is stored with CardboardBox, for which I have also submitted a bugfix in the official topic).
    So, this works, but in a production scenario where there can possibly be hundreds of chests, this will use up a lot of RAM.
    So I looked up on the Internet for a embedded object-based database, but this looked a bit an overkill to me.
    I'm asking, to anyone which has experience in maintaining a database of inventories, how can I do this.
    Some additional information: the inventories need to be accessed by player name and chest ID (which is unique only to the player but there can be infinite chests with ID 1, for example).

    Thank you!
  2. Offline


    Maybe simple database?:)
    SQLite for example.
  3. Offline


    Well, at that point an embedded relational database would be the best thing, because it is invisible to the user...
  4. Offline


    I make a class containing inventory data stored in an array. I use variables for the armor slots. I think it looks much, much cleaner than HashMaps.

    Edit: Oh... I guess I talked about the wrong thing.

    You could always write the inventories to disk, but that may be slow at some times or it use up a lot of space. I think a SQL database would be best.
  5. Offline


    The easiest way by far is to simply save to a yaml file, since ItemStacks are ConfigurationSerializable.
  6. Offline


    Mmh the problem here is that we are talking about hundreds of inventories...
    Yaml files are too slow to parse, think of a busy server where there could be the need to load (peak)
    3-4 inventories in a second... That would freeze Bukkit for sure, and I can't do it asynchronously because I need to call the API to show the inventory to the user, which is not thread safe :(
  7. randomizer1234
    Nobody forces you to save them all the time.
    You just load them when plugin enables then you use that handle to modify the variables, basically you're dealing with memory which is faster than any database.
    And after all that you can just save them to file when your plugin disables.

    You also can load them async and then send them to the main thread when done for display and stuff, but there's no need to do that anyways, just deal with memory instead of I/O.

    Also, memory is cheap, processing power is not.

    EDIT: alternatively you can just stringify the map from ItemStack's serialization and send it to a database but if you need it frequently it's best that you hold it in memory.
    GusGold likes this.
  8. Offline


    As I've said in the first post, the problem with keeping all of them in memory, it that... You need a lot of memory! And most of VPS and Shared Hosting services doesn't offer a lot of it... :(
    How much space does a Java object take? (in addition to the variables, I mean)
Thread Status:
Not open for further replies.

Share This Page