Clearing a players inventory.

Discussion in 'Resources' started by Frazz86, Sep 2, 2012.

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

    Frazz86

    Code:
    public void clearInv(Player p)
    {
    for(int j=0; j<38; j++)
    {
    p.getInventory().setItem(j, null);
    }
    p.getInventory().setHelmet(null);
    p.getInventory().setChestplate(null);
    p.getInventory().setLeggings(null);
    p.getInventory().setBoots(null);
    }
    
    This clear's both the inventory and the armour. To use this simply use it like this:
    If its within a command:
    Code:
    Player p = (Player)sender;
    if(a ==b)
    {
    clearInv(p);
    }
    
    Without a command
    Code:
    Player p;
     
    if(a ==b)
    {
     
    clearInv(p);
    }
    
    If you think you can simplify or think somethign is wrong feel free to post :).

    P.S if you'd like me to explain how the code works feel free to ask :)
     
  2. Offline

    one4me

    This may help you a little bit...
    Code:
    /**To clear items only*/
    p.getInventory().setContents(null);
     
    /**To clear armor only*/
    p.getInventory().setArmorContents(null);
     
    /**To clear items and armor*/
    ((CraftPlayer)p).getHandle().inventory.b(new NBTTagList());
    
    There may be different code that clears the items and armor in a single statement, but I couldn't find it.
     
  3. Offline

    fireball9919

    I'm new to custom codes, I'm trying to reset all my players items in their inventoried but leave their armor intact as well as their enderchest. This can be done by deleting the players .dat file in essentials But I want to keep all their other data intact. How would I go about implementing this code above to make this happen?
     
  4. Offline

    stoneminer02

    Make it like one4me said: (Player)sender.getInventory().setContents(null);
     
  5. Offline

    MineStein

    Inventory.clear();
    Inventory.setArmorContents(null);

    Simple.

    one4me Why use NMS to do something simple like clear an inventory and armor.

    EDIT:
    Why are you specifying the type of excrement in your signature stoneminer02?
     
  6. Offline

    RingOfStorms

    He gave your example as well as the nms example. Your example uses two lines, and he provided that, along with a possible one line example. He didn't necessarily say to use the nms one over the other ones.
     
  7. Offline

    xTrollxDudex

    OP has not been seen since last year. Last reply has not been seen for 7 months. Please make a new thread.
     
    korikisulda likes this.
  8. Offline

    stoneminer02

    What do you mean?
    If you mean that the signature code is awful, thank you :p
    It wasn't meant to be taken serious or to be good.
     
  9. Offline

    MineStein

    stoneminer02 I was just asking why you are specifying the type of fecal matter in an enum?
     
  10. Offline

    d3v1n302418

    stoneminer02 your signature provides inefficient code. With no break from that loop youll crash rather quickly.
     
  11. Offline

    teej107

    It wasn't meant to be taken serious. My old signature was
    Code:java
    1. public void brainFart()
    2. {
    3. Thread.sleep()
    4. }
     
  12. Offline

    d3v1n302418

    teej107 I know, thats the point of a joke :p
     
  13. Offline

    MasterDoctor

    d3v1n302418 You are contradicting yourself - Besides It could be a small part of code without the closing loops!
     
  14. Offline

    d3v1n302418

  15. Offline

    MasterDoctor

  16. d3v1n302418 This code is actually really efficient in that it doesn't compile and therefore never runs at all.
     
    korikisulda likes this.
  17. Offline

    Funergy

Thread Status:
Not open for further replies.

Share This Page