[Tutorial] Clearing a players inventory!

Discussion in 'Resources' started by ImPhantom, Feb 24, 2014.

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


    Today, for my first ever tutorial i am going to show you all how to clear your own or another players inventory. I couldn't find a Solid tutorial on how to do this, Even though its pretty easy, i still wanted to show the beginners how to do it. I am creating this tutorial under the assumption that you have already set up a CommandExecutor and your onCommand method.

    First: Set up your command:

    1. if(cmd.getName().equalsIgnoreCase("clearinv")) {
    3. }

    Second: Youll wanna make sure that the player has the "clearinv" permission.

    1. if(s.hasPermission("plugin.clearinv")) {
    3. }

    Third: Check if the amount of arguments = 0.

    1. if(args.length == 0) {
    2. s.sendMessage(ChatColor.RED + "You didn't type enough command arguments! Usage: /clearinv [name]");
    3. }

    Three Point Five: You could also make it where if you dont input any arguments then it clears YOUR inventory by making the code in step Three into this:

    1. if(args.length == 0) {
    2. s.getInventory().clear();
    3. s.updateInventory();
    4. s.sendMessage("Cleared Your Inventory");
    5. }

    Fourth: (else statement to step 3) If more than 0 arguments then get player also if player is not in the server (return false)

    1. else if(args.length > 0) {
    2. Player p1 = p.getServer().getPlayer(args[0]);
    3. if(p1 == null) {
    4. return false;
    5. }

    Fifth: Clear p1's inventory and send a success message

    1. p1.getInventory().clear();
    2. p1.updateInventory();
    3. s.sendMessage(ChatColor.AQUA + "Cleared Inventory of: " + ChatColor.WHITE + p1.getDisplayName());

    And You're done!
    Your finished command should look like this:

    1. if(cmd.getName().equalsIgnoreCase("clearinv")) {
    2. if(s.hasPermission("plugin.clearinv")) {
    3. if(args.length == 0) {
    4. s.sendMessage(ChatColor.RED + "You typed too many command arguments! Usage: /clearinv [name]");
    5. } else if(args.length > 0) {
    6. Player p1 = p.getServer().getPlayer(args[0]);
    7. if(p1 == null) {
    8. return false;
    9. }
    10. p1.getInventory().clear();
    11. p1.updateInventory();
    12. s.sendMessage(ChatColor.AQUA + "Cleared Inventory of: " + ChatColor.WHITE + p1.getDisplayName());
    13. }
    14. }
    15. }
  2. Offline


    You forgot
    1. <player>.updateInventory();
  3. Offline


    I thought that was Depreciated and didn't really matter.
  4. Offline


    Still works! :p
  5. Offline


    Ill throw it in there for the shits and giggles. Note: if it isnt right then ill fix it later. I dont have Access to and IDE at the moment.
  6. Offline


    It is needed when you replace items after clearing
  7. Offline

    lol768 Retired Staff

    The term is deprecated ;)
  8. Offline


    Well you can just add it and then SuppressWarnings so put it in there if you favor it.
  9. Offline


    ........... I think you made it more complex than it actually is to clear an inventory
    Cirno, KingFaris11, iiHeroo and 2 others like this.
  10. Offline



    Just a little reminder

    s.sendMessage(ChatColor.RED+"You typed too many command arguments! Usage: /clearinv [name]");
    How can you have too many arguments when the length is 0? :D
    ZeusAllMighty11 likes this.
  11. Offline


    That will only clear items - If you want to clear armor, use player.getInventory().setArmorContents(null);
  12. Offline


    It's not always needed, and in this case it is not needed.
    MrInspector and ZeusAllMighty11 like this.
  13. Offline


    Haha i guess i didn't really pay attention to my error warnings! Thanks for letting me know.
  14. Offline

    ZeusAllMighty11 Retired Staff

    Since the 1.6 update, I haven't had to use .updateInventory(). Adding items, removing items, setting items.. all of it works realtime, and they appear visually for the player. Not sure why people still need it :p
    sgavster likes this.
  15. Offline


    I have run into many instances when using the inventory.setItem method where the inventory has not updated for the player ;)
  16. Offline


    I found a case the other day: after placing (via code called from a InventoryClickEvent handler) an item into slot X of a custom inventory, I have code which automatically moves the placed item to a different slot in that inventory. Without player.updateInventory(), I found a ghost item left in slot X.

    Rule of thumb: try doing what you need to do without the player.updateInventory() call first. If you see ghost items lying around, add a player.updateInventory() call after making your changes to the inventory.
  17. Offline


    is there a way to remove a specific item, like a dirt block? This tut has been great btw.
  18. Offline


    Sorry for the late response, but this is the way you would do that:

    1. player.getInventory().removeItem(new ItemStack[] { new ItemStack(Material.MaterialYouWant, amount) });
  19. Offline


    Thanks! I only asked yesterday btw!
  20. Offline


    I know. But usually i would respond in the same day.
Thread Status:
Not open for further replies.

Share This Page