Solved .closeInventory() does not save !?

Discussion in 'Plugin Development' started by InflamedSebi, May 13, 2013.

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

    InflamedSebi

    I m saving inventories to files, but to prevent bugs, i get all viewers and want to close their view...
    Problem: it does not save the inventory, if i close the view manually with entity.closeInventory()

    any suggestions?
     
  2. Offline

    Darq

    More information? Closing an inventory isn't meant to "save" it, any edits to the contents of an open inventory are made in real time to the Inventory object involved, so my presumption is that you're not handling your Inventory objects correctly. Do you create a new inventory and set its contents each time it's opened? Or do you hold on to an existing Inventory object (either from a container block, or one you've made yourself)?
     
  3. Offline

    InflamedSebi

    Darq I created an inventpory with Bukkit.createInventory(holder, size, name); and save it to a map. Player can open it with /open <name> (works just fine) if the player disconectes or the server is stopping all inventories gets saved to files (serialisation is also working just fine). but i want to close the inventory, before saving to file, so i get the viewers() than for each viewer i do viewer.closeInventory() but i after restart of the server the inventory does not contain the changes of the last opening ... so it seems like the close() does not save it ...
    if i close the inventory with my player jusr normaly, all is just fine ...

    execute order:

    map.get(inventory)
    inventory.get(viewer)
    viewer.closeInventory()
    inventory.get(contents)
    contents.serialize.save()

    ----------------------EDIT

    oh ok i realized, that the inventory gets not saved if it is open, and all changes since the last save are lost ... this is odd ...

    Code:java
    1.  
    2. public void savePlayer(HumanEntity owner) {
    3. if (plugin.chests.containsKey(owner)) {
    4. for (Inventory inv : plugin.chests.get(owner).values()) {
    5. if (!inv.getViewers().isEmpty())
    6. for (HumanEntity entity : inv.getViewers()) {
    7. entity.closeInventory();
    8. }
    9. List objects = new ArrayList();
    10. objects.add(inv.getSize());
    11. try {
    12. List<Map<String, Object>> serialized = SerializationUtility.serializeItemList(Arrays.asList((ConfigurationSerializable[]) inv
    13. .getContents()));
    14. File pFolder = new File(plugin.file.toString() + File.separator + owner.getName());
    15. if (!pFolder.exists())
    16. pFolder.mkdir();
    17. objects.addAll(serialized);
    18. String filename = plugin.chestSettings.getChestPath(inv.getName());
    19. FileInOutput.write(objects, new File(pFolder, filename + ".sc"));
    20. } catch (Exception e) {
    21. e.printStackTrace();
    22. sl.log(ChatColor.RED + "Inventory was broken! " + plugin.chestSettings.getChestPath(inv.getName()) + " " + owner.getName(),
    23. Level.WARNING);
    24. }
    25.  
    26. }
    27. }
    28. sl.log(ChatColor.DARK_GREEN + "Done!", 3);
    29.  
    30. }


    this is my code
     
  4. Offline

    Ivan

    Why are you using HumanEntity while it is a player? Why not just use Player rightaway?
     
  5. Offline

    InflamedSebi

    ok seems like i fixed that issue ... ... if i dont close the inventory it gets saved ... seems like closeInventory() delets the whole inventory ...
     
  6. You have to save this to an external file, just create a new file with the name of the inventory (set it in the title or so) and then create a new file with that name and save the inventory contents as a list; in backpacks++ I save them under "inventory:", to get the contents just use: List<?> contents = file.getList("inventory"); and to load it use: if (list != null) {
    for (int i = 0; i < Math.min(list.size(), inventory.getSize()); i++){
    inventory.setItem(i, (ItemStack)list.get(i));
    }
    }
    I will not tell you how to save the whole thing, supposing you can find out yourself.
     
  7. I would d entity.savedata and entity.updateinventory if those are available
     
  8. Offline

    Technius

    InflamedSebi You could try listening to InventoryCloseEvents.
     
  9. Offline

    InflamedSebi

    DreTaX Technius CaptainBern
    the save itself was not my problem ... it worked just fine ... but entity.closeInventory() did delete the inventory and that was the problem ...
     
  10. Offline

    the_merciless

    Why dont u just save the inventory before you close it?
     
Thread Status:
Not open for further replies.

Share This Page