[SOLVED] Keeping inventory on death?

Discussion in 'Resources' started by Kalman Olah, Jul 11, 2011.

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

    Kalman Olah

    So I'm writing a plugin that lets a player keep his or her inventory under certain conditions. Problem is, either I keep the inventory and the item drops stay, or I don't keep anything and the item drops aren't there either.

    I'd like to keep my inventory, and not to drop any items on death...

    My entitylistener:

    Code:
    public void onEntityDeath(EntityDeathEvent event){
    
            if(event.getEntity() instanceof Player){
    
                Player plr = (Player) event.getEntity();
                String name = plr.getName();
                        if(OKmain.keepinv.equals("true")){
    
                            if(event.getDrops() != null){
    
                                OKmain.dropmap.put(name, event.getDrops());
    
                                event.getDrops().clear();
    
                            }
    
                        }
    
            }
    
    }
    My playerlistener:
    Code:
    public void onPlayerRespawn(PlayerRespawnEvent event){
    
            Player plr = event.getPlayer();
            String name = plr.getName();
    
    
                    if(OKmain.keepinv.equals("true")){
    
                        if(OKmain.dropmap.get(name) != null){
    
    
                            List<ItemStack> inv = OKmain.dropmap.get(name);
    
                            Iterator<ItemStack> it = inv.iterator();
    
                            while(it.hasNext()){
    
                                plr.getInventory().addItem(it.next());
    
                                it.remove();
    
                            }
    
                        }
    
                    }
                }
    
    Even though my event.getDrops().clear(); comes after I put the inventory contents into the HashMap, the event.getDrops() being put into the HashMap seems to be the cleared, empty one...

    Any ideas?

    Bump...could really use some help with this.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  2. Offline

    captainawesome7

    Just do:
    HashMap<Player, ItemStack[]> inventories = new HashMap<Player, ItemStack[]>();
    Then add the player and player.getInventory().getContents() to the hashmap.
    Then when the player respawns just use player.getInventory().setContents() to set it to the item stack found in the hashmap. And as far as clearing the drops from the ground, that isn't working is it? I'm going to go into Eclipse for a second and see if I can fix that.
     
  3. Offline

    Kalman Olah

    Oh, the event.getDrops().clear() works(as in: it removes all inventory drops), but it also breaks the HashMap insert I execute before I call the clear() function. I'll check out your suggestion in a moment.
     
  4. Offline

    captainawesome7

    Ahh if that works you should be fine with the HashMap<Player, ItemStack[]> and clearing the getDrops() then
     
  5. Offline

    Kalman Olah

    Alright, so the inventory saving & adding works, thanks a lot :). I'll try calling the clear() function on getDrops() now, since I'm not actually using it, I'm saving the inventory.

    EDIT: Yeah, pretty much what you just said :p. Thanks for the help.
     
Thread Status:
Not open for further replies.

Share This Page