Saving custom inventories

Discussion in 'Plugin Development' started by Monkeyboystein, Jul 16, 2013.

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

    Monkeyboystein

    Before you all say use the search button, i have tried and failed, on that note here we go:

    Im attempting to make a custom inventory of 9 slots with the name of ChatColor.Dark_Red + "Armor Storage"

    I actually got it to create the inventory but i dont know how to save it, and reopen it, im very confused,
    how do i go about doing this?
    thanks
     
  2. Offline

    soulofw0lf

    you can save it to a hashmap when created
     
  3. Offline

    Monkeyboystein

    how do you do that?
    soulofw0lf
     
  4. Offline

    soulofw0lf

  5. Offline

    Monkeyboystein

    Are there any other methods?
     
  6. Offline

    iFamasssxD

    You could get the ItemStack[] and save it to a config so it doesnt reset on reload or restart. Set it to a list and read from that list per player.
     
  7. Offline

    Monkeyboystein

     
  8. Offline

    iFamasssxD

  9. Offline

    onCommand

    You want to open a custom Inventory right?

    Code:
    Code:java
    1. public void OpenInventory(Player p){
    2. Inventory inv = Bukkit.createInventory(null,9 *(rows you want to add),"name");
    3. p.openInventory(inv);
    4. }
    5.  


    You can handle this custom created Inventory just like a normal Player Inventory. ^^
     
  10. Offline

    xTrollxDudex

  11. Offline

    artish1




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

    And setup these methods:
    Code:
        @SuppressWarnings("deprecation")
        public void saveSurvivalInventory(Player p){
           
            ItemStack[] inv = p.getInventory().getContents();
            ItemStack[] arm = p.getInventory().getArmorContents();
            plugin.inventories.put(p.getName(), inv);
            plugin.armour.put(p.getName(), arm);
            p.getInventory().clear();
            p.updateInventory();
        }
     
        @SuppressWarnings("deprecation")
        public void loadSurvivalInvetory(Player p){
       
            p.getInventory().setContents(plugin.inventories.get(p.getName()));
            p.getInventory().setArmorContents(plugin.armour.get(p.getName()));
            plugin.inventories.remove(p.getName());
            plugin.armour.remove(p.getName());
            p.updateInventory();
        }
     
  12. Offline

    gomeow

  13. Offline

    Burnett1

    Code:
    //To save
     
    //Get contents as a list.
    List<ItemStack> inv = Arrays.asList(inv.getContents());
     
    //Save to a file.
    Config.set(p.getName() + ".Contents", main);
     
    //To load
     
    //Get the items.
    List<ItemStack> inv = (List<ItemStack>) Config.getList(p.getName() + ".Main");
     
    try {
     
        for (@SuppressWarnings("unused")
        ItemStack i : main);
     
    } catch (ClassCastException e) {
        //Check for an error while getting the items.
        e.printStackTrace();
    }
     
    //Set content.
    inv.setContents(main.toArray(new ItemStack[36]));
    An example of the config when saving a players armor and main inventory.

    Code:
    Burnett1:
      Main:
      - ==: org.bukkit.inventory.ItemStack
        type: BLAZE_ROD
      - ==: org.bukkit.inventory.ItemStack
        type: ANVIL
      - null
      - null
      - null
      - null
      - null
      - ==: org.bukkit.inventory.ItemStack
        type: DIAMOND_SWORD
        meta:
          ==: ItemMeta
          meta-type: UNSPECIFIC
          enchants:
            LOOT_BONUS_MOBS: 3
            PROTECTION_PROJECTILE: 4
            WATER_WORKER: 1
            ARROW_DAMAGE: 5
          repair-cost: 6
      - ==: org.bukkit.inventory.ItemStack
        type: WRITTEN_BOOK
        meta:
          ==: ItemMeta
          meta-type: BOOK
          title: Test
          author: Burnett1
          pages:
          - Test text to see if it saves.
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      - null
      Armor:
      - ==: org.bukkit.inventory.ItemStack
        type: AIR
        damage: -1
        amount: 0
      - ==: org.bukkit.inventory.ItemStack
        type: AIR
        damage: -1
        amount: 0
      - ==: org.bukkit.inventory.ItemStack
        type: AIR
        damage: -1
        amount: 0
      - ==: org.bukkit.inventory.ItemStack
        type: AIR
        damage: -1
        amount: 0
     
Thread Status:
Not open for further replies.

Share This Page