Saving Inventories to files

Discussion in 'Plugin Development' started by DeamonZ, May 10, 2014.

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

    DeamonZ

    Hello!

    I'm working on a minigame plugin for a friend of mine however I got a little stuck on one of its features.
    He asked me to save a players inventory before he enters a game and then he gets his inventory back after its finished.
    I looked around on these forums and found some threads with a LOT of codes but some are outdated and gives errors. Also I don't fully understand what they do and I want to learn how to save them.

    Regards,
    Deamon
     
  2. Offline

    Serializator

    You can make a hashmap with the key UUID and value ItemStack[] and put the player.getInventory().getContents(); erin.

    Code:java
    1. Map<UUID, ItemStack[]> inventory = new HashMap<UUID, ItemStackp[>();
    2. inventory.put(player.getUniqueId(), player.getInventory().getContents());
     
  3. Offline

    DeamonZ

    I've made this as a little test for when a player returns to gamemode 0 (Survival) he would get his armor back and his items.
    However it doesn't work. I also tried it with a command /inv save and /inv load and that did work however this doesn't.
    Any idea why not?
    Code:java
    1. @EventHandler
    2. public void onGamemodeChange(PlayerGameModeChangeEvent e) {
    3. Map<UUID, ItemStack[]> inv = new HashMap<UUID, ItemStack[]>();
    4. Map<UUID, ItemStack[]> armour = new HashMap<UUID, ItemStack[]>();
    5.  
    6. Player p = (Player) e.getPlayer();
    7. GameMode nieuw = e.getNewGameMode();
    8.  
    9. if(nieuw == GameMode.SURVIVAL) {
    10. if(inv.containsKey(p.getUniqueId()) && armour.containsKey(p.getUniqueId())) {
    11. p.getInventory().setContents(inv.get(p.getUniqueId()));
    12. p.getInventory().setArmorContents(armour.get(p.getUniqueId()));
    13. }
    14. } else if(nieuw == GameMode.CREATIVE) {
    15. if(inv.containsKey(p.getUniqueId()) && armour.containsKey(p.getUniqueId())) {
    16. armour.replace(p.getUniqueId(), p.getInventory().getArmorContents());
    17. inv.replace(p.getUniqueId(), p.getInventory().getContents());
    18. } else if(!inv.containsKey(p.getUniqueId()) && !armour.containsKey(p.getUniqueId())) {
    19. armour.put(p.getUniqueId(), p.getInventory().getArmorContents());
    20. inv.put(p.getUniqueId(), p.getInventory().getContents());
    21. }
    22. }
    23. }
     
  4. Offline

    DeamonZ

    Please help!
     
Thread Status:
Not open for further replies.

Share This Page