Save and restore the inventory when a player die (With commands)

Discussion in 'Archived: Plugin Requests' started by Lummox, Sep 3, 2013.

  1. I need a plugin to save the inventory in any file or database for when a player dies, and one administrator can restore it with a command.

    Is there any plugin that works on 1.6.2?
     
  2. Offline

    CeramicTitan

    Save it in a hashmap, before they die. Give it back to them after they die. If you wanna use commands, save it in the same was as below, but them use the same method to give it back to them via commands. If you are unsure on how to do that, just tahg me.

    Code:java
    1. //Declare a global variables
    2. public Map<String, ItemStack[]> savedInventory = new HashMap<String, ItemStack[]>();
    3. public Map<String, ItemStack[]> savedArmourInventory = new HashMap<String, ItemStack[]>();
    4.  
    5. //Saving the inventory
    6. @EventHandler
    7. public void savePlayerInventory(PlayerDeathEvent event){
    8. Player p = event.getEntity();
    9. if(!savedInventory.containsKey(p.getName()) && !savedArmourInventory.containsKey(p.getName())){
    10. savedInventory.put(p.getName(), p.getInventory().getContents().clone());
    11. savedArmourInventory.put(p.getName(), p.getInventory().getArmorContents().clone());
    12. p.sendMessage("Your inventory (" + p.getInventory().getContents().clone().length +" items saved). Armour saved as well");
    13. }
    14. }
    15. //Restoring the inventory
    16. @EventHandler
    17. public void restorePlayerInventory(PlayerRespawnEvent event){
    18. final Player p = event.getPlayer();
    19. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    20. @Override
    21. public void run() {
    22. if(savedArmourInventory.containsKey(p.getName())){
    23. p.getInventory().setArmorContents(savedArmourInventory.get(p.getName()));
    24. savedArmourInventory.remove(p.getName());
    25. p.sendMessage("Restored your armour");
    26. }
    27. if(savedInventory.containsKey(p.getName())){
    28. p.getInventory().setContents(savedInventory.get(p.getName()));
    29. savedInventory.remove(p.getName());
    30. p.sendMessage("Restored" + savedInventory.get(p.getName()).length + "items");
    31. }
    32. }
    33.  
    34. },20L);
    35. }
    36.  
    37.  
     
  3. Offline

    AndyMcB1



    • if(p.getHealth() <= 2){





    why?
     
  4. Offline

    EdenCampo

    AndyMcB1
    When he has low hp, he is probably being attacked..
     
  5. Offline

    Czech Erface

    EdenCampo
    The damage event makes it easily calculable as to whether or not a player will die. The getHealth() call seems to obstruct the purpose of the original request. Or am I missing something?
     
  6. Offline

    CeramicTitan

    I used that event, because i wasn't sure that with the death event, if the inventorys contents would return null or not
     

Share This Page