Saving ItemStack[] to database?

Discussion in 'Plugin Development' started by ItsComits, Feb 8, 2018.

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

    ItsComits

    Hello, When researching how to store [] into a database. I recently noticed that storing arrays into columns is not a good idea. What would be the best way to store an ItemStack[]. Keeping in mind that these ItemStack[] will only be used twice (Enable & Disable of plugin)
     
  2. Offline

    timtower Administrator Administrator Moderator

    @ItsComits Use rows instead of columns.
    Biggest challenge is serializing the ItemStack itself though
     
  3. Offline

    ItsComits

    @timtower I am using the rows for the players UUID inherited from another table where all the players are stored. I am wanting to create a column named armor. Which will store an array of the players armor. Why would I need to serialise the itemStack[]. Can I not use the array as I normally would?
     
  4. Offline

    timtower Administrator Administrator Moderator

    Databases don't know ItemStack as type.
    And it might work, depends on your serialization, but I like it to give each value its own row instead of putting multiple into the same cell
     
  5. Offline

    ItsComits

    @timtower How would I go about adding an array to the database? Example code would help a ton. :)
     
  6. Offline

    timtower Administrator Administrator Moderator

    @ItsComits Don't see it as an array, see it as multiple values, so a loop.
     
  7. Offline

    ItsComits

    @timtower Thank you for your help. Just out of curiosity. Can multiple hashmaps per player cause performance issues?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    ItsComits

    @timtower What content would you say to avoid the most?
     
  10. Offline

    timtower Administrator Administrator Moderator

    @ItsComits Player objects.
    But everything is relative at this point.
     
  11. Offline

    PlayerWinEvent

    Code:
    public static void saveBackpack(Player player) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            try {
                BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream);
                dataOutput.writeInt(player.getInventory().getSize());
                for(int i = 0; i < player.getInventory().getSize(); i ++) {
                    dataOutput.writeObject(player.getInventory().getItem(i));
                }
                dataOutput.close();
                String serialized = Base64Coder.encodeLines(outputStream.toByteArray());
                MongoInitializer.database.getCollection("vanilla").updateOne(eq("_id", player.getName()), set("inventory", serialized), MongoInitializer.options);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    hope this might help you (ps: i use mongo as my main db)
     
  12. Offline

    Horsey

    @timtower @ItsComits
    Why would it depend on the content? Maps store the pointers to the data you put in them, so regardless of what you put in them they will always take the same amount of memory.

    This does come with a few caveats: mainly that when storing objects like players, they cannot be cleared by the GC even after they leave because your Map still stores a reference to them. Although regardless of the data, removing entries that you're done with is good practice, so you should be doing it.
     
    RcExtract likes this.
Thread Status:
Not open for further replies.

Share This Page