Save Inventory

Discussion in 'Plugin Development' started by Julian1Reinhardt, Aug 3, 2013.

Thread Status:
Not open for further replies.
  1. I have an inventory
    Code:
    Inventory cloudInventory = Bukkit.createInventory(null, 54, "CloudChest");
    that gets opened with a command. How can i say, that the inventory should be saved to a file, when the player closes it, and how can i say, that it will load up the same inventory when opened again?
    I'm a noob when it comes to saving stuff to a file :3
     
  2. Offline

    Samthelord1

    Julian1Reinhardt get slots and save to a config. Will be a big config though :p
     
  3. Julian1Reinhardt This is really easy, I use it in one of my plugins, basically (because the inventory is serializable) you get all the contents (will return a list) and save that list to a config, then to load it just do:
    Code:java
    1.  
    2. List<?> list = config.getList("inventory");
    3.  
    4. if (list != null) {
    5. for (int i = 0; i < Math.min(list.size(), inventory.getSize()); i++){
    6. inventory.setItem(i, (ItemStack)list.get(i));
    7. }
    8. }
    9.  


    Samthelord1 See above

    The size of a full inventory (27 slots) is around 1 or 2 kb
     
  4. I'm a total noob at files, so how do is get all the content and save to a config?

    And how do you say "save" when he exits the inventoy?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
  5. Julian1Reinhardt You do:
    Code:java
    1.  
    2. Inventoy inv = /*create ur inv here*/
    3.  
    4. //to save
    5. File f = /* ur file */
    6. Configuration config = YamlConfiguration.loadConfiguration(f);
    7.  
    8. config.set("inventory", inv.getContents());
    9.  


    To do the save thing, when you create the inventory and show it to the player put his name in a list of map, then listen for the inventorycloseevent, get the player who closed it and check if it equals a player in the hashmap. Obviously you also need this to save it. (Also don't store player objects in a list or map etc.., and if you do, make sure to remove the player when you're done doing the stuff you needed to do)
     
  6. Offline

    artish1


    Code:
     public HashMap <String, ItemStack[]> inventories = new HashMap <String, ItemStack[]>();
    public HashMap <String, ItemStack[]> armour = new HashMap <String, ItemStack[]>();

    Code:
    @SuppressWarnings("deprecation")
    public void saveSurvivalInventory(Player p){
         
        ItemStack[] inv = p.getInventory().getContents();
        ItemStack[] arm = p.getInventory().getArmorContents();
        inventories.put(p.getName(), inv);
        armour.put(p.getName(), arm);
        p.getInventory().clear();
        p.updateInventory();
    }
     
    @SuppressWarnings("deprecation")
    public void loadSurvivalInvetory(Player p){
        if(inventories.containsKey(p.getName()) && armour.containsKey(p.getName())){
       
     
        p.getInventory().clear();
       
        p.getInventory().setContents(inventories.get(p.getName()));
        p.getInventory().setArmorContents(armour.get(p.getName()));
        inventories.remove(p.getName());
        armour.remove(p.getName());
        p.updateInventory();
        }
    }
    
     
  7. CaptainBern
    It won't trigger the InventoryCloseEvent...
     
  8. Offline

    gomeow

  9. Offline

    JoshArgent

    https://github.com/JoshArgent/MySQL...orld/MySQLInventories/Backend/Serializer.java
    This class will serialize an ItemStack array (from an inventory) and save it as YAML in a byte array. If you skip out thr bit that converts it to a byte array you'll be able to save the config variable to file. If you are going to be storing lots of player vaults you may also want to look into compression.

    https://github.com/JoshArgent/MySQL...rld/MySQLInventories/Backend/Compression.java This class will compress the byte array using the standard gzip libraries and make a real difference to the size. Feel free to use any of the classes mentioned. :)
     
Thread Status:
Not open for further replies.

Share This Page