Prevent Command if Inventory is not empty

Discussion in 'Plugin Development' started by Dysko, Dec 5, 2014.

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

    Dysko

    How would I do this for the command /test? Does it use the PlayerCommandPreprocessEvent?
     
  2. Offline

    Skionz

    Dysko For a new command or one from another plugin?
     
  3. Offline

    Dysko

    Skionz

    A new command that is also in the plugin.
     
  4. Offline

    Googlelover1234

  5. Offline

    Dysko

    Googlelover1234
    I know how to make a command. I'm asking how to prevent the command if the player's inventory is not empty.
     
  6. Offline

    Googlelover1234

    Dysko
    I believe you could loop through the contents of the inventory, and if an item is != to air, then do whatever.
     
  7. Offline

    Dysko

    Googlelover1234
    Code:java
    1. private boolean isInventoryEmpty(Player player) {
    2. for (ItemStack item : player.getInventory().getContents()) {
    3. if (!item.equals(Material.AIR)) {
    4. return false;
    5. }
    6. }
    7. return true;
    8. }


    I am using this code here but it's giving a null pointer exception on the if statement.
     
  8. Offline

    CoolGuy2001

    Code:
    @EventHandler
        public void cancelEmptyInventory(PlayerCommandPreprocessEvent e)
        {
            Player p = e.getPlayer();
            if(!p.getInventory().getContents() == null && !p.getInventory().getArmorContents() == null)
            {
                e.setCancelled(true);
                p.sendMessage(ChatColor.RED + "Your command was cancelled!");
            }
        }
     
Thread Status:
Not open for further replies.

Share This Page