Saving/Pasting Inventories :)

Discussion in 'Plugin Development' started by MrGermanrain, Aug 14, 2013.

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

    MrGermanrain

    I am trying to make it so that when a player vanishes it saves his inventory and when he unvanishes it gives him that inventory again...

    This is my code so far:

    Code:java
    1. this.vanished.remove(sender.getName());
    2. bit = "joined the server";
    3. p.setGameMode(GameMode.SURVIVAL);
    4.  
    5. } else {
    6. Player p = (Player) sender;
    7. this.vanished.add(sender.getName());
    8. bit = "left the server";
    9. p.setGameMode(GameMode.CREATIVE);
    10.  
    11. p.getInventory(); ??
     
  2. Offline

    MistPhizzle

    Create a new HashMap containing the player's name and inventory.
    Code:java
    1.  
    2. public static HashMap<String, Inventory> vanishInvs = new HashMap<String, Inventory>() // Creates the HashMap. You'll probably have to do an import on Inventory.
    3.  
    4. vanishInvs.put(p.getName(), p.getInventory()); //Use this when the player vanishes to put their inventory in the HashMap
    5.  
    6. p.getInventory().clear(); // Clears the inventory.
    7.  
    8. p.getInventory().setContents(vanishInvs.get(p.getName()); // Restores inventory to whatever was in the HashMap
    9. vanishInvs.remove(p.getName()); //Removes the player from the HashMap. (Do this AFTER you restore their old inventory)
    10.  
    11.  
     
  3. Offline

    MrGermanrain


    I did it like this now:

    Code:java
    1. HashMap<String, Inventory> vanishInvs = new HashMap<String, Inventory>(); // Creates the HashMap. You'll probably have to do an import on Inventory.
    2. p.getInventory().setContents(vanishInvs.get(p.getName()); // Restores inventory to whatever was in the HashMap
    3. vanishInvs.remove(p.getName()); //Removes the player from the HashMap. (Do this AFTER you restore their old inventory)
    4.  
    5.  
    6.  
    7. } else {
    8. Player p = (Player) sender;
    9. this.vanished.add(sender.getName());
    10. bit = "left the server";
    11. p.setGameMode(GameMode.CREATIVE);
    12.  
    13. HashMap<String, Inventory> vanishInvs = new HashMap<String, Inventory>(); // Creates the HashMap. You'll probably have to do an import on Inventory.
    14.  
    15. vanishInvs.put(p.getName(), p.getInventory()); //Use this when the player vanishes to put their inventory in the HashMap
    16.  
    17. p.getInventory().clear(); // Clears the inventory.


    Im getting an error message on setContents... Any idea?
     
  4. Offline

    Kuuichi

    The setContents() method takes an ItemStack[] array, so you'll need to get the ItemStack[] array from the old inventory.

    Code:java
    1. p.getInventory().setContents(vanishInvs.get(p.getName()).getContents()); // Restores inventory to whatever was in the HashMap


    Also, I'm not sure if setContents() restores the armor contents, so you should definitely make sure it does.
     
  5. Offline

    MrGermanrain

    I get an internal error while testing the plugin, any idea?
     
  6. Offline

    MistPhizzle

    You don't need .getContents(), it already has the contents when you get the data from the HashMap.
     
  7. Offline

    MrGermanrain

    Im sry, im new to Hashmaps, mind telling me the line of code?
     
  8. Offline

    xTrollxDudex

    This can't be a serious question -_-
    It doesn't. You need a separate HashMap with ItemStack[] somewhere and set it using getArmorContents() and retrieve and setArmorContents(). Make sure to clear the entry after.
     
  9. Offline

    MrGermanrain

    I don't know if you understand what I meant... I have been coding for 2 weeks now and going from simple stuff to this... Its a difference.
     
  10. Offline

    xTrollxDudex

    MrGermanrain
    I don't think "remove .getContents()" is that difficult for a beginner.
     
    tommycake50 likes this.
  11. Offline

    LucasEmanuel

  12. Offline

    xTrollxDudex

    LucasEmanuel
    Not a bad idea. I prefer to create my own objects to store though.
    PHP:
    private class Invent{

    ItemStack[] inv;
    ItemStack[] armor;

    public 
    void save(Player p){
    inv p.getInventory().getContents();
    armor p.getInventory().getArmorContents();
    }

    public 
    ItemStack[] getInv(){
    return 
    this.inv;
    }

    public 
    ItemStack[] getArmor(){
    return 
    this.armor;
    }
    }

    Map<StringInventinv = new HashMap<StringInvent>();

    public 
    void load(Player p){

    Invent i = new Invent();
    i.save(p);

    inv.put(p.getName(), i);
    }

    public 
    void restore(Player p){
    Invent i inv.get(p.getName());

    p.getInventory().setContents(i.getInv());
    p.getInventory().setArmorContents(i.getArmor());

    inv.remove(p.getName());
    }
     
Thread Status:
Not open for further replies.

Share This Page