Solved How to store a players inventory before log off

Discussion in 'Plugin Development' started by kevinspl2000, Nov 12, 2013.

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

    kevinspl2000

    So how would I store a players inventory before he logs off?
     
  2. Offline

    Dread9Nought

    public static HashMap<String, ItemStack[]> inventories = new HashMap<String, ItemStack[]>();

    public void onLogout(PlayerLeaveEvent e) {
    inventories.put(e.getPlayer().getName(), e.getPlayer().getInventory().getContents());
    }
     
  3. Offline

    NinjaWAffles

    It's PlayerQuitEvent. :p
     
    AoH_Ruthless likes this.
  4. Offline

    Dread9Nought

    Psssht yay for typing code w/o ide
     
  5. Offline

    kevinspl2000

    If I try to do something to the intentory, it says 'The method dropItem(Location, ItemStack) in the type World is not applicable for the arguments (Location, ArrayList<ItemStack>)'. Yes I changed it to an arraylist..
     
  6. Offline

    kevinspl2000

  7. Offline

    iFamasssxD

    Well you need to not change it to ArrayList..
    Instead you can iterate through the arrayList and on each ItemStack in there you can call the dropItem(location, itemstack)

    As for "Storing" the inventory, a HashMap wont store it through a reset. You can check around for ways to Serialize inventorys and save/load them from configs.
     
  8. Offline

    kevinspl2000

    iFamasssxD
    I only need to store it for a small amount of time and I only need to store the inventory, I dont need the player name part...

    ... I don't get the HashMap part everything is much simplier with ArrayLists and HashSets

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  9. Offline

    drtshock

    Have you even tried any code yourself? If you have then it makes it easier to help you.
     
  10. Offline

    blm456

    kevinspl2000 a HashMap allows you to recall the inventory with the player's name. If you use a List or HashSet it will be harder to retrieve later. You can put it in by:
    Code:java
    1. HashMap <Player, Inventory> inventories = new HashMap<Player, Inventory>();
    2. //p is the predefined player
    3. Inventory i = p.getInventory();
    4. inventories.put(p, i);

    and to retrieve it's
    Code:
    //p is still a predefined player
    if(inventories.containsKey(p)){
    Inventory i = inventories.get(p);
    }
     
    XvBaseballkidvX likes this.
  11. Offline

    xTrollxDudex

    blm456
    Yay for putting Player objects in a Collection
     
    9903286 and jacklin213 like this.
  12. Offline

    jacklin213

    p.getName();

    QQ
     
  13. Offline

    kevinspl2000

    Okay thanks but I want to drop the inventory to somewhere
    So I did
    player.getWorld().dropItem(loc, inventories);
    and I am getting errors because the inventories is supposed to be an item stack...
     
  14. Offline

    kevinspl2000

    BUMP

    Daily BUMP...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  15. Offline

    drtshock

    Iterate over the itemstack.
     
    chaseoes and kevinspl2000 like this.
  16. Offline

    jacklin213

    Replace inventories to inventories.getContents() to drop the whole inv
     
  17. Offline

    kevinspl2000

  18. Offline

    Jentagh

    Keep in mind that your hashmap wont persist through reloads/restarts, if I'm correct.
     
  19. Offline

    drtshock

    No, they do not.
     
    chaseoes likes this.
Thread Status:
Not open for further replies.

Share This Page