How to remove item from inventory?

Discussion in 'Plugin Development' started by h4344, Nov 22, 2011.

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

    h4344

    Im looking for a way to remove a specific item from a players inventory. I think it would be like inventory.addItem(); Anyone know the code for this and does it require a listener? Ty for any help :)

    (Also if it turns out to be inventory.removeItem(); im gonna feel stupid)
     
  2. Offline

    Lex Talionis

  3. Offline

    h4344

  4. Offline

    Lex Talionis

    Sorry, I didn't test that other post myself, but I figured I'd try pointing just once. Lesson learned. Anyway, if all your plugin cares about is flint and steel, you could just use:
    Code:
    if (player.getInventory().contains(Material.FLINT_AND_STEEL)) {
        player.getInventory().remove(Material.FLINT_AND_STEEL);
    }
    Much simpler.

    Here we go... Got it all worked out nao for removing items other than Flint & Steel in case that's not your only problem item. For a command like /removeitems <player> <itemID>:
    Code:
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            if (plugin.getServer().getPlayer(args[0]) != null && isNumeric(args[1])) {
                Player player = plugin.getServer().getPlayer(args[0]);
                int itemNum = Integer.parseInt(args[1]);
                Material material = Material.getMaterial(itemNum);
    
                if (player.getInventory().contains(material)) {
                    player.getInventory().remove(material);
                }
                return true;
            }
            return false;
        }
    
        public static boolean isNumeric(String string) {
            try {
                int i = Integer.parseInt(string);
            }
            catch(NumberFormatException e) {
                return false;
            }
            return true;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  5. Offline

    h4344

    Thank you for the response :) im pretty sure i got it right now.
     
  6. Offline

    Lex Talionis

    No problem. :3
     
Thread Status:
Not open for further replies.

Share This Page