[LIB] Easily save and load an inventory to config

Discussion in 'Resources' started by monkeymanboy, Jun 1, 2014.

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

    monkeymanboy

    I am working on something where I need to save an inventory to a file so I made these 2 functions to easily do that
    Code:java
    1. public void saveInventory(Inventory inventory, FileConfiguration file, String path){
    2. int i = 0;
    3. for(ItemStack im : inventory.getContents()){
    4. file.set(path + ".contents." + i, im);
    5. i++;
    6. }
    7. file.set(path + ".maxstacksize", inventory.getMaxStackSize());
    8. file.set(path + ".inventorytitle", inventory.getTitle());
    9. file.set(path + ".inventorysize", inventory.getSize());
    10. file.set(path + ".inventoryholder", inventory.getHolder());
    11. }
    12. public Inventory getInventory(FileConfiguration file, String path){
    13. if(file.contains(path)){
    14. Inventory inv = Bukkit.createInventory((InventoryHolder) file.get(path + ".inventoryholder"), file.getInt(path + ".inventorysize"), file.getString(path + ".inventorytitle"));
    15. inv.setMaxStackSize(file.getInt(path + ".maxstacksize"));
    16. for(int i=0; i<inv.getSize(); i++){
    17. if(file.contains(path + ".contents." + i)){
    18. inv.setItem(i, file.getItemStack(path + ".contents." + i));
    19. }
    20. }
    21. return inv;
    22. }
    23. return null;
    24. }

    Sample Usage:
    Code:java
    1. @EventHandler
    2. public void PlayerOpen(PlayerInteractEvent event){
    3. if(event.getPlayer().getItemInHand().getType() == Material.STONE){
    4. if(getInventory(this.getConfig(), "path") != null){
    5. event.getPlayer().openInventory(getInventory(this.getConfig(), "path"));
    6. } else
    7. event.getPlayer().openInventory(Bukkit.createInventory(null, 27, "title"));
    8. }
    9. }
    10. @EventHandler
    11. public void PlayerClose(InventoryCloseEvent event){
    12. if(event.getInventory().getTitle().equalsIgnoreCase("title")){
    13. saveInventory(event.getInventory(), this.getConfig(), "path");
    14. this.saveConfig()
    15. }
    16. }
     
  2. It would be nice if you formatted this better with indents, and you know you can do i++ instead of i = i + 1. Otherwise, nice!
     
    Someone_Like_You likes this.
  3. Offline

    lenis0012

    Someone_Like_You likes this.
  4. Although i += 1 allows more customizability, when you only want to add one, I think it's quicker just to do i++. :p Then again, different people have different preferences so I won't complain. But doing: i = i + 1; it's quite silly in my opinion (What this guy did).
     
  5. Offline

    shohouku

    Any idea why when I restart my server my player turns into an Offline Player?
     
  6. Offline

    MajorSkillage

    C += 1 Yeah lets code in it.
    Anyway, can't you just
    Code:
    ItemStack[] blah = p.getInventory();
    getConfig().set("sup", blah);
    ItemStack[] is = (ItemStack[]) getConfig().get("sup");
    p.setContents(is);
    Worked for meh :/
     
Thread Status:
Not open for further replies.

Share This Page