Save Inventory

Discussion in 'Plugin Development' started by CraftCreeper6, Oct 15, 2014.

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

    CraftCreeper6

    Hello! I was just wondering how I would save a custom inventory for each player. Like, creating a custom inventory with 54 slots & saving it.

    Any help is appreciated :)
     
  2. Offline

    TheCodingCat

    Look into Serializing ItemStacks and learn to save them to a per player YAML file
     
  3. Offline

    CraftCreeper6

    TheCodingCat
    Hello, thanks for the reply. I managed to find some methods. They work fine, but how would I use this to save a custom inventory?
    Here are the methods:

    Save Inventory:
    Code:java
    1. public boolean saveInventoryToFile(Inventory inventory, File path, Player p) {
    2. if (inventory == null || path == null || path == null) return false;
    3. try {
    4. File invFile = new File(path, p.getName() + ".invsave");
    5. if (invFile.exists()) invFile.delete();
    6. FileConfiguration invConfig = YamlConfiguration.loadConfiguration(invFile);
    7.  
    8. invConfig.set("Title", inventory.getTitle());
    9. invConfig.set("Size", inventory.getSize());
    10. invConfig.set("Max stack size", inventory.getMaxStackSize());
    11. if (inventory.getHolder() instanceof Player) invConfig.set("Holder", ((Player) inventory.getHolder()).getName());
    12.  
    13. ItemStack[] invContents = inventory.getContents();
    14. for (int i = 0; i < invContents.length; i++) {
    15. ItemStack itemInInv = invContents[i];
    16. if (itemInInv != null) if (itemInInv.getType() != Material.AIR) invConfig.set("Slot " + i, itemInInv);
    17. }
    18.  
    19. invConfig.save(invFile);
    20. return true;
    21. } catch (Exception ex) {
    22. return false;
    23. }
    24. }[/i]


    Load Inventory:
    Code:java
    1. public Inventory getInventoryFromFile(File file) {
    2. if (file == null) return null;
    3. if (!file.exists() || file.isDirectory() || !file.getAbsolutePath().endsWith(".invsave")) return null;
    4. try {
    5. FileConfiguration invConfig = YamlConfiguration.loadConfiguration(file);
    6. Inventory inventory = null;
    7. String invTitle = invConfig.getString("Title", "Inventory");
    8. int invSize = invConfig.getInt("Size", 54);
    9. int invMaxStackSize = invConfig.getInt("Max stack size", 64);
    10. InventoryHolder invHolder = null;
    11. if (invConfig.contains("Holder")) invHolder = Bukkit.getPlayer(invConfig.getString("Holder"));
    12. inventory = Bukkit.getServer().createInventory(invHolder, invSize, ChatColor.translateAlternateColorCodes('&', invTitle));
    13. inventory.setMaxStackSize(invMaxStackSize);
    14. try {
    15. ItemStack[] invContents = new ItemStack[invSize];
    16. for (int i = 0; i < invSize; i++) {
    17. if (invConfig.contains("Slot " + i)) invContents[i] = invConfig.getItemStack("Slot " + i);
    18. else invContents[i] = new ItemStack(Material.AIR);
    19. }
    20. inventory.setContents(invContents);
    21. } catch (Exception ex) {
    22. }
    23. return inventory;
    24. } catch (Exception ex) {
    25. return null;
    26. }
    27. }[/i][/i]
     
  4. Offline

    JjPwN1

    You would go about saving an inventory like this:

    Code:java
    1. for(int x = 0; x <= (inv.getSize() - 1); x++){
    2. if(inv.getItem(x) != null && inv.getItem(x).getType() != Material.AIR){
    3. ItemStack item = inv.getItem(x);
    4. this.setConfigValue("savedInventory." + x + ".item", item);
    5. }
    6. }


    You would then load the inventory like this:

    Code:java
    1. for(int x = 0; x <= (inv.getSize() - 1); x++){
    2. if(this.getConfiguration().getItemStack("savedInventory." + x + ".item") != null){
    3. inv.setItem(x, this.getConfiguration().getItemStack("savedInventory." + x + ".item"));
    4. }
    5. }


    These methods will not save the player's armor. You'll have to figure out how that'll work by yourself.
     
  5. Offline

    CraftCreeper6

    JjPwN1
     
  6. Offline

    JjPwN1

    I don't see the problem here.
     
  7. Offline

    CraftCreeper6

    JjPwN1
    I don't want to save the players inventory, instead, a custom one. With 54 slots & Custom item metas. By the way, I managed to do this but how would I save the inventory? Because when I close it -- it just shows up blank when I open it next time.

    EDIT: I found the cause of the problem, it is not saving to the file correctly. :/ Not sure why
     
  8. Offline

    JjPwN1

    The code I provided would still work with a custom inventory. The "inv" variable would be your custom inventory.

    You have to create a method to save the inventory, as you already know. You then need to write the inventory data to a file - your main class's getConfig() method (or another FileConfiguration) would probably be easiest to start off with, as it already has a method for saving itemstacks. An example:

    Code:java
    1. public void saveInventory(Inventory inv){
    2. for(int x = 0; x <= (inv.getSize() - 1); x++){
    3. if(inv.getItem(x) != null && inv.getItem(x).getType() != Material.AIR){
    4. main.getConfig().set("savedInventory." + x + ".item", inv.getItem(x));
    5. }
    6. }
    7. main.saveConfig();
    8. }
     
  9. Offline

    CraftCreeper6

    JjPwN1
    Thank you so much! :D

    EDIT: I can take items then they will still be there when I re-open the inventory.
     
  10. Offline

    JjPwN1

    Create a new InventoryCloseEvent and check if the event.getInventory() is the same as your custom inventory. If it is, then save the inventory with saveInventory(event.getInventory())
     
  11. Offline

    CraftCreeper6

    JjPwN1
    Already done that. Doesn't work :/ I even tried deleting the file and re-making it.
     
  12. Offline

    JjPwN1

    You need to wipe the configuration section before changing it. Add this to the beginning of your saveInventory() method:
    Code:java
    1. main.getConfig().set("savedInventory", null);
     
  13. Offline

    CraftCreeper6

    JjPwN1
    Still doesn't work.
     
  14. Offline

    JjPwN1

    Show me your code, please.
     
  15. Offline

    CraftCreeper6

    JjPwN1
    Save / Open
    Code:java
    1. public void saveInventory(Player p, Inventory inv){
    2. File f = new File(getDataFolder(), p.getName() + ".yml");
    3. FileConfiguration fc = YamlConfiguration.loadConfiguration(f);
    4. fc.set("savedInventory", null);
    5. for(int x = 0; x <= (inv.getSize() - 1); x++){
    6. if(inv.getItem(x) != null && inv.getItem(x).getType() != Material.AIR){
    7. fc.set(p.getName() + "." + x + ".item", inv.getItem(x));
    8. }
    9. }
    10. try {
    11. fc.save(f);
    12. } catch (IOException e) {
    13. e.printStackTrace();
    14. }
    15. }
    16.  
    17. public void openInventory(Player p, Inventory inv) {
    18. File f = new File(getDataFolder(), p.getName() + ".yml");
    19. FileConfiguration fc = YamlConfiguration.loadConfiguration(f);
    20. for(int x = 0; x <= (inv.getSize() - 1); x++){
    21. if(fc.getItemStack(p.getName() + "." + x + ".item") != null){
    22. inv.setItem(x, fc.getItemStack(p.getName() + "." + x + ".item"));
    23. }
    24. }
    25.  
    26. p.openInventory(inv);
    27.  
    28. }


    Open the inventory and save it too.
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEntityEvent e) {
    3.  
    4. Villager v = (Villager) e.getRightClicked(); // DO NOT JUDGE ME ON THIS. I KNOW I'LL GET AN NPE I'LL FIX IT LATER
    5.  
    6. if(!(v instanceof Villager)) return;
    7.  
    8. e.setCancelled(true);
    9.  
    10. e.getPlayer().closeInventory();
    11.  
    12. Inventory inv = Bukkit.createInventory(e.getPlayer(), 54, ChatColor.BLUE + e.getPlayer().getName() + "'s Bank");
    13.  
    14. main.openInventory(e.getPlayer(), inv);
    15.  
    16. }
    17.  
    18. @EventHandler
    19. public void onInvClose(InventoryCloseEvent e) {
    20.  
    21. Player p = (Player) e.getPlayer();
    22.  
    23. if(e.getInventory().getName().equalsIgnoreCase(ChatColor.BLUE + p.getName() + "'s Bank")){
    24.  
    25. main.saveInventory(p, e.getInventory());
    26.  
    27. }
    28. }
     
  16. Offline

    JjPwN1

    In your saveInventory() method, you need to clear the currently saved inventory through this instead:
    Code:java
    1. fc.set(p.getName(), null);
     
  17. Offline

    CraftCreeper6

    JjPwN1
    NVM Fixed it! :D Turns out I got the wrong path in the config it was the players name not savedInventory :D
     
Thread Status:
Not open for further replies.

Share This Page