Solved Method for Saving and Restoring Inventory?

Discussion in 'Plugin Development' started by PatoTheBest, Jan 26, 2014.

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

    PatoTheBest

    Hello, I was wondering if this is a good method of saving and restoring inventories?

    EDIT: Not tested so point out bugs or improvements if you see any.
    Code:java
    1. public class InventoryManager {
    2.  
    3. private static HashMap<String, ItemStack[]> armourContents = new HashMap<String, ItemStack[]>();
    4. private static HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
    5. private static HashMap<String, Location> locations = new HashMap<String, Location>();
    6.  
    7. public static void saveInventory(Player player){
    8. armourContents.put(player.getName().toLowerCase(), player.getInventory().getArmorContents());
    9. inventoryContents.put(player.getName().toLowerCase(), player.getInventory().getContents());
    10. locations.put(player.getName().toLowerCase(), player.getLocation());
    11. player.getInventory().clear();
    12. }
    13.  
    14. public static void restoreInventory(Player player){
    15. player.getInventory().clear();
    16. int x = locations.get(player.getName().toLowerCase()).getBlockX(),
    17. y = locations.get(player.getName().toLowerCase()).getBlockY(),
    18. z = locations.get(player.getName().toLowerCase()).getBlockZ();
    19.  
    20. float pitch = locations.get(player.getName().toLowerCase()).getPitch(),
    21. yaw = locations.get(player.getName().toLowerCase()).getYaw();
    22.  
    23. World world = locations.get(player.getName().toLowerCase()).getWorld();
    24.  
    25. player.teleport(new Location(world, x, y, z, pitch, yaw));
    26.  
    27. player.getInventory().setContents(inventoryContents.get(player.getName().toLowerCase()));
    28. player.getInventory().setArmorContents(armourContents.get(player.getName().toLowerCase()));
    29. }
    30.  
    31. }
     
  2. PatoTheBest
    That's the way I do it, just remember to remove them from the maps after restoration. Also, why are you getting the location coordinates and world from the map, then turning them into another location? Just use player.teleport(locations.get(player.getName().toLowerCase()))
     
  3. Offline

    calebbfmv

    Um yeah thats fine, if you only want to keep things in memory, I.E. they only last till a reload or a stop.
     
  4. Offline

    PatoTheBest

    Assist
    Thanks for the location fix, I feel dumb for having such a big method for a small simple thing.

    calebbfmv and Assist
    Thanks for pointing out that I didn't remove the Hasmap.

    EDIT: Final code:
    Code:java
    1. public class InventoryManager {
    2.  
    3. private static HashMap<String, ItemStack[]> armourContents = new HashMap<String, ItemStack[]>();
    4. private static HashMap<String, ItemStack[]> inventoryContents = new HashMap<String, ItemStack[]>();
    5. private static HashMap<String, Location> locations = new HashMap<String, Location>();
    6.  
    7. public static void saveInventory(Player player){
    8. armourContents.put(player.getName().toLowerCase(), player.getInventory().getArmorContents());
    9. inventoryContents.put(player.getName().toLowerCase(), player.getInventory().getContents());
    10. locations.put(player.getName().toLowerCase(), player.getLocation());
    11. player.getInventory().clear();
    12. }
    13.  
    14. public static void restoreInventory(Player player){
    15. player.getInventory().clear();
    16. player.teleport(locations.get(player.getName().toLowerCase()));
    17.  
    18. player.getInventory().setContents(inventoryContents.get(player.getName().toLowerCase()));
    19. player.getInventory().setArmorContents(armourContents.get(player.getName().toLowerCase()));
    20.  
    21. locations.remove(player.getName().toLowerCase());
    22. armourContents.remove(player.getName().toLowerCase());
    23. inventoryContents.remove(player.getName().toLowerCase());
    24. }
    25. }
    26.  
     
  5. Offline

    calebbfmv

    PatoTheBest
    To be honest, not that it matters much, but you don't need the .toLowerCase(); stuff
     
  6. Offline

    PatoTheBest

    Just safety.
     
Thread Status:
Not open for further replies.

Share This Page