If Inventory is Empty

Discussion in 'Plugin Development' started by Blah1, Nov 18, 2013.

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

    Blah1

    So I want to check if a player's inventory is empty and if it's not, I want to add his/her whole inventory to hashmap.

    I'm getting an NPE on this block of code:
    Code:java
    1. int i = 0;
    2. for (ItemStack item : player.getInventory().getContents()) {
    3. if (item != null){
    4. i++;
    5. }
    6. if (i < 36){
    7. plugin.getEventMain().inv.put(player, player.getInventory().getContents());
    8. }
    9. }

    *cough* Bukkit team, add player.getInventory().isEmpty() *cough*
     
  2. Offline

    dmoney12321

    You have it set to read "if item is not equal to null" meaning if it even has one if wont execute the code. Did you mean for this?
     
  3. Offline

    Blah1

    dmoney12321 This exact thing works perfectly in my other plugin:
    Code:java
    1. public void Put(Player player){
    2. if (player.getInventory().getArmorContents() != null) {
    3. plugin.getEventMain().armor.put(player, player.getInventory().getArmorContents());
    4. }
    5. if (player.getInventory().getContents() != null) {
    6. plugin.getEventMain().inv.put(player, player.getInventory().getContents());
    7. }
    8. if (!player.getActivePotionEffects().isEmpty()) {
    9. plugin.getEventMain().potion.put(player, player.getActivePotionEffects());
    10. }
    11. for (PotionEffect effect : player.getActivePotionEffects()) {
    12. player.removePotionEffect(effect.getType());
    13. }
    14. }

    But it's giving me errors in this one. So that's why I've been trying to do it different ways
     
  4. Offline

    dmoney12321

    Are you looking for it to come back as not equal to?
     
  5. Offline

    the_merciless

    if (p.getInventory().getContents().length > 0){
    // not empty
    }
     
    jacklin213 likes this.
Thread Status:
Not open for further replies.

Share This Page