Inventory issues

Discussion in 'Plugin Development' started by Areoace, Jan 12, 2016.

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

    Areoace

    So I've got a HashMap that stores a player's inventory and drops it when I tell it to, however sometimes there are missing items.

    Edit: The clearInv method gives me an NPE, is there a fix?

    Here's all the methods I use to store and use the methods

    Code:
    public HashMap<Sheep, Inventory> inventoryitems = new HashMap<Sheep, Inventory>();
    
    public void clearInv(Inventory inventory, Entity entity)
    
    {
    
    Player player = (Player) inventory.getHolder();
    
    
    
    for (ItemStack items : inventory.getContents()) 
    
    {
    
    
    
    for (ItemStack armour : player.getInventory().getArmorContents()) 
    
    {
    
    
    
    if(!items.equals(null))
    
    {
    
    if(!armour.equals(null))
    
    {
    
    
    
    entity.getWorld().dropItemNaturally(entity.getLocation(), items);
    
    
    
    entity.getWorld().dropItemNaturally(entity.getLocation(), armour);
    
    
    
    player.getInventory().setHelmet(new ItemStack(Material.AIR, 1));
    
    player.getInventory().setChestplate(new ItemStack(Material.AIR, 1));
    
    player.getInventory().setLeggings(new ItemStack(Material.AIR, 1));
    
    player.getInventory().setBoots(null);
    
    
    
    player.getInventory().clear();
    
    
    
    inventoryitems.remove(items);
    
    }
    
    }
    
    }
    
    }
    
    }
    
    
    
    public void rightClickSheep(Sheep sheep)
    
    {
    
    if(log.containsKey(sheep))
    
    {
    
    
    
    clearInv(inventoryitems.get(sheep), sheep);
    
    
    
    Player player = (Player) inventoryitems.get(sheep).getHolder();
    
    
    
    plugin.getServer().broadcastMessage(ChatColor.BLUE + "[A] " + ChatColor.LIGHT_PURPLE + player.getName() + "" + ChatColor.YELLOW + " dropped their inventory for combat logging");
    
    }
    
    }
    
     
    Last edited: Jan 12, 2016
  2. Offline

    Zombie_Striker

  3. Offline

    metincasper

    @Areoace try to do this instead if say items.equals(null)
    Code:
    if (items==null)
    It may be the same, but give it a try :)
     
  4. Offline

    Xerox262

    It will most certainly not be the same, this will fix the error, calling .equals on a null object will throw the NPE
     
    Zombie_Striker likes this.
Thread Status:
Not open for further replies.

Share This Page