Program: 19853
Project: 19853
Buggy source:
public boolean hatOn(CommandSender sender,Command cmd,String commandLabel,String[] args){
  Player player=(Player)sender;
  if (player.getItemInHand().getTypeId() == 0) {
    player.sendMessage(ChatColor.RED + "String_Node_Str");
    return true;
  }
 else {
    ItemStack itemHand=player.getItemInHand();
    PlayerInventory inventory=player.getInventory();
    ItemStack itemHead=inventory.getHelmet();
    if (itemHead.getTypeId() != 0) {
      int empty=inventory.firstEmpty();
      if (empty == -1) {
        player.sendMessage(ChatColor.RED + "String_Node_Str");
      }
 else {
        int itemHandAmount=itemHand.getAmount();
        if (itemHandAmount != 1) {
          int newItemHandAmount=itemHandAmount - 1;
          itemHand.setAmount(newItemHandAmount);
          Material itemId=Material.getMaterial(itemHand.getTypeId());
          ItemStack newHead=new ItemStack(itemId,1);
          if (itemHand.getTypeId() == 35 || itemHand.getTypeId() == 17 || itemHand.getTypeId() == 18 || itemHand.getTypeId() == 44) {
            short itemHandData=itemHand.getDurability();
            newHead=new ItemStack(itemId,1,itemHandData);
          }
          inventory.setHelmet(newHead);
        }
 else {
          inventory.setHelmet(itemHand);
          inventory.setItemInHand(null);
          player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
          return true;
        }
        inventory.setItem(empty,itemHead);
        return true;
      }
      return false;
    }
 else {
      int itemHandAmount=itemHand.getAmount();
      if (itemHandAmount != 1) {
        int newItemHandAmount=itemHandAmount - 1;
        itemHand.setAmount(newItemHandAmount);
        Material itemId=Material.getMaterial(itemHand.getTypeId());
        ItemStack newHead=new ItemStack(itemId,1);
        if (itemHand.getTypeId() == 35 || itemHand.getTypeId() == 17 || itemHand.getTypeId() == 18 || itemHand.getTypeId() == 44) {
          short itemHandData=itemHand.getDurability();
          newHead=new ItemStack(itemId,1,itemHandData);
        }
        inventory.setHelmet(newHead);
      }
 else {
        inventory.setHelmet(itemHand);
        inventory.setItemInHand(null);
        player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
        return true;
      }
      player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
      return true;
    }
  }
}

Fixed version (ground truth):
public boolean hatOn(CommandSender sender,Command cmd,String commandLabel,String[] args){
  Player player=(Player)sender;
  if (player.getItemInHand().getTypeId() == 0) {
    player.sendMessage(ChatColor.RED + "String_Node_Str");
    return true;
  }
 else {
    ItemStack itemHand=player.getItemInHand();
    PlayerInventory inventory=player.getInventory();
    ItemStack itemHead=inventory.getHelmet();
    if (itemHead.getTypeId() != 0) {
      int empty=inventory.firstEmpty();
      if (empty == -1) {
        player.sendMessage(ChatColor.RED + "String_Node_Str");
      }
 else {
        int itemHandAmount=itemHand.getAmount();
        if (itemHandAmount != 1) {
          int newItemHandAmount=itemHandAmount - 1;
          itemHand.setAmount(newItemHandAmount);
          Material itemId=Material.getMaterial(itemHand.getTypeId());
          ItemStack newHead=new ItemStack(itemId,1);
          if (itemHand.getTypeId() == 35 || itemHand.getTypeId() == 17 || itemHand.getTypeId() == 18 || itemHand.getTypeId() == 44) {
            short itemHandData=itemHand.getDurability();
            newHead=new ItemStack(itemId,1,itemHandData);
          }
          inventory.setHelmet(newHead);
        }
 else {
          inventory.setHelmet(itemHand);
          inventory.setItemInHand(null);
          player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
          inventory.setItem(empty,itemHead);
          return true;
        }
        return true;
      }
      return false;
    }
 else {
      int itemHandAmount=itemHand.getAmount();
      if (itemHandAmount != 1) {
        int newItemHandAmount=itemHandAmount - 1;
        itemHand.setAmount(newItemHandAmount);
        Material itemId=Material.getMaterial(itemHand.getTypeId());
        ItemStack newHead=new ItemStack(itemId,1);
        if (itemHand.getTypeId() == 35 || itemHand.getTypeId() == 17 || itemHand.getTypeId() == 18 || itemHand.getTypeId() == 44) {
          short itemHandData=itemHand.getDurability();
          newHead=new ItemStack(itemId,1,itemHandData);
        }
        inventory.setHelmet(newHead);
      }
 else {
        inventory.setHelmet(itemHand);
        inventory.setItemInHand(null);
        player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
        return true;
      }
      player.sendMessage(ChatColor.YELLOW + "String_Node_Str");
      return true;
    }
  }
}