Guest User

InventorySave

a guest
May 28th, 2018
84
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package me.uba;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.inventory.Inventory;
  7. import org.bukkit.inventory.ItemStack;
  8.  
  9. public class InventorySave {
  10.  
  11. private InventorySave() { }
  12.  
  13. static InventorySave instance = new InventorySave();
  14.  
  15. public static InventorySave getInstance() {
  16. return instance;
  17. }
  18.  
  19. InventorySave plugin = this;
  20.  
  21. private HashMap<String, ItemStack[]> mySavedItems = new HashMap<String, ItemStack[]>();
  22.  
  23. public void saveInventory(Player player)
  24. {
  25. this.mySavedItems.put(player.getName(), copyInventory(player.getInventory()));
  26. }
  27.  
  28. /**
  29. * This removes the saved inventory from our HashMap, and restores it to the player if it existed.
  30. * @return true if success
  31. */
  32. public boolean restoreInventory(Player player)
  33. {
  34. ItemStack[] savedInventory = this.mySavedItems.remove(player.getName());
  35. if(savedInventory == null)
  36. return false;
  37. restoreInventory(player, savedInventory);
  38. return true;
  39. }
  40.  
  41. private ItemStack[] copyInventory(Inventory inv)
  42. {
  43. ItemStack[] original = inv.getContents();
  44. ItemStack[] copy = new ItemStack[original.length];
  45. for(int i = 0; i < original.length; ++i)
  46. if(original != null)
  47. copy = (original);
  48. return copy;
  49. }
  50.  
  51. private void restoreInventory(Player p, ItemStack[] inventory)
  52. {
  53. p.getInventory().setContents(inventory);
  54. }
  55. }
RAW Paste Data